Click here to Skip to main content
15,891,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add a button to my window..
And here's the code i've tried..

C++
#include <Windows.h>
#include <tchar.h>

LRESULT CALLBACK winproc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int cmdShow)
{
	TCHAR className[] = _T("button test");

	WNDCLASS winclass;
	winclass.cbClsExtra = 0;
	winclass.cbWndExtra = 0;
	winclass.hbrBackground = static_cast<HBRUSH>(GetStockObject(LTGRAY_BRUSH));
	winclass.hCursor = LoadCursor(NULL, IDC_CROSS);
	winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	winclass.hInstance = hInstance;
	winclass.lpfnWndProc = winproc;
	winclass.lpszClassName = className;
	winclass.lpszMenuName = NULL;
	winclass.style = CS_VREDRAW | CS_HREDRAW;

	RegisterClass(&winclass);

	HWND hWnd = CreateWindow(className, className,
		WS_OVERLAPPEDWINDOW,
		100, 100, 500, 500,
		NULL, NULL, hInstance, NULL);

	ShowWindow(hWnd, cmdShow);

	MSG msg;
	while (GetMessage(&msg, hWnd, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK winproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message == WM_CREATE)
	{
		HWND hWndButton = CreateWindow(TEXT("BUTTON"), TEXT("btn"),
			WS_CHILD | WS_VISIBLE | BS_PUSHBOX,
			10, 10, 100, 25,
			hWnd, NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
	}
	if (message == WM_COMMAND)
	{
		MessageBox(hWnd, _T(""), _T(""), MB_OK);
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}


and here's a screen shot of my window

[IMG]http://i61.tinypic.com/2gxelxi.png[/IMG]

http://i61.tinypic.com/2gxelxi.png[^]

it doesn't even send WM_COMMAND when I click on it.. what's the wrong with my code ?

thanks in advance !
Posted
Updated 3-Feb-18 16:53pm
Comments
enhzflep 16-Jul-14 10:18am    
From the MSDN help for BS_PUSHBOX:

"The PUSHBOX resource-definition statement creates a push-box control, which is identical to a PUSHBUTTON, except that it does not display a button face or frame; only the text appears."

When I remove the BS_PUSHBUTTON style and build I
(a) get a button with text on it
and
(b) get a WM_COMMAND when it's clicked.

I often use ResEdit - both for creating actual resources, but also as a means to quickly generate the code needed to create a control/font from scratch. By making a dummy dialog and adding the control I want, followed by selecting File->Code Preview->C++ Source.
M­­ar­­­­k 16-Jul-14 10:30am    
i changed as u said.. But I still dont receive the WM_COMMAND message
enhzflep 16-Jul-14 13:12pm    
Okay, so then what does Spy++ (or a non-ms variant) tell you happens when you click the button. Try listening for messages both on the main window and on the button itself. You can filter the messages to only return WM_COMMAND ones (or at least, to exclude WM_MOUSEMOVE, WM_NCHITTEST and WM_SETCURSOR ones)

When set to listen to the main window and exclude all messages except WM_COMMAND, I get the following lines presented when I click the button: (along with an empty message box displayed)

<00001> 00320520 S WM_COMMAND wNotifyCode:BN_CLICKED wID:0 hwndCtl:0024106
<00002> 00320520 R WM_COMMAND

The only change I've made from your source as posted, is to remove the BS_PUSHBOX style. I've still got TEXT("BUTTON") as the class and, as you can see, I've still got a control ID of 0.

Built using mingw32-g++ (4.7.something) and run on win7

You need to add the command id to your button (buttons look nicer than boxes) like:
C++
HWND hWndButton = CreateWindow(TEXT("BUTTON"), TEXT("Push Me"),
    WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    10, 10, 100, 25,
    hWnd, (HMENU)ID_MYCOMMAND, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);

where ID_MYCOMMAND is the command identifier for this control, which you can use in your command handler to take the appropriate code path.
 
Share this answer
 
Comments
M­­ar­­­­k 16-Jul-14 12:05pm    
I added.. But I still dont receive any WM_COMMAND messages....
Richard MacCutchan 16-Jul-14 13:41pm    
Well you must be doing something wrong, it worked perfectly for me, for both BS_PUSHBUTTON and BS_PUSHBOX.
Richard MacCutchan 16-Jul-14 13:49pm    
And just to make sure, I copied your code, added an ID value (just a large number) to the CreateWindow for the button, and it worked correctly.
Just a guess: To be able to send a WM_COMMAND message the button needs an id. For template-based dialogs this id normally comes from the template. In your case you must set manually, for example:
C++
SetWindowLong (m_hWnd, GWL_ID, 100); // sets id 100


[ADDED] Another possible error source is that you have specified the Window class "BUTTON" in all uppercase. You should try "Button" instead.
 
Share this answer
 
v2
Comments
M­­ar­­­­k 16-Jul-14 10:29am    
It doesn't change it..
I still dont receive the WM_COMMAND message
nv3 16-Jul-14 12:33pm    
Okay, see my text above for another try.
Richard MacCutchan 16-Jul-14 13:50pm    
Class names are case insensitive; see my latest comments below.
nv3 16-Jul-14 15:53pm    
Oh, you are so right. He seems to be doing something wrong, but what?
Richard MacCutchan 16-Jul-14 15:55pm    
Well, as with so many questions we only get half the story. :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900