1 Pluspunkt 0 Minuspunkte

Wie kann ich einen Button einbauen und wenn man darauf klickt soll ein neues Fenster geöffnet werden.

#include <windows.h>
#include <tchar.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_CLOSE:
            PostQuitMessage(0);
            return 0;
        case WM_COMMAND:
            if (LOWORD(wParam) == BN_CLICKED) {
                MessageBox(hwnd, _T("Button wurde geklickt!"), _T("Button Klick"), MB_OK);
            }
            break;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = _T("SimpleWindowClass");

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0,
        _T("SimpleWindowClass"),
        _T("Button"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        0,
        0,
        hInstance,
        0);

    ShowWindow(hwnd, nCmdShow);

    HWND hButton = CreateWindow(
        _T("BUTTON"),
        _T("Klick mich"),
        WS_CHILD | WS_VISIBLE,
        50, 50, 100, 30,
        hwnd,
        NULL,
        hInstance,
        NULL
    );

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

    return 0;
}

von  

1 Antwort

0 Pluspunkte 0 Minuspunkte

Hier ist ein Beispiel.

#include <windows.h>
#include <tchar.h>

HWND hwnd;

void CreateChildWindow() {

    HWND hNewWindow = CreateWindow(
        _T("SimpleWindowClass"),
        _T("Neues Fenster"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        0,
        0,
        (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
        0);
        
    ShowWindow(hNewWindow, SW_SHOWNORMAL);

}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_CLOSE:
            PostQuitMessage(0);
            return 0;
        case WM_COMMAND:
            if (LOWORD(wParam) == 1) {
                CreateChildWindow();
            }
            break;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = _T("SimpleWindowClass");

    RegisterClass(&wc);

    hwnd = CreateWindowEx(
        0,
        _T("SimpleWindowClass"),
        _T("Button"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        0,
        0,
        hInstance,
        0);

    ShowWindow(hwnd, nCmdShow);

    HWND hButton = CreateWindow(
        _T("BUTTON"),
        _T("Klick mich"),
        WS_CHILD | WS_VISIBLE,
        50, 50, 100, 30,
        hwnd,
        (HMENU)1, // Der Identifier des Buttons ist 1
        hInstance,
        NULL
    );

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

    return 0;
}

von (532 Punkte)