PDA

Click to See Complete Forum and Search --> : Adding buttons.....


CyberCarsten
May 27th, 2001, 09:33 AM
How do I add buttons to my main window???
I'm using pure API....NO MFC!! :D

Good Dreams
May 27th, 2001, 09:58 AM
I'm a beginner but I think you must use the CreateWindow function

Megatron
May 27th, 2001, 11:33 AM
hButton = CreateWindowEx(NULL, "Button", "MyButton", WS_CHILD | WS_VISIBLE,
0, 0, 50, 50, hWnd_App, NULL, hInstance, NULL);

Vlatko
May 27th, 2001, 11:34 AM
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.

Vlatko
May 27th, 2001, 11:35 AM
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.

parksie
May 27th, 2001, 11:36 AM
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:

case WM_COMMAND:
if(LOWORD(wParam) == BN_CLICKED && (HWND)lParam == ghWnd_TheButton) {
MessageBox(hWnd, "Button clicked!", "Button", MB_OK);
}
return 0;

parksie
May 27th, 2001, 11:38 AM
Why does everyone always leave a question and then all reply at the same time? :rolleyes:

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 :rolleyes:

Megatron
May 27th, 2001, 11:43 AM
Isn't Dialogue is U.K spelling, and Dialog the U.S spelling?

Vlatko
May 27th, 2001, 11:43 AM
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. :rolleyes:
Oh, well!:cool:

Vlatko
May 27th, 2001, 11:45 AM
This thread should go in the "spelling" forum.:D

parksie
May 27th, 2001, 11:45 AM
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 :D

Good Dreams
May 27th, 2001, 11:49 AM
GD: Is that Neko in your sig?

That's right parksie.

CyberCarsten
May 27th, 2001, 03:19 PM
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...

parksie
May 27th, 2001, 03:24 PM
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:

HINSTANCE hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_INSTANCE);

(or is it GWL_HINSTANCE?)

Vlatko
May 27th, 2001, 03:32 PM
It is GWL_HINSTANCE!:p

parksie
May 27th, 2001, 03:34 PM
I knew it was one of them, but I haven't got much of my system reinstalled yet (was reformatting at 2:30AM)

Thanks!

Vlatko
May 27th, 2001, 03:47 PM
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.

parksie
May 27th, 2001, 03:50 PM
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 ;)

Megatron
May 27th, 2001, 06:14 PM
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"?

Vlatko
May 28th, 2001, 05:07 AM
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).

CyberCarsten
May 28th, 2001, 09:22 AM
I tried this:


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)

parksie
May 28th, 2001, 10:32 AM
Where is hButton defined?

CyberCarsten
May 28th, 2001, 11:17 AM
Decleration of hButton:


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;

parksie
May 28th, 2001, 11:56 AM
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.

CyberCarsten
May 28th, 2001, 12:33 PM
That did the job! Thanks parksie! ;)
BTW: How do I show a window???

Good Dreams
May 28th, 2001, 12:37 PM
CyberCarsten: do you visit API sites...? I don't know, but that question seems a bit... something...

Vlatko
May 28th, 2001, 12:42 PM
ShowWindow(hWnd,SW_SHOW);


INFO:

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.

CyberCarsten
May 28th, 2001, 12:53 PM
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...

Good Dreams
May 28th, 2001, 01:01 PM
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 :rolleyes:

(Vlatko enters)

CyberCarsten
May 28th, 2001, 01:03 PM
Yeah, I forgot that one. It's actually quite good.

CyberCarsten
May 28th, 2001, 01:17 PM
Vlatko, could you please give an example of the ShowWindow.
I tried the following myself, but it didn't work out...


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! :)

parksie
May 28th, 2001, 02:18 PM
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!)

Vlatko
May 28th, 2001, 02:35 PM
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.

CyberCarsten
May 28th, 2001, 02:37 PM
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:


DialogBox(hInstance, NULL, hwnd,IDD_DIALOG1);


PS: I couldn't find the link to your page...

Vlatko
May 28th, 2001, 02:44 PM
Use this:


DialogBox(NULL, (LPCTSTR)IDD_DIALOG1, NULL, (DLGPROC)DialogProcedure);

Vlatko
May 28th, 2001, 02:47 PM
Maybe this will help you understand the parameters:

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.

Megatron
May 28th, 2001, 02:53 PM
If you specify the WS_VISIBLE style, you don't need to use ShowWindow.

parksie
May 28th, 2001, 04:50 PM
Originally posted by CyberCarsten
find the link to your page... www.parksie.net/Raw.zip

CyberCarsten
May 29th, 2001, 09:18 AM
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)

Vlatko
May 29th, 2001, 11:38 AM
Under DialogProcedure i meant the name of your dialog's procedure (where the dialogs events are handled).
Example:

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;
}

CyberCarsten
May 29th, 2001, 01:18 PM
That works fine Vlatko, except I can't see my main window now! :D

Vlatko
May 29th, 2001, 01:34 PM
Where did you put this code:

DialogBox(NULL, (LPCTSTR)IDD_DIALOG1, NULL, (DLGPROC)About);

CyberCarsten
May 29th, 2001, 01:37 PM
In WM_CREATE

Vlatko
May 29th, 2001, 01:41 PM
The WM_CREATE message is sent when an application requests that a window be created by calling the CreateWindowEx or CreateWindow function. The window procedure of the new window receives this message after the window is created, but before the window becomes visible. The message is sent before the CreateWindowEx or CreateWindow function returns.


Try putting the code in the WM_SHOWWINDOW event (not sure).

CyberCarsten
May 29th, 2001, 02:00 PM
Houston we have a problem....I added the following:

case WM_SHOWWINDOW:
{
DialogBox(NULL, (LPCTSTR)IDD_DIALOG1, NULL, (DLGPROC)About);
return 0;
}break;


And got the following errors:

--------------------Configuration: Win32TrainingApp - Win32 Release--------------------
Compiling...
file.cpp
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(96) : error C2043: illegal break
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(98) : error C2046: illegal case
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(110) : error C2043: illegal break
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(111) : error C2047: illegal default
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(111) : error C2043: illegal break
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(117) : error C2143: syntax error : missing ';' before 'return'
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(118) : error C2143: syntax error : missing ';' before '}'
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(118) : error C2143: syntax error : missing ';' before '}'
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(118) : error C2143: syntax error : missing ';' before '}'
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(127) : error C2143: syntax error : missing ';' before '{'
C:\PROGRAMMER\MICROSOFT VISUAL STUDIO\MyProjects\Win32TrainingApp\file.cpp(127) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.

Win32TrainingApp.exe - 11 error(s), 0 warning(s)