Max
Max

Reputation: 29

How To Detect Pushbutton pressing?

Im trying to build simple application (Using Win32 API) which shows a black window within a button which should close the application, The problem is that I cant figure out how detect a PushBotton click.

Little peace of my code for example:

HWND hButton = CreateWindow(TEXT("Button"),TEXT("Exit"),WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,100,100,50,50,hWnd,0,hInstance,0);

Upvotes: 1

Views: 2162

Answers (2)

Jim Mischel
Jim Mischel

Reputation: 133975

Briefly, you need to give the button an ID and then handle WM_COMMAND messages from that button in your window proc. The article at http://www.infernodevelopment.com/c-win32-api-tutorial gives a decent example.

Upvotes: 6

Mike
Mike

Reputation: 1727

You need to analyze WM_COMMAND message in main window procedure:

LRESULT CALLBACK MainWndProc(  
HWND hwnd,        // handle to window  
UINT uMsg,        // message identifier  
WPARAM wParam,    // first message parameter  
LPARAM lParam)    // second message parameter  
{   
if ((uMsg == WM_COMMAND) && ((HWND)lParam == hButton))  //check MSDN for WM_COMMAND and BN_CLICKED notifications
{  
    //button was pressed  
}  
.......  
}  

Upvotes: 2

Related Questions