Results 1 to 7 of 7

Thread: [RESOLVED] cutout a circlular region

  1. #1

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Resolved [RESOLVED] cutout a circlular region

    I'm trying to cutout a circular region out of a command button .this is what i have done::

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    4. Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    5. Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
    6. Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
    7. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    8.  
    9. Private m_FormRegion As Long
    10.  
    11. Private Sub CutRect(ByRef outer_rgn As Long)
    12. Const RGN_DIFF = 1
    13. Dim inner_rgn As Long
    14. Dim combined_rgn As Long
    15.  
    16.     inner_rgn = CreateEllipticRgn(100 - 30, 50 - 0, 20, 10)
    17.     combined_rgn = CreateRectRgn(Command1.Width, Command1.Height, Command1.Left - 10, Command1.Top - 10)
    18.     CombineRgn combined_rgn, outer_rgn, inner_rgn, RGN_DIFF
    19.     DeleteObject outer_rgn
    20.     DeleteObject inner_rgn
    21.     outer_rgn = combined_rgn
    22.  
    23. End Sub
    24.  
    25. Private Sub Form_Load()
    26. Dim border_width As Single
    27. Dim title_height As Single
    28.     m_FormRegion = CreateRectRgn(Command1.Width, Command1.Height, Command1.Left - 10, Command1.Top - 10)
    29.     border_width = (Command1.Left + Command1.Width)
    30.     title_height = Command1.Top + Command1.Height
    31.     CutRect m_FormRegion
    32.     SetWindowRgn Command1.hWnd, m_FormRegion, True
    33. End Sub
    34.  
    35. Private Sub Form_Unload(Cancel As Integer)
    36.     If m_FormRegion <> 0 Then
    37.         DeleteObject m_FormRegion
    38.         m_FormRegion = 0
    39.     End If
    40. End Sub

    It works if i place my command1 button at the top left corner of the form .but when i place it anywhere else,it doesnt work....

    any idea why??

    ps:this is the first time i'm coding api's and stuff...

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: cutout a circlular region



    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: cutout a circlular region

    the problem is here...
    VB Code:
    1. m_FormRegion = CreateRectRgn(Command1.Width, Command1.Height, Command1.Left - 10, Command1.Top - 10)

    the CreateRectRgn takes coordinates of the upper left corner (x1,y1) and lower right corner (x2,y2)

    if you wan't to create a region of the whole window then the upper left corner is always (0,0) and the lower right corner is (x.width, x.heigth)

    if you have to do this with several buttons then this code might be easier to use...
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    4. Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    5. Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
    6. Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
    7. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    8.  
    9. Private Const RGN_DIFF = 1
    10.  
    11. Private Sub Form_Load()
    12.  
    13.     CutButtonRgn Command1, 105, 75
    14.    
    15. End Sub
    16.  
    17. Private Sub CutButtonRgn(cmdButton As CommandButton, BorderWidthX As Long, BorderWidthY As Long)
    18.     Dim lngSC As Long
    19.     Dim rct_rgn As Long, elli_rgn As Long, comb_rgn As Long
    20.    
    21.     If Me.ScaleMode = vbPixels Then
    22.         lngSC = 1
    23.     Else
    24.         lngSC = Screen.TwipsPerPixelX
    25.     End If
    26.    
    27.     rct_rgn = CreateRectRgn(0, 0, cmdButton.Width / lngSC, cmdButton.Height / lngSC)
    28.     elli_rgn = CreateEllipticRgn(BorderWidthX / lngSC, BorderWidthY / lngSC, (cmdButton.Width - BorderWidthX) / lngSC, (cmdButton.Height - BorderWidthY) / lngSC)
    29.     comb_rgn = CreateRectRgn(0, 0, 1, 1)
    30.     CombineRgn comb_rgn, rct_rgn, elli_rgn, RGN_DIFF
    31.     SetWindowRgn cmdButton.hWnd, comb_rgn, True
    32.     DeleteObject rct_rgn
    33.     DeleteObject elli_rgn
    34.     DeleteObject comb_rgn
    35. End Sub

  4. #4
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: cutout a circlular region

    Quote Originally Posted by manavo11
    good example

    i guess i can remove some unneeded code from my CutButtonRgn sub.

    VB Code:
    1. Private Sub CutButtonRgn(cmdButton As CommandButton, BorderWidthX As Long, BorderWidthY As Long)
    2.     Dim elli_rgn As Long
    3.        
    4.     elli_rgn = CreateEllipticRgn(BorderWidthX / Screen.TwipsPerPixelX, BorderWidthY / Screen.TwipsPerPixelY, (cmdButton.Width - BorderWidthX) / Screen.TwipsPerPixelX, (cmdButton.Height - BorderWidthY) / Screen.TwipsPerPixelY)
    5.     SetWindowRgn cmdButton.hWnd, elli_rgn, True
    6.     DeleteObject elli_rgn
    7. End Sub

  5. #5
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: cutout a circlular region

    the problem in your code is that Const RGN_DIFF = 4 and not 1 as in your code. it's RGN_AND which is 1 and it is what you need. please check API viewer to confirm this.

    see the Agilaz example (though he made the same mistake), but his example is working. you may need to change the parameters according to your project.
    Show Appreciation. Rate Posts.

  6. #6

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: cutout a circlular region

    Quote Originally Posted by Harsh Gupta
    the problem in your code is that Const RGN_DIFF = 4 and not 1 as in your code. it's RGN_AND which is 1 and it is what you need. please check API viewer to confirm this.

    see the Agilaz example (though he made the same mistake), but his example is working. you may need to change the parameters according to your project.
    I was actually trying to make a hole in the button, thats the reason why i used RGN_DIFF ,later on i chngd my mind to cutting out the circular button .thanks a lot for that link .

    p.s: its actually my first attempt with API's .I need to learn a lot abt API's

  7. #7
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: cutout a circlular region

    Quote Originally Posted by litlewiki
    I was actually trying to make a hole in the button, thats the reason why i used RGN_DIFF ,later on i chngd my mind to cutting out the circular button .thanks a lot for that link .

    p.s: its actually my first attempt with API's .I need to learn a lot abt API's
    you can do that. use the Agilaz code and instead of 1 - RGN_AND, use 4 - RGN_DIFF.
    Show Appreciation. Rate Posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width