Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User resizing of controls at run time. 11

Status
Not open for further replies.

TheVampire

Programmer
May 1, 2002
828
US
I found this elsewhere, but's it's so neat I thought I'd pass it along.

With just a few API calls, you can set a control to be sizeable by the user while the program is running. The control has to have a windows handle, so there are a few controls that this will not work for. ( like labels and image boxes )

First, you need these API calls and constants in your declarations section:

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Const GWL_STYLE = (-16)
Private Const GWL_EXSTYLE = (-20)
Private Const WS_THICKFRAME = &H40000
Private Const WS_EX_STATICEDGE = &H20000

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Const SWP_FRAMECHANGED = &H20
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOSIZE = &H1


Next, you perform these operations on the control.

SetWindowLong Control.hwnd, GWL_STYLE, GetWindowLong(Control.hwnd, GWL_STYLE) Or WS_THICKFRAME

SetWindowLong Control.hwnd, GWL_EXSTYLE, GetWindowLong(Control.hwnd, GWL_EXSTYLE) Or WS_EX_STATICEDGE

SetWindowPos Control.hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOZORDER Or SWP_FRAMECHANGED


And that's it! Now, when you move your mouse to the border of the control, you will get a sizeing arrow. ( Sorry, I don't know how to make the "Grab Handles" )

If you left click and drag, you can make the control resize!

Different controls act different ways to this. List boxes will automatically show or hide scroll bars as necessary. Combos will not size up or down, but will size left and right.

One of the most useful controls that I found to use with this method is the picturebox. With two ( or more ) pictureboxes set up like this, you can put other controls inside of them and resize them inside the picturebox resize event. This way you can set up a "Splitter", by resizeing one picturebox using the mouse, you can then set the other one's size in code. Like windows explorer and a lot of other programs.

Hope this helps someone else out there!

Robert
 
Hi,
Thank u verymuch for ur valuable tips.


Regds,
vinu
 
Very nice Vamp.
star.gif
 
I just tried it on a frame using win98, and I get the same effect ( the arrow appears, but it will not size ).

I had previously only did this with pictureboxes, listbox and combo box, options, text boxes and buttons, and had not tried frames.

I'll poke around with it and see what I can do.

Robert
 
Well, it might be awhile before I can get back to this. if anyone else has any ideas, I'd appreciate it.

I have had problems with frames not responding to windows messages before, so this might be related to it.

If all else fails, put a picturebox on the form with no border, put the frame inside of it, and on the picturebox resize event, have code to resize the frame to the size of the picturebox.

Robert
 
Nice code, Vampire!

After your poking around, you should place the information in a FAQ. It will make it much easier to find for everyone.

Another star!

Thanks and Good Luck!

zemp
 
Vampire, I decided to do some searching of my own. Found this link that adds the 'grab handles' at runtime for resizing. It uses subclassing.


It might interest you. Apparently you can also control the colour of the grab handles, but I didn't test it thoroughly yet.

You will need the Dbgwproc.dll Debug Object. supposedly available from microsoft. But I did not find it and all links that I found in my searches failed (could not find page...). You can get a copy of this dll from the CCRP ( when you download a copy of their 'folder treeview' control, the dll is included in the download.


Thanks and Good Luck!

zemp
 
Thanks for looking that up zemp. I've already got all of the CCRP controls, so I'm set there.

Robert
 
O i used the Forms' handle.. it looks scary !

Works quite great for other controls tho..

Thanks Robert.
 
Good god
 
I think I'll have to try this just to see what it looks like...

Robert
 
That's super fantastic, being able to do that has been bugging me all night. A star for you Vampire. Now if only someone has some code that allows me to move a control around on a form.....

SOL
I'm only guessing but my guess work generally works for me.
 
That is the BOMB! vamp

"Hacker by Heart"
saenzcorp@hotpop.com
 
great tips vamp, i was really amaze

JBats
Good is not better if not than best...
 
Yes - very nifty, thank you.

Is there a way to do this without changing the physical appearance of the control?
 
Oh and have a star by the way.

Is there anything similarly smart to allow controls to be moved by the user at runtime?
 
>Is there anything similarly smart to allow controls to be moved by the user at runtime?

Use DragMode=Automatic and trap in DragDrop event I suppose!
 
Glasgow,
I had to do this as well and I found the answer last night also on tek-tips. Here's the code.

'Drag parameters for moving/resizing controls

Private Sub text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
mblnDrag = True
mlngOffset = Y
mingoffset2 = X
End Sub

Private Sub text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If mblnDrag Then
If Button = vbLeftButton Then
Text1.Move Text1.Left + (X - mingoffset2), Text1.Top + (Y - mlngOffset)
End If
End If
End Sub

You should now have a totally moveable, resizeable control.


SOL
I'm only guessing but my guess work generally works for me.
 
Thanks SOL. I've never really experimented with dragging and dropping too much in the past. It seems worth pursuing in conjunction with this resize capability but I guess it may be worth starting a new thread if I can't quite master it!
 
As threatened I have continued the 'moving' (rather than resizing) debate under thread222-672576. I hope someone can help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top