Free Educational Resource Center for teachers and students. Includes Interviews,
Sourcecode, Free Software, Research Papers, Articles, Tutorials and much more..
     R E S E A R C H A C T I V I T Y . C O M
Our Fellow Research Center for Ph.D Schollars
Home About Submit & Earn Archives: C And C + + Programming » Dev Packages » Interviews » Php Mysql Programming » Windows Programming
search
Windows Programming > create a button and track click event in winapi

Create a Button and track click event

Creating simple button on a simple dialog using pure window API. And tracking the user input, i.e when user clicks the button.



#include <windows.h>

int button_id = 156; //choose any positive integer value

BOOl WINAPI myProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_CLOSE)
PostQuitMessage(0);

//check if button was clicked
if(message==WM_COMMAND && LOWORD(wParam)==button_id)
MessageBox(hwnd,"button has been clicked","Message title",0);

return false;
}

int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{

MSG msg;
HWND myDialog = CreateWindowEx(
0,WC_DIALOG,"My Window",WS_OVERLAPPEDWINDOW | WS_VISIBLE,
400,100,200,200,NULL,NULL,NULL,NULL
);
//create button
CreateWindowEx(
0,"BUTTON","Click me",BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
50,50,100,25,myDialog,(HMENU)button_id,NULL,NULL
);

SetWindowLong(myDialog, DWL_DLGPROC, (long)myProc);

while(GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}