-
[RESOLVED] Need some help with obscure shaped forms API
Hey guys,
The API here: http://www.vbforums.com/showpost.php...27&postcount=5 allows you to do custom shaped forms, namely rectangular with rounded edges. It works well, it's easy and lightweight and I use it often.
When you [uncomment] and apply your needed parameters to one of the hRgn lines, it will round the corners based off of your given values
Code:
hRgn = CreateRoundRectRgn(0, 0, w, h, 9, 9)
Imagine you have a form with rounded corners like this; http://img507.imageshack.us/img507/2479/form1sp2.jpg - A result of something using the code I just pasted above.
My question is, how can I tell it to crop my form like this as well: http://img53.imageshack.us/img53/8035/form2tv5.jpg
My natural assumption was to modify the above code to something like this:
Quote:
hRgn = CreateRoundRectRgn(0, 120, w, h, 9, 9)
But by doing this it cropped off the top portion. Then it looked like this, http://img132.imageshack.us/img132/9818/form3qo5.jpg
Again, the way I want it to look is like this: http://img53.imageshack.us/img53/8035/form2tv5.jpg
I understand thats not how this API is supposed to work, and it is only doing what it thinks it should by giving me undesired effects, but I was wondering what I can do to modify or add this ability.
Sorry if I didn't explain well, I tried to make it easy to understand with basic pictures and stuff. Any responses would be great and much appreciated!
Thanks!!
David
-
Re: Need some help with obscure shaped forms API
You probably have to make two API calls. Remember that your first 4 arguments are the coordinates of the rectangle area to "round" So figure out the coords for the center 1/2 up and create two 1/4 rounds, once for each side of the form.
-
Re: Need some help with obscure shaped forms API
You mean like:
Code:
hRgn = CreateRoundRectRgn(0, 0, w, 125, 9, 9)
hRgn = CreateRoundRectRgn(0, 125, w, h, 9, 9)
That produces this effect: http://img132.imageshack.us/img132/9818/form3qo5.jpg
It only applies the second line.
-
Re: Need some help with obscure shaped forms API
You would also need to call the calls on #67 and 68 inbetween each CreateRectanglregion or use separate return vars as you dont want to be working on the same regions or you will clear the first region with teh second.
-
Re: Need some help with obscure shaped forms API
Something like this? (Same effect is happening still)
Code:
hRgn = CreateRoundRectRgn(0, 0, w, 125, 9, 9)
Call SetWindowRgn(Me.hwnd, hRgn, True)
Call DeleteObject(hRgn)
hRgn2 = CreateRoundRectRgn(0, 125, w, h, 9, 9)
Call SetWindowRgn(Me.hwnd, hRgn2, True)
Call DeleteObject(hRgn2)
-
Re: Need some help with obscure shaped forms API
I think you might want to play with CombineRgn
-
Re: Need some help with obscure shaped forms API
What i've been doing has worked well, is there any way we can continue on with what Rob was talking about? I get the idea my above code was just done incorrectly, but would work otherwise?
Thanks
-
Re: Need some help with obscure shaped forms API
Actually the only easy way to combine two regions is CombineRgn so use what you have learned with Rob with that and you are sorted. It's an easy API.
-
Re: Need some help with obscure shaped forms API
@Milk, yea its probably about the same difficulity but thats another API for him to learn.
We could use two separate regions to get the desired effect and it wouldnt be a combation either. If you look at the design its basically two horizontal rounded rectangles. so two calls with the proper coords and done :D
-
Re: Need some help with obscure shaped forms API
Lol I really don't mean to bother you guys, as I've stated before I know that the point of a forum is to learn, not have things done. So I definitely try before I run in and say "Can you all fix and code this for me?" or "Yes! / No!!!" twenty times in a row (sound familiar? :P)
In conclusion, could you help me a bit more with how I could combine the two rounded rectangles? :P
Thanks, I really do appreciate it.
-
1 Attachment(s)
Re: Need some help with obscure shaped forms API
This is what Im trying to get you to see. Its just two calls to create two separate rounded regions.
I'm not 100% sure if it will need to be combined after (in case of dragging a form type functionality etc) but either way this is your regions. If you need to combine them or not. :)
(Sorry the picture cut off 1 pixel too short at the top and left sides lol
-
Re: Need some help with obscure shaped forms API
Lol okay, but I dont understand why this, with its own separate calls to the API, won't work;
Code:
hRgn = CreateRoundRectRgn(0, 0, w, 145, 9, 9)
Call SetWindowRgn(Me.hwnd, hRgn, True)
Call DeleteObject(hRgn)
hRgn = CreateRoundRectRgn(0, 140, w, h, 9, 9)
Call SetWindowRgn(Me.hwnd, hRgn, True)
Call DeleteObject(hRgn)
-
Re: Need some help with obscure shaped forms API
Maybe it doesnt allow more then one region thus the CombinRegn call I guess. Seems lame that it would restrict you to have only one region
-
Re: Need some help with obscure shaped forms API
I keep getting an Argument Not Optional error.
Code:
Private Sub Form_Resize()
Dim hRgn As Long, w As Long, h As Long, hRgn2 As Long
Dim retval As Long ' return value
Dim p(0 To 3) As POINT
w = ScaleX(Me.Width, Me.ScaleMode, vbPixels) - 1
h = ScaleY(Me.Height, Me.ScaleMode, vbPixels) - 1
'Square Window
'hRgn = CreateRectRgn(0, 0, w \ 2, h \ 2)
'Elliptical Window
'hRgn = CreateEllipticRgn(0, 0, w, h)
'Square Window with round corners
hRgn = CreateRoundRectRgn(0, 0, w, 145, 9, 9)
Call SetWindowRgn(Me.hwnd, hRgn, True)
Call DeleteObject(hRgn)
hRgn2 = CreateRoundRectRgn(0, 140, w, h, 9, 9)
Call SetWindowRgn(Me.hwnd, hRgn2, True)
Call DeleteObject(hRgn2)
retval = CombineRgn(hRgn1, hRgn2, RGN_XOR)
retval = CombineRgn(hRgn1, hRgn2, RGN_AND)
retval = DeleteObject(hRgn1)
retval = DeleteObject(hRgn2)
'Polygon shaped window
'p(0).x = w / 2
'p(0).y = 0
'p(1).x = w
'p(1).y = h / 2
'p(2).x = w / 2
'p(2).y = h
'p(3).x = 0
'p(3).y = h / 2
'hRgn = CreatePolygonRgn(p(0), 4, 1)
End Sub
-
Re: Need some help with obscure shaped forms API
Heres a version I think might be less screwed up. :-p But with this, I get no errors, but after my splashscreen the main form only flashes for a second but disappears:
Code:
Private Sub Form_Resize()
Dim hRgn As Long, w As Long, h As Long, hRgn2 As Long
Dim retval As Long ' return value
Dim p(0 To 3) As POINT
w = ScaleX(Me.Width, Me.ScaleMode, vbPixels) - 1
h = ScaleY(Me.Height, Me.ScaleMode, vbPixels) - 1
'Square Window
'hRgn = CreateRectRgn(0, 0, w \ 2, h \ 2)
'Elliptical Window
'hRgn = CreateEllipticRgn(0, 0, w, h)
'Square Window with round corners
hRgn = CreateRoundRectRgn(0, 0, w, 145, 9, 9)
hRgn2 = CreateRoundRectRgn(0, 140, w, h, 9, 9)
hAndRgn = CreateRectRgn(0, 0, 0, 0) ' meaningless initialization
Call CombineRgn(hAndRgn, hRgn1, hRgn2, RGN_AND)
Call SetWindowRgn(Me.hwnd, hAndRgn, True)
Call DeleteObject(hRgn1)
Call DeleteObject(hRgn2)
'Polygon shaped window
'p(0).x = w / 2
'p(0).y = 0
'p(1).x = w
'p(1).y = h / 2
'p(2).x = w / 2
'p(2).y = h
'p(3).x = 0
'p(3).y = h / 2
'hRgn = CreatePolygonRgn(p(0), 4, 1)
End Sub
-
Re: Need some help with obscure shaped forms API
Your missing arguments in the combine api.
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 CreateRoundRectRgn Lib "gdi32" ( _
ByVal X1 As Long, _
ByVal Y1 As Long, _
ByVal X2 As Long, _
ByVal Y2 As Long, _
ByVal X3 As Long, _
ByVal Y3 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" ( _
ByVal hWnd As Long, _
ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
Private Declare Function CreatePolygonRgn Lib "gdi32" ( _
lpPoint As POINT, _
ByVal nCount As Long, _
ByVal nPolyFillMode As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" ( _
ByVal hObject As Long) As Long
Private Declare Function CombineRgn Lib "gdi32.dll" ( _
ByVal hDestRgn As Long, _
ByVal hSrcRgn1 As Long, _
ByVal hSrcRgn2 As Long, _
ByVal nCombineMode As Long) As Long
Private Type POINT
x As Long
y As Long
End Type
Private Const ALTERNATE = 1
Private Const WINDING = 2
Private Const RGN_AND As Long = 1
Private Const RGN_XOR As Long = 3
Private Sub Form_Resize()
Dim hRgn1 As Long, w As Long, h As Long, hRgn2 As Long
Dim retval As Long ' return value
Dim p(0 To 3) As POINT
w = ScaleX(Me.Width, Me.ScaleMode, vbPixels) - 1
h = ScaleY(Me.Height, Me.ScaleMode, vbPixels) - 1
'Square Window with round corners
hRgn1 = CreateRoundRectRgn(0, 0, w, 145, 9, 9)
Call SetWindowRgn(Me.hWnd, hRgn1, True)
Call DeleteObject(hRgn1)
hRgn2 = CreateRoundRectRgn(0, 140, w, h, 9, 9)
Call SetWindowRgn(Me.hWnd, hRgn2, True)
Call DeleteObject(hRgn2)
retval = CombineRgn(YouNeedAhRgnDestinationVariableHere, hRgn1, hRgn2, RGN_XOR)
retval = CombineRgn(YouNeedAhRgnDestinationVariableHere, hRgn1, hRgn2, RGN_AND)
retval = DeleteObject(hRgn1)
retval = DeleteObject(hRgn2)
End Sub
-
Re: Need some help with obscure shaped forms API
Lol What a mess... Ok heres what I have now.. I did have the arguments up there, btw... Now I'm back to only getting the bottom half showing up:
Code:
Private Sub Form_Resize()
Dim hRgn1 As Long, w As Long, h As Long, hRgn2 As Long
Dim retval As Long ' return value
Dim p(0 To 3) As POINT
w = ScaleX(Me.Width, Me.ScaleMode, vbPixels) - 1
h = ScaleY(Me.Height, Me.ScaleMode, vbPixels) - 1
'Square Window with round corners
hXorRgn = CreateRectRgn(0, 0, 0, 0)
hAndRgn = CreateRectRgn(0, 0, 0, 0)
hRgn1 = CreateRoundRectRgn(0, 0, w, 145, 9, 9)
hRgn2 = CreateRoundRectRgn(0, 140, w, h, 9, 9)
Call SetWindowRgn(Me.hWnd, hRgn2, True)
Call CombineRgn(hXorRgn, hRgn1, hRgn2, RGN_XOR)
Call CombineRgn(hAndRgn, hRgn1, hRgn2, RGN_AND)
Call DeleteObject(hRgn1)
Call DeleteObject(hRgn2)
Call DeleteObject(hXorRgn)
Call DeleteObject(hAndRgn)
End Sub
-
Re: Need some help with obscure shaped forms API
You may be needing to call the SetWindowRgn on the combined region and not just the one
-
Re: Need some help with obscure shaped forms API
Ok I just tried multiple variations but none seemed to do the trick, all of them in fact just result in the form flashing for a split second then hiding upon load. I appreciate all your help, you're probably wondering how I get out of bed in the morning by now! :-p
Code:
Private Sub Form_Resize()
Dim hRgn1 As Long, w As Long, h As Long, hRgn2 As Long
Dim retval As Long ' return value
Dim p(0 To 3) As POINT
w = ScaleX(Me.Width, Me.ScaleMode, vbPixels) - 1
h = ScaleY(Me.Height, Me.ScaleMode, vbPixels) - 1
'Square Window with round corners
hXorRgn = CreateRectRgn(0, 0, 0, 0)
hAndRgn = CreateRectRgn(0, 0, 0, 0)
hRgn1 = CreateRoundRectRgn(0, 0, w, 145, 9, 9)
hRgn2 = CreateRoundRectRgn(0, 140, w, h, 9, 9)
Call SetWindowRgn(Me.hWnd, hRgn1, True)
Call SetWindowRgn(Me.hWnd, hRgn2, True)
Call SetWindowRgn(Me.hWnd, hXorRgn, True)
Call SetWindowRgn(Me.hWnd, hAndRgn, True)
Call CombineRgn(hXorRgn, hRgn1, hRgn2, RGN_XOR)
Call CombineRgn(hAndRgn, hRgn1, hRgn2, RGN_AND)
Call DeleteObject(hRgn1)
Call DeleteObject(hRgn2)
Call DeleteObject(hXorRgn)
Call DeleteObject(hAndRgn)
End Sub
-
Re: Need some help with obscure shaped forms API
Not really as I dont do much of this type of stuff either.
You have variables that are not efined. Always use Option Explicit
hXorRgn and hAndRgn
-
Re: Need some help with obscure shaped forms API
Defined the variables, still didn't work.
4 instances of "SetWindowRgn" seems like a lot to me, but I have no idea what I'm doing here, on the other hand. :-p
-
Re: Need some help with obscure shaped forms API
Ok I've been sitting here trying to get this work for hours straight, lol. I'm just going to post everything I've got that pertains to this, and someone who is knowledgeable on this will hopefully be able to go through it with a fine-tooth comb :thumb:
I would like hRgn1 to be the first source to be merged, hRgn2 being the second - hAndRgn is the target, or destination source.
Declarations; A few are unrelated to this issue and used for other things.
VB6 Code:
Private Const ERROR = 0
Private Const NULLREGION = 1
Private Const SIMPLEREGION = 2
Private Const COMPLEXREGION = 3
Private Const RGN_AND = 1
Private Const RGN_OR = 2
Private Const RGN_XOR = 3
Private Const RGN_DIFF = 4
Private Const RGN_COPY = 5
Private Const GWL_STYLE = (-16)
Private Const WS_MINIMIZEBOX = &H20000
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 CreateRoundRectRgn Lib "gdi32" ( _
ByVal X1 As Long, _
ByVal Y1 As Long, _
ByVal X2 As Long, _
ByVal Y2 As Long, _
ByVal X3 As Long, _
ByVal Y3 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" ( _
ByVal hWnd As Long, _
ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
Private Declare Function CreatePolygonRgn Lib "gdi32" ( _
lpPoint As POINT, _
ByVal nCount As Long, _
ByVal nPolyFillMode As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" ( _
ByVal hObject As Long) As Long
Private Declare Function CombineRgn Lib "gdi32.dll" ( _
ByVal hAndRgn As Long, _
ByVal hRgn1 As Long, _
ByVal hRgn2 As Long, _
ByVal RGN_AND As Long) As Long
Private Type POINT
x As Long
y As Long
End Type
Private Const ALTERNATE = 1
Private Const WINDING = 2
Form_Resize()
I've commented out a few of the Call SetWindowRgn lines, because I don't know which to use, none of them seem to work, in any combination.
Code:
Dim hRgn1 As Long, hRgn2 As Long
Dim w As Long, h As Long
Dim hXorRgn As Long, hAndRgn As Long
Dim p(0 To 3) As POINT
w = ScaleX(Me.Width, Me.ScaleMode, vbPixels) - 1
h = ScaleY(Me.Height, Me.ScaleMode, vbPixels) - 1
'Square Window with round corners
hAndRgn = CreateRoundRectRgn(0, 0, 0, 0, 9, 9)
hRgn1 = CreateRoundRectRgn(0, 0, w, 145, 9, 9)
hRgn2 = CreateRoundRectRgn(0, 140, w, h, 9, 9)
'Call SetWindowRgn(Me.hWnd, hRgn1, True)
'Call SetWindowRgn(Me.hWnd, hRgn2, True)
'Call SetWindowRgn(Me.hWnd, hXorRgn, True)
Call SetWindowRgn(Me.hWnd, hAndRgn, True)
Call CombineRgn(hAndRgn, hRgn1, hRgn2, RGN_AND) ' intersection
Call DeleteObject(hRgn1)
Call DeleteObject(hRgn2)
Call DeleteObject(hAndRgn)
-
Re: Need some help with obscure shaped forms API
I think this is what you need. There is a small mistake in Crptcblades code, once set to a window regions should not be deleted. An exception to the usual GDI delete all your objects when finished with rule.
Code:
Option Explicit
Private Const RGN_OR = 2
Private Declare Function CreateRoundRectRgn Lib "gdi32" ( _
ByVal X1 As Long, _
ByVal Y1 As Long, _
ByVal X2 As Long, _
ByVal Y2 As Long, _
ByVal X3 As Long, _
ByVal Y3 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" ( _
ByVal hWnd As Long, _
ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
Private Declare Function DeleteObject Lib "gdi32" ( _
ByVal hObject As Long) As Long
Private Declare Function CombineRgn Lib "gdi32.dll" ( _
ByVal hDestRgn As Long, _
ByVal hRgnSrc1 As Long, _
ByVal hRgnSrc2 As Long, _
ByVal nCombineMode As Long) As Long
Private Sub Form_Load()
Dim hRgn As Long, hRgnTmp As Long
Dim w As Long, h As Long
Const CORNERSZ = 16
w = ScaleX(Width, ScaleMode, vbPixels)
h = ScaleY(Height, ScaleMode, vbPixels)
'Two rectangular regions with round corners
hRgn = CreateRoundRectRgn(0, 0, w, h \ 2, CORNERSZ, CORNERSZ) 'Create the top region
If hRgn Then
hRgnTmp = CreateRoundRectRgn(0, h \ 2, w, h, CORNERSZ, CORNERSZ) 'Create the bottom region
If hRgnTmp Then
CombineRgn hRgn, hRgn, hRgnTmp, RGN_OR 'Combine using Or (union) into the first region
SetWindowRgn hWnd, hRgn, True 'Apply the new combined region
DeleteObject hRgnTmp 'Delete the unused region
'Don't delete the used region (it won't error, it just won't work, it's now owned by the system not the program)
Else
DeleteObject hRgn 'something has failed, delete the unused region
End If
End If
End Sub
-
Re: Need some help with obscure shaped forms API
Ah that's great!!! Thanks a ton. I'll be messing with this more tomorrow, I've done so much programming today in Javascript and PHP, (and struggling with VB the past couple hours), that I've seemed to have lost any depth perception in my sight. :P I can only take so much staring at syntax highlights in a day. Lol, thanks a bunch.
I appreciate it guys!
David
-
Re: [RESOLVED] Need some help with obscure shaped forms API
-
Re: [RESOLVED] Need some help with obscure shaped forms API
Just wondering, it appears there are not enough supplied arguments to use combineRgn for anymore than three (or two sources) at a time, is that true?
So to add more I'd have to do two separate combineRgn's, then combineRgn those? Just wanting to make sure, before I make a huge mess of this project for nothing. :P
-
Re: [RESOLVED] Need some help with obscure shaped forms API
To me it looks like you would be appending one region as your main source with each new region with the combignregn api. So separate calls appending one to the main. I could be wrong as graphics are not my main area.
-
Re: [RESOLVED] Need some help with obscure shaped forms API
That sounds right.
There are a few more region GDI API's that enable creation of more complex regions with one call...
CreatePolygonRgn
CreatePolyPolygonRgn
ExtCreateRegion
ExtCreateRegion is arguably the most powerful (and the hardest to use)
If you only want to combine a handful of regions CombineRgn is probably still the best way to go.
Just in case you did not know you can apply regions to any windowed control (one with a hWnd.)
-
Re: [RESOLVED] Need some help with obscure shaped forms API
Thanks again.
VB6 is pretty sweet. I am head of the development department at a company which is predominantly web-based development. I sit and do PHP, javascript, but most of all graphic design all day. So when I was assigned this VB6 project for a new brand we're doing, I got overwhelmed a bit, and although I've had my fair share of trouble, it's really been fun and great, and very powerful!
Thanks guys
-
Re: [RESOLVED] Need some help with obscure shaped forms API
Small off topic question: Why didnt they do it in VB.NET?
-
Re: [RESOLVED] Need some help with obscure shaped forms API
That was my initial question, I guess its because the biggest demographic for this software will likely not have .NET 2.0 or above. I've messed with VS.net 2008 a good amount on my own time, and it seems some of these things I've had to learn quite a bit about to do in VB6 are easily done in .NET
-
Re: [RESOLVED] Need some help with obscure shaped forms API
Yea like using the GDI+ classes to do this would have been alot easier. But anyways thanks for answering my question :thumb: