[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:
Option Explicit
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private m_FormRegion As Long
Private Sub CutRect(ByRef outer_rgn As Long)
Const RGN_DIFF = 1
Dim inner_rgn As Long
Dim combined_rgn As Long
inner_rgn = CreateEllipticRgn(100 - 30, 50 - 0, 20, 10)
combined_rgn = CreateRectRgn(Command1.Width, Command1.Height, Command1.Left - 10, Command1.Top - 10)
CombineRgn combined_rgn, outer_rgn, inner_rgn, RGN_DIFF
DeleteObject outer_rgn
DeleteObject inner_rgn
outer_rgn = combined_rgn
End Sub
Private Sub Form_Load()
Dim border_width As Single
Dim title_height As Single
m_FormRegion = CreateRectRgn(Command1.Width, Command1.Height, Command1.Left - 10, Command1.Top - 10)
border_width = (Command1.Left + Command1.Width)
title_height = Command1.Top + Command1.Height
CutRect m_FormRegion
SetWindowRgn Command1.hWnd, m_FormRegion, True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If m_FormRegion <> 0 Then
DeleteObject m_FormRegion
m_FormRegion = 0
End If
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...
Re: cutout a circlular region
Re: cutout a circlular region
the problem is here...
VB Code:
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:
Option Explicit
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Const RGN_DIFF = 1
Private Sub Form_Load()
CutButtonRgn Command1, 105, 75
End Sub
Private Sub CutButtonRgn(cmdButton As CommandButton, BorderWidthX As Long, BorderWidthY As Long)
Dim lngSC As Long
Dim rct_rgn As Long, elli_rgn As Long, comb_rgn As Long
If Me.ScaleMode = vbPixels Then
lngSC = 1
Else
lngSC = Screen.TwipsPerPixelX
End If
rct_rgn = CreateRectRgn(0, 0, cmdButton.Width / lngSC, cmdButton.Height / lngSC)
elli_rgn = CreateEllipticRgn(BorderWidthX / lngSC, BorderWidthY / lngSC, (cmdButton.Width - BorderWidthX) / lngSC, (cmdButton.Height - BorderWidthY) / lngSC)
comb_rgn = CreateRectRgn(0, 0, 1, 1)
CombineRgn comb_rgn, rct_rgn, elli_rgn, RGN_DIFF
SetWindowRgn cmdButton.hWnd, comb_rgn, True
DeleteObject rct_rgn
DeleteObject elli_rgn
DeleteObject comb_rgn
End Sub
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:
Private Sub CutButtonRgn(cmdButton As CommandButton, BorderWidthX As Long, BorderWidthY As Long)
Dim elli_rgn As Long
elli_rgn = CreateEllipticRgn(BorderWidthX / Screen.TwipsPerPixelX, BorderWidthY / Screen.TwipsPerPixelY, (cmdButton.Width - BorderWidthX) / Screen.TwipsPerPixelX, (cmdButton.Height - BorderWidthY) / Screen.TwipsPerPixelY)
SetWindowRgn cmdButton.hWnd, elli_rgn, True
DeleteObject elli_rgn
End Sub
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.
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
:(
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.