Breaking News

C++ Window with Button Click Event


Use the CreateWindow function to create a button control.
In the following C++ example, he BS_DEFPUSHBUTTON style specifies that a default push button should be created. Note that the size and position values must be specified because using CW_USEDEFAULT for a button sets the values to zero.

Code:

#include <windows.h>
#define IDB_BUT 100

LRESULT CALLBACK WindowFunc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam){
    switch(Message) {
        case WM_CREATE:
            CreateWindow("button", 
                "Say Hello", WS_VISIBLE | WS_CHILD, 
                100, 100, 120, 30, hWnd, (HMENU) IDB_BUT, 
                NULL, NULL
            );
            break;

        case WM_COMMAND:
            switch (HIWORD(wParam)) {
                case BN_CLICKED:
                    MessageBox(NULL, "Hello IDEA Developers", "Dialog Box", MB_OK);
                    break;

                default:
                    break;
            }
            break;
        case WM_PAINT: {
                PAINTSTRUCT ps;
                BeginPaint(hWnd, &ps);
                EndPaint(hWnd, &ps);
            }
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hWnd, Message, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode){
WNDCLASSEX WinClass = { 0 };
WinClass.cbSize        = sizeof(WNDCLASSEX);
WinClass.hInstance     = hThisInst;
WinClass.lpszClassName = "BUTEXP";
WinClass.lpfnWndProc   = WindowFunc;
WinClass.style         = 0;
WinClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hIconSm       = LoadIcon(NULL, IDI_WINLOGO);
WinClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
WinClass.lpszMenuName  = NULL;
WinClass.cbClsExtra    = 0;
WinClass.cbWndExtra    = 0;

if (!RegisterClassEx(&WinClass)) {
            MessageBox(NULL, "Cannot register class", "Windows error", MB_OK);
            return 1;
}

HWND hWnd = CreateWindow(
                "BUTEXP", "IDEA Developers", 
                WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 
                CW_USEDEFAULT, 500, 400, NULL, 
                NULL, hThisInst, NULL
        );

if (!(hWnd)) {
            MessageBox(NULL, "Cannot create window", "Windows error", MB_OK);
            return 2;
}

ShowWindow(hWnd, WinMode);
UpdateWindow(hWnd);
MSG Message;

while (GetMessage(&Message, NULL, 0, 0) > 0){
            TranslateMessage(&Message);
            DispatchMessage(&Message);
}
return Message.wParam;
}

5 comments:

  1. Hi, is it possible to use this void function to add button to somewhere?
    --------------------

    BUTTON void button(HWND hwnd, LPCTSTR text, int x, int y, int width, int height) {

    hwnd = CreateWindowEx(
    0,
    WC_BUTTON, // Predefined class; Unicode assumed
    text, // Text will be defined later
    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
    x, // x position
    y, // y position
    width, // Button width
    height, // Button height
    hwnd, // Parent window
    (HMENU) BUTTON_ID, // No menu
    (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
    NULL);

    }

    ReplyDelete
  2. We needed to make it a great and significant issue, an affair event that would be delighted in by the lady and the lucky man as well as by all the wedding visitors. What's more, that is actually how it ended up! The wedding was trailed by a lavish lunch get-together. Everybody raised a toast to the recently wedded couple. marijuana networking events

    ReplyDelete
  3. I have a similar interest this is my page read everything carefully and let me know what you think. Shisha Delivery

    ReplyDelete
  4. Blessings occupied one entire room, accumulating nearly to the roof. We all took photos of the event, even Daniela, disregarding her following marriage outfit. website

    ReplyDelete