Wouldn't it be nice to have a sizable command button in VB6? Face it. In the hidden recesses of your mind you have always wanted to be able to resize that command button in VB6--after the program is running that is. It's just that VB6 has not provided you with a control you could just drop, double click, or paste onto your form that allows you that capability.
Well, this tutorial should show you how to have your own sizable command button. You can resize it. And that's cool, But you'll have to do further modifications, if you want to respond to click events, down states, up states, etc. If the response on this short tutorial is sufficient, I plan to post a tutorial on how to add those events to your VB6 program.
Option Explicit
Private Const WS_THICKFRAME = &H40000
Private Const WS_CHILD = &H40000000
Private Const WS_VISIBLE = &H10000000
Private Const BS_PUSHBUTTON = &H0&
Private Const WS_SIZEBOX = WS_THICKFRAME
Private lngSizeButton As Long
Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" _
(ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, _
ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, _
ByVal hInstance As Long, lpParam As Any) As Long
Private Sub Form_Load()
lngSizeButton = CreateWindowEx(0, "Button", "&Sizeable Button", WS_SIZEBOX Or BS_PUSHBUTTON Or WS_CHILD _
Or WS_VISIBLE, 10, 10, 200, 100, Me.hWnd, 0, 0, 0)
End Sub
Explanation:
You can copy and paste the Windows API CreateWindowEx into your form using the VB6 API Viewer as well as the constants that you will need for use in the API. But the purpose of this API is to create a window. Here you learn that a command button is really just a window with additional properties for click events, appearance, states, etc. The API has 12 different parameters. You may be getting an idea right now as to why Microsoft wrote Visual Basic.
But you don't need the first parameter. That's for extended window styles which you really don't need with a command button. So, you just enter 0 which stands for NULL in the C programming language. But you do need to
be careful to use the word button in the second parameter. When creating a window with this API, the user may use several predefined classes of windows: one of those classes is a button. Don't worry about capitalization. Microsoft doesn't here. The third parameter sets the text which will appear in the window of our sizable button.
And the fourth parameter is interesting. Here is where you can set several different characteristics of a window. Of primary importance is being able to see the window: Use WS_VISIBLE. Also, important is to tell
the program that it is a child window that belongs to another window. In our case the form on which we wish to see the window. Use WS_CHILD. And since we want a push style button, we use BS_PUSHBUTTON. And the one
option we have been waiting for: WS_SIZEBOX. With this option we can size the button using our mouse.
Cool.
The fifth, sixth, seventh, and eighth parameters set the upper left corner (10, 10) and the length and height (200,100).
The ninth parameter asks for the handle of the parent window. That's easy: Me.hWnd gives us that value.
The tenth parameter asks for a handle to a menu. Command Buttons don't need menus; so, leave that NULL or 0 also. And you won't need to supply a handle to the instance of your command button; so you can also leave that NULL or 0. And since the last parameter asks for additional information which we don't need. We also set that to 0.
It's pretty simple. Not much code. But you can click it, resize it. and move it all around the form by resizing it with the left button of your mouse. It just doesn't do much. A future tutorial for adding features is pending, if favorable response is forthcoming to this short tutorial.