|
-
May 27th, 2001, 09:33 AM
#1
Thread Starter
Frenzied Member
Adding buttons.....
How do I add buttons to my main window???
I'm using pure API....NO MFC!!
-
May 27th, 2001, 09:58 AM
#2
Banned
I'm a beginner but I think you must use the CreateWindow function
-
May 27th, 2001, 11:33 AM
#3
Code:
hButton = CreateWindowEx(NULL, "Button", "MyButton", WS_CHILD | WS_VISIBLE,
0, 0, 50, 50, hWnd_App, NULL, hInstance, NULL);
-
May 27th, 2001, 11:34 AM
#4
Frenzied Member
If you are not using resources (dialog) then use the CreateWindow API and pass "BUTTON" as the classname parameter. This will create a button. If you are using resources just draw the button.
-
May 27th, 2001, 11:35 AM
#5
Frenzied Member
Code:
Button Styles
If you create a button by specifying the BUTTON class with the CreateWindow or CreateWindowEx function, you can specify a combination of the button styles in the following table.
Style Meaning
BS_3STATE Creates a button that is the same as a check box, except that the box can be grayed as well as checked or unchecked. Use the grayed state to show that the state of the check box is not determined.
BS_AUTO3STATE Creates a button that is the same as a three-state check box, except that the box changes its state when the user selects it. The state cycles through checked, grayed, and unchecked.
BS_AUTOCHECKBOX Creates a button that is the same as a check box, except that the check state automatically toggles between checked and unchecked each time the user selects the check box.
BS_AUTORADIOBUTTON Creates a button that is the same as a radio button, except that when the user selects it, The system automatically sets the button's check state to checked and automatically sets the check state for all other buttons in the same group to unchecked.
BS_CHECKBOX Creates a small, empty check box with text. By default, the text is displayed to the right of the check box. To display the text to the left of the check box, combine this flag with the BS_LEFTTEXT style (or with the equivalent BS_RIGHTBUTTON style).
BS_DEFPUSHBUTTON Creates a push button that behaves like a BS_PUSHBUTTON style button, but also has a heavy black border. If the button is in a dialog box, the user can select the button by pressing the enter key, even when the button does not have the input focus. This style is useful for enabling the user to quickly select the most likely (default) option.
BS_GROUPBOX Creates a rectangle in which other controls can be grouped. Any text associated with this style is displayed in the rectangle's upper left corner.
BS_LEFTTEXT Places text on the left side of the radio button or check box when combined with a radio button or check box style. Same as the BS_RIGHTBUTTON style.
BS_OWNERDRAW Creates an owner-drawn button. The owner window receives a WM_MEASUREITEM message when the button is created and a WM_DRAWITEM message when a visual aspect of the button has changed. Do not combine the BS_OWNERDRAW style with any other button styles.
BS_PUSHBUTTON Creates a push button that posts a WM_COMMAND message to the owner window when the user selects the button.
BS_RADIOBUTTON Creates a small circle with text. By default, the text is displayed to the right of the circle. To display the text to the left of the circle, combine this flag with the BS_LEFTTEXT style (or with the equivalent BS_RIGHTBUTTON style). Use radio buttons for groups of related, but mutually exclusive choices.
BS_USERBUTTON Obsolete, but provided for compatibility with 16-bit versions of Windows. Win32-based applications should use BS_OWNERDRAW instead.
BS_BITMAP Specifies that the button displays a bitmap.
BS_BOTTOM Places text at the bottom of the button rectangle.
BS_CENTER Centers text horizontally in the button rectangle.
BS_ICON Specifies that the button displays an icon.
BS_FLAT Specifies that the button is two-dimensional; it does not use the default shading to create a 3-D image.
BS_LEFT Left-justifies the text in the button rectangle. However, if the button is a check box or radio button that does not have the BS_RIGHTBUTTON style, the text is left justified on the right side of the check box or radio button.
BS_MULTILINE Wraps the button text to multiple lines if the text string is too long to fit on a single line in the button rectangle.
BS_NOTIFY Enables a button to send BN_DBLCLK, BN_KILLFOCUS, and BN_SETFOCUS notification messages to its parent window. Note that buttons send the BN_CLICKED notification message regardless of whether it has this style.
BS_PUSHLIKE Makes a button (such as a check box, three-state check box, or radio button) look and act like a push button. The button looks raised when it isn't pushed or checked, and sunken when it is pushed or checked.
BS_RIGHT Right-justifies text in the button rectangle. However, if the button is a check box or radio button that does not have the BS_RIGHTBUTTON style, the text is right justified on the right side of the check box or radio button.
BS_RIGHTBUTTON Positions a radio button's circle or a check box's square on the right side of the button rectangle. Same as the BS_LEFTTEXT style.
BS_TEXT Specifies that the button displays text.
BS_TOP Places text at the top of the button rectangle.
BS_VCENTER Places text in the middle (vertically) of the button rectangle.
-
May 27th, 2001, 11:36 AM
#6
Monday Morning Lunatic
Correct 
Use CreateWindow (or CreateWindowEx depending on what you want) with a class name of "BUTTON". Make sure you specify the WS_CHILD style for it, and pass the window handle to the window you want to put it on as the parent window.
In your window procedure, you'll need to catch the WM_COMMAND message:
Code:
case WM_COMMAND:
if(LOWORD(wParam) == BN_CLICKED && (HWND)lParam == ghWnd_TheButton) {
MessageBox(hWnd, "Button clicked!", "Button", MB_OK);
}
return 0;
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 27th, 2001, 11:38 AM
#7
Monday Morning Lunatic
Why does everyone always leave a question and then all reply at the same time? 
PS: GD, is that Neko in your sig?
PPS: (and yes, I am a Nazi about this) Vlatko - despite MS's function names and documentation, it's supposed to be spelt Dialogue...but oh well
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 27th, 2001, 11:43 AM
#8
Isn't Dialogue is U.K spelling, and Dialog the U.S spelling?
-
May 27th, 2001, 11:43 AM
#9
Frenzied Member
Vlatko - despite MS's function names and documentation, it's supposed to be spelt Dialogue...but oh well
It is not MS' fault a spell it dialog, it's just my English.
Oh, well!
-
May 27th, 2001, 11:45 AM
#10
Frenzied Member
This thread should go in the "spelling" forum.
-
May 27th, 2001, 11:45 AM
#11
Monday Morning Lunatic
Nothing personal towards anyone's spelling 
Megatron -- well, yes, but since we're in England ours is the correct spelling of an English word 
Okay, I'll shut up now...I've indulged my reputation enough
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 27th, 2001, 11:49 AM
#12
Banned
GD: Is that Neko in your sig?
That's right parksie.
Last edited by Good Dreams; May 27th, 2001 at 11:52 AM.
-
May 27th, 2001, 03:19 PM
#13
Thread Starter
Frenzied Member
Thanks for all your replies guys, but where do I put the code?? Do I put it after WinMain() since hInstance is declared there, and should I declare hButton as an Integer??
I only get one error regarding the hInstance not being declared, because I put the code in WM_CREATE...
-
May 27th, 2001, 03:24 PM
#14
Monday Morning Lunatic
I usually create them in WinMain after the main window's been successfully created, but you can put them in WM_CREATE (personal preference).
If you use WM_CREATE, the instance comes from:
Code:
HINSTANCE hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_INSTANCE);
(or is it GWL_HINSTANCE?)
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 27th, 2001, 03:32 PM
#15
Frenzied Member
It is GWL_HINSTANCE!
-
May 27th, 2001, 03:34 PM
#16
Monday Morning Lunatic
I knew it was one of them, but I haven't got much of my system reinstalled yet (was reformatting at 2:30AM)
Thanks!
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 27th, 2001, 03:47 PM
#17
Frenzied Member
I really hate thet "after format reistalling". I suggest you use Norton Ghost to make an image of your disk after formatting (with all the necessary stuff installed) and then after formatting just get it all back (1GB in 20 mins) without installing piece by piece.
-
May 27th, 2001, 03:50 PM
#18
Monday Morning Lunatic
Yeah, it's a b*tch Using something like Ghost is okay, but normally I just delete c:\win98 and reinstall...this time I backed it up and reformatted it because my Program Files was about 3GB 
I like the suggestion...will think about that
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 27th, 2001, 06:14 PM
#19
Originally posted by Vlatko
I really hate thet "after format reistalling". I suggest you use Norton Ghost to make an image of your disk after formatting (with all the necessary stuff installed) and then after formatting just get it all back (1GB in 20 mins) without installing piece by piece.
Interesting. Where does it store all of this information? On a CD? Or is it stored in the same place where you retrieve the "Undeleted files"?
-
May 28th, 2001, 05:07 AM
#20
Frenzied Member
I use it to make an image of a partitition and that image file is saved on another partitition or directly (if you have a CD-R) on a CD. The best way is to save the image file on a bootable CD and then afer formatting just instert the CD, boot the system and extract the image file. Everything is done in 20 mins. No drivers instalation (nothing).
-
May 28th, 2001, 09:22 AM
#21
Thread Starter
Frenzied Member
I tried this:
Code:
case WM_CREATE:
{
HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
hButton = CreateWindowEx(NULL, "Button", "MyButton", WS_CHILD | WS_VISIBLE,
0, 0, 50, 50, hwnd, NULL, hInstance, NULL);
return 0;
} break;
But I got the following error:
--------------------Configuration: Win32TrainingApp - Win32 Release--------------------
Compiling...
file.cpp
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(30) : error C2440: '=' : cannot convert from 'struct HWND__ *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
Win32TrainingApp.exe - 1 error(s), 0 warning(s)
-
May 28th, 2001, 10:32 AM
#22
Monday Morning Lunatic
Where is hButton defined?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 28th, 2001, 11:17 AM
#23
Thread Starter
Frenzied Member
Decleration of hButton:
Code:
HWND main_window_handle = NULL;
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;
char buffer[80];
static int counter = 0;
int hButton;
switch(msg)
{
case WM_CREATE:
{
HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
hButton = CreateWindowEx(NULL, "Button", "MyButton", WS_CHILD | WS_VISIBLE,
0, 0, 50, 50, hwnd, NULL, hInstance, NULL);
return 0;
} break;
-
May 28th, 2001, 11:56 AM
#24
Monday Morning Lunatic
CreateWindow returns a window handle (HWND) so that's what hButton should be. Also, hButton needs to be global so it's not lost when you go round again.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 28th, 2001, 12:33 PM
#25
Thread Starter
Frenzied Member
That did the job! Thanks parksie! 
BTW: How do I show a window???
-
May 28th, 2001, 12:37 PM
#26
Banned
I'm a begginer, but...
CyberCarsten: do you visit API sites...? I don't know, but that question seems a bit... something...
-
May 28th, 2001, 12:42 PM
#27
Frenzied Member
Code:
ShowWindow(hWnd,SW_SHOW);
INFO:
Code:
Value Meaning
SW_FORCEMINIMIZE Windows NT 5.0 and later: Minimizes a window, even if the thread that owns the window is hung. This flag should only be used when minimizing windows from a different thread.
SW_HIDE Hides the window and activates another window.
SW_MAXIMIZE Maximizes the specified window.
SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order.
SW_RESTORE Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
SW_SHOW Activates the window and displays it in its current size and position.
SW_SHOWDEFAULT Sets the show state based on the SW_ flag specified in theSTARTUPINFO structure passed to theCreateProcess function by the program that started the application.
SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE Displays the window as a minimized window. The active window remains active.
SW_SHOWNA Displays the window in its current state. The active window remains active.
SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active.
SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
-
May 28th, 2001, 12:53 PM
#28
Thread Starter
Frenzied Member
Vlatko: Thanks for you reply. Is the hWnd the window I want to display??
Good Dreams: I have been searching a very long time for a good windows tutorial, but I never found one...
-
May 28th, 2001, 01:01 PM
#29
Banned
What? Do you think I'm going to put all the APIs in my head? When I want to use an API func I search the reference in micro$oft. Isn't it enough?
Ok, that was my opinion, but you can use the Forger's tutorial. Don't remember the link though 
(Vlatko enters)
-
May 28th, 2001, 01:03 PM
#30
Thread Starter
Frenzied Member
Yeah, I forgot that one. It's actually quite good.
-
May 28th, 2001, 01:17 PM
#31
Thread Starter
Frenzied Member
Vlatko, could you please give an example of the ShowWindow.
I tried the following myself, but it didn't work out...
Code:
HWND hButton;
HWND hDialog;
switch(msg)
{
case WM_CREATE:
{
HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
hButton = CreateWindowEx(NULL, "Button", "MyButton", WS_CHILD | WS_VISIBLE,
0, 0, 50, 50, hwnd, NULL, hInstance, NULL);
hDialog = ShowWindow(IDD_DIALOG1,SW_SHOWNORMAL);
return 0;
} break;
PS: The button code works fine!
-
May 28th, 2001, 02:18 PM
#32
Monday Morning Lunatic
You have to create a window before you can show it. If it's a dialogue you need to use CreateDialog (creates a dialogue like a window and you need a message loop) or DialogBox (doesn't return until the dialogue is closed, no message loop required).
See the two examples on my site (Raw.zip and RawDlg.zip) for how to do this in both ways (I think!)
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 28th, 2001, 02:35 PM
#33
Frenzied Member
HWND hDialog;
hDialog = ShowWindow(IDD_DIALOG1,SW_SHOWNORMAL);
ShowWindow requires the hWnd of the dialog.(hDialog i assume) and not IDD_DIALOG1.
Also ShowWindos's return type isn't HWND it is BOOL.
-
May 28th, 2001, 02:37 PM
#34
Thread Starter
Frenzied Member
You'll have to bare with me parksie, I have just started to learn Windows programming. I can't get the DialogBox function to work...
I'm using the following code:
Code:
DialogBox(hInstance, NULL, hwnd,IDD_DIALOG1);
PS: I couldn't find the link to your page...
-
May 28th, 2001, 02:44 PM
#35
Frenzied Member
Use this:
Code:
DialogBox(NULL, (LPCTSTR)IDD_DIALOG1, NULL, (DLGPROC)DialogProcedure);
-
May 28th, 2001, 02:47 PM
#36
Frenzied Member
Maybe this will help you understand the parameters:
Code:
DialogBox
The DialogBox macro creates a modal dialog box from a dialog box template resource. DialogBox does not return control until the specified callback function terminates the modal dialog box by calling the EndDialog function. The DialogBox macro uses the DialogBoxParam function.
int DialogBox(
HINSTANCE hInstance, // handle to application instance
LPCTSTR lpTemplate, // identifies dialog box template
HWND hWndParent, // handle to owner window
DLGPROC lpDialogFunc // pointer to dialog box procedure
);
Parameters
hInstance
Identifies an instance of the module whose executable file contains the dialog box template.
lpTemplate
Identifies the dialog box template. This parameter is either the pointer to a null-terminated character string that specifies the name of the dialog box template or an integer value that specifies the resource identifier of the dialog box template. If the parameter specifies a resource identifier, its high-order word must be zero and its low-order word must contain the identifier. You can use the MAKEINTRESOURCE macro to create this value.
hWndParent
Identifies the window that owns the dialog box.
lpDialogFunc
Pointer to the dialog box procedure. For more information about the dialog box procedure, see DialogProc.
Return Values
If the function succeeds, the return value is the nResult parameter in the call to the EndDialog function used to terminate the dialog box.
If the function fails, the return value is –1. To get extended error information, callGetLastError.
Remarks
The DialogBox macro uses the CreateWindowEx function to create the dialog box. DialogBox then sends a WM_INITDIALOG message (and a WM_SETFONT message if the template specifies the DS_SETFONT style) to the dialog box procedure. The function displays the dialog box (regardless of whether the template specifies the WS_VISIBLE style), disables the owner window, and starts its own message loop to retrieve and dispatch messages for the dialog box.
When the dialog box procedure calls the EndDialog function, DialogBox destroys the dialog box, ends the message loop, enables the owner window (if previously enabled), and returns the nResult parameter specified by the dialog box procedure when it called EndDialog.
Windows 95 and later: The system can support a maximum of 255 controls per dialog box template. To place more than 255 controls in a dialog box, create the controls in the WM_INITDIALOG message handler rather than placing them in the template.
-
May 28th, 2001, 02:53 PM
#37
If you specify the WS_VISIBLE style, you don't need to use ShowWindow.
-
May 28th, 2001, 04:50 PM
#38
Monday Morning Lunatic
Originally posted by CyberCarsten
find the link to your page...
www.parksie.net/Raw.zip
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 29th, 2001, 09:18 AM
#39
Thread Starter
Frenzied Member
Thanks parksie! 
Vlatko, I get the following error:
--------------------Configuration: Win32TrainingApp - Win32 Release--------------------
Compiling...
file.cpp
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(33) : error C2065: 'DialogProcedure' : undeclared identifier
Error executing cl.exe.
Win32TrainingApp.exe - 1 error(s), 0 warning(s)
-
May 29th, 2001, 11:38 AM
#40
Frenzied Member
Under DialogProcedure i meant the name of your dialog's procedure (where the dialogs events are handled).
Example:
Code:
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
....
DialogBox(NULL, (LPCTSTR)IDD_DIALOG1, NULL, (DLGPROC)About);
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
break;
}
return 0;
}
Last edited by Vlatko; May 29th, 2001 at 11:45 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|