Sign in to follow this  

C++ Window Button

This topic is 4529 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would you put a button into a C++ window and make it do an action? (I.E. display a message box.) And it's not like I haven't Googled it myself - all the examples are for .NET, which I have NO interest in. [Edited by - orcfan32 on July 18, 2005 10:22:30 PM]

Share this post


Link to post
Share on other sites
You should specify which compiler you are using, which API and whether you are using a dialog/resource editor to create the window or are creating it all in code.

Using MFC and the MSVC dialog editor, it's simply a case of placing a button control on the form and double-clicking on it will create a method that will be executed when the button is clicked at runtime.

Using plain Win32 without the dialog editor, you'd call CreateWindow/CreateWindowEx, specifying "BUTTON" as the window class and your main window's HWND as parent. When clicked, the button sends a WM_COMMAND message to its owner. Win32 tutorial.

Share this post


Link to post
Share on other sites
If you are using MFC but you are not using the Forms designer, you would probably use the CButton class. However, Kippesoep is right - you need to give us a little more information about the language/os/setup you are using.

Share this post


Link to post
Share on other sites
switch(HIWORD(wParam)) {
case BN_CLICKED:
if(LOWORD(wParam)== BUTTON_ID){
//do what ever here
break;
}

just like that

I hade the same question and some one in these forums showed me

Share this post


Link to post
Share on other sites
Assuming it is not MFC, you could do the following:

switch( message )
{
case WM_CREATE:
hButton = CreateWindow( "Button", "Click Me", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, 10, 10, 75, 20, hWnd, (HMENU)ID_BUTTON1, NULL, NULL );
break;

case WM_COMMAND:
switch( wParam )
{
case ID_BUTTON1:
MessageBox( NULL, "You have clicked me!", "Alert", MB_OK );
break;
}
}

Where hButton is declared as HWND and ID_BUTTON1 is defined as a global.

Share this post


Link to post
Share on other sites

This topic is 4529 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this