C++ button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lumpybanana247
    New Member
    • Apr 2007
    • 134

    C++ button

    this is kind of a lame question, but in C++ (mingw/devc++ for example) how do you simply add a button to your window (assuming your window is set up something like...)

    Code:
    #include <windows.h>
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    char szClassName[ ] = "WindowsApp";
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdArgumentCmdLine, int nCmdShow) 
    {
        HWND hwnd;          
        MSG messages;         
        WNDCLASSEX wincl;  
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;  
        wincl.style = CS_DBLCLKS; 
        wincl.cbSize = sizeof (WNDCLASSEX);
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;  
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
        if (!RegisterClassEx (&wincl))
        return 0;
        hwnd = CreateWindowEx (0, szClassName, "Windows App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL,hThisInstance, NULL);
        ShowWindow (hwnd, nCmdShow);
        while (GetMessage (&messages, NULL, 0, 0))
        {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        return messages.wParam;
    }
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                
        {
            case WM_DESTROY:
                PostQuitMessage (0);      
                break;
            default:                    
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
    }
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Your code is the basic window setup any Win32 API tutorial will show you. Continue reading the tutorial (or preferably, Petzold's book), to see how to add buttons.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      You call the function CreateWindow (or CreateWindowEx) . One of the things about Windows is that everything, window, dialogue box, buttons and other controls is a window. Just with differing behaviour.

      Anyway look up CreateWindow(Ex ) on MSDN and it will tell you how.

      Comment

      Working...