CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2006
    Posts
    300

    Cool How to add a button in win32 app

    Hi, this is my first win32 program and I'm making changes while I learn. I have a win32 window that needs functionality and I started looking into how to add buttons to your win32 program from here,

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM param, LPARAM lparam);
    
    int WINAPI WinMain(HINSTANCE currentInstance, HINSTANCE previousInstance, PSTR cmdLine, int cmdCount)
    {
    	// Register the window class
    	const char* CLASS_NAME = "m0nst3r";
    	WNDCLASS wc{};
    	wc.hInstance = currentInstance;
    	wc.lpszClassName = CLASS_NAME;
    	wc.hCursor = LoadCursor(0, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    	wc.lpfnWndProc = WindowProcessMessages;
    	RegisterClass(&wc);
    
    	// Create the window
    	CreateWindow(CLASS_NAME, "Windows Look e-Tr0n",
    		WS_OVERLAPPEDWINDOW | WS_VISIBLE,			// Window style
    		CW_USEDEFAULT, CW_USEDEFAULT,				// Window initial position
    		800, 600,						// Window size
    		0, 0, 0, 0);
    
    	// Window loop
    	MSG msg{};
    	while (GetMessage(&msg, 0, 0, 0)) {
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return 0;
    }
    
    LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM param, LPARAM lparam) {
    	switch (msg) {
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	default:
    		return DefWindowProc(hwnd, msg, param, lparam);
    	}
    }
    This is the first window I have I need a little advice as to how to add a button to start making it a program.

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,240

    Re: How to add a button in win32 app

    A button is usually placed in a dialog box, which is created by calling CreateDialog or DialogBox and is based on a dialog box template resource.
    Code:
    DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_TEMPLATE_ID), hWndOwner, DialogProcedure);
    Attached here is a simple example which can show an "About" dialog box, containing an "OK" button.
    DemoWin32App.zip
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Jun 2025
    Posts
    4

    Re: How to add a button in win32 app

    Quote Originally Posted by dellthinker View Post
    I have a win32 window that needs functionality
    If the primary purpose isn't to learn Win32, it may be easier to use a GUI framework. There are many available, but I tried Win32++ some time ago and liked it very much.

    https://win32-framework.sourceforge.net/

    I started with one of the supplied demo applications and modified it to fit my needs.

  4. #4
    Join Date
    Nov 2006
    Posts
    300

    Re: How to add a button in win32 app

    Quote Originally Posted by ovidiucucu View Post
    A button is usually placed in a dialog box, which is created by calling CreateDialog or DialogBox and is based on a dialog box template resource.
    Code:
    DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_TEMPLATE_ID), hWndOwner, DialogProcedure);
    Attached here is a simple example which can show an "About" dialog box, containing an "OK" button.
    DemoWin32App.zip
    Excellent, now I just need to know how to edit this. How would someone begin programming this solution?
    Last edited by dellthinker; June 25th, 2025 at 02:27 PM.

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,240

    Re: How to add a button in win32 app

    Quote Originally Posted by dellthinker View Post
    Excellent, now I just need to know how to edit this. How would someone begin programming this solution?
    First, install an IDE, as for example Visual Studio (Community edition is complete and free for your purpose). Then, keep ahead!
    Visual Studio Community
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured