Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,104 questions

40,777 answers

573 users

How to create Window with button and click event in C Win32 API

Learn & Practice SQL


2,613 views
asked Dec 17, 2014 by avibootz
edited Jan 28, 2016 by avibootz

2 Answers

0 votes
#include <windows.h>

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  MSG  msg ;    
  HWND hwnd;

  WNDCLASS wc = {0};
	
  wc.lpszClassName = TEXT("Window");
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc;
  
  RegisterClass(&wc);
  hwnd = CreateWindow(wc.lpszClassName, TEXT("Window Title"), 
	                  WS_OVERLAPPEDWINDOW | WS_VISIBLE, 
					  100, 100, 350, 250, NULL, NULL, hInstance, NULL);  

  while(GetMessage(&msg, NULL, 0, 0)) 
  {
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch(msg)  
  {
        case WM_CREATE:
        {
            CreateWindow(TEXT("button"), TEXT("Ok"),  WS_VISIBLE | WS_CHILD ,
                         100, 100, 90, 30, hwnd, (HMENU) 1, NULL, NULL);  
            break;    
        }
        case WM_COMMAND:
        {
            MessageBox(NULL, TEXT("Hello World"), TEXT("MessageBox Title"), 0);
            break;
        }
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}






answered Dec 17, 2014 by avibootz
0 votes
#include <windows.h> 

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  MSG  msg ;    
  HWND hwnd;

  WNDCLASS wc = {0};
	
  wc.lpszClassName = TEXT("Window");
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc;
  
  RegisterClass(&wc);
  hwnd = CreateWindow(wc.lpszClassName, L"Window Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 
					  100, 100, 350, 250, NULL, NULL, hInstance, NULL);  

  while(GetMessage(&msg, NULL, 0, 0)) 
  {
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch(msg)  
  {
		case WM_CREATE:
		{
			CreateWindow(L"button", L"Ok",  WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
						 100, 100, 90, 30, hwnd, (HMENU) 1, NULL, NULL);  


			CreateWindowEx(WS_EX_CLIENTEDGE, L"BUTTON", L"OK", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 
				           50, 50, 200, 30, hwnd, (HMENU) 1, NULL, NULL);

			break;    
		}
		case WM_COMMAND:
		{
			MessageBox(NULL, L"Hello World", L"MessageBox Title", 0);
			break;
		}
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}






answered Dec 18, 2014 by avibootz
...