簡體   English   中英

在單獨的 class 中創建 Win32 控件

[英]Creating Win32 controls in a separate class

I have a Window class and a MainMenu class. In the Window class, I create the window itself, and in the MainMenu class I create the controls for the Window (like in C# with form and user-control).

我是否需要在Window.cppMainMenu.cpp中定義#define EXIT_BUTTON 1以使按鈕事件起作用,還是有更好的方法?

exit = CreateWindow(L"Button", L"Exit", style, monitor.right / 2 - 100, 150, 200, 100, m_hWnd, **HMENU(EXIT_BUTTON)**, NULL, NULL);

Window.cpp

#include "Window.h"

#define EXIT_BUTTON 1

MainMenu* mainMenu1;

Window::Window() : m_hInst(GetModuleHandle(nullptr)) //creates the window
{
    WNDCLASS wc = {};

    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hInstance = m_hInst;
    wc.lpszClassName = ClassName;
    wc.lpfnWndProc = WindProc;

    RegisterClass(&wc);
    
    DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;

    GetWindowRect(GetDesktopWindow(), &monitor);

    m_hWnd = CreateWindow(ClassName, WindowTitle, style, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
    mainMenu1 = new MainMenu(m_hWnd, monitor);

    ShowWindow(m_hWnd, SW_MAXIMIZE);
}

Window::~Window()
{
    UnregisterClass(ClassName, m_hInst);
    delete mainMenu1;
}

LRESULT CALLBACK WindProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) // gets input from user
{
    switch (msg) {
    case WM_CREATE:
        AddControls();
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_COMMAND:
        switch (wp) {
        case EXIT_BUTTON:
            PostQuitMessage(0);
            break;
        }
    case WM_WINDOWPOSCHANGED:
        std::cout << "1";
        break;
    default:
        return DefWindowProcW(hwnd, msg, wp, lp);
    }
    return 1;
}

bool Window::ProcessMessage()
{
    MSG msg = {};
    while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
        if (msg.message == WM_QUIT)
            return false;

        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
    
    return true;
}

void AddControls()
{
    mainMenu1->Initialize();
}

主菜單.cpp

#include "MainMenu.h"

#define EXIT_BUTTON 1

MainMenu::MainMenu(HWND hWnd, RECT monitor)
{
    this->monitor = monitor;
    m_hWnd = hWnd;
}

MainMenu::~MainMenu()
{
    
}

void MainMenu::Initialize()
{
    DWORD style = WS_VISIBLE | WS_CHILD | SS_CENTER;

    title = CreateWindow(L"static", L"Welcome", style, monitor.right / 2 - 100, 100, 200, 100, m_hWnd, NULL, NULL, NULL);
    SendMessage(title, WM_SETFONT, WPARAM(CreateFont(50, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
        OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Arial")), true);
    exit = CreateWindow(L"Button", L"Exit", style, monitor.right / 2 - 100, 150, 200, 100, m_hWnd, HMENU(EXIT_BUTTON), NULL, NULL);
}

void MainMenu::Hide()
{
}

void MainMenu::Show()
{
}

您應該創建 app_common.h header 文件並將定義寫入其中。

app_common.h

#pragma once

#define EXIT_BUTTON 1

並更新您的 Window.cpp

#include "app_common.h"
#include "Window.h"
....

然后更新您的 MainMenu.cpp

#include "app_common.h"
#include "MainMenu.h"
.....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM