WinAPI 多控件问题

WinAPI multiple controls issue

我正在尝试创建一个 window 包含一个按钮控件和一个编辑控件。 我使用两种功能创建控件,一种用于按钮控件,一种用于编辑控件。

我遇到的问题是我无法同时在 window 上使用两个控件。 window.

显示的只是第一个函数初始化的控件

代码如下: main.c

LRESULT CALLBACK WindowProc(HWND hWindow, unsigned int message, WPARAM wParam, LPARAM lParam)
{
    HWND* hButtonConvert = NULL;
    HWND* hEditNumber = NULL;

    switch (message)
    {
        case WM_CREATE:
        {
            CreateEditControls(hWindow, hEditNumber); // Only this functions works
            CreateButtonControls(hWindow, hButtonConvert); // hButtonConvert is not displayed
        }
        break;

        ...
    }

    return 0;
}

gui.c

#include <windows.h>
#include "gui.h"

void CreateButtonControls(HWND hWindow, HWND* hButtonConvert)
{
    const char* buttonConvertCaption = "Convert";
    const unsigned int buttonConvertXPosition = 100;
    const unsigned int buttonConvertYPosition = 10;
    const unsigned int buttonConvertWidth = 125;
    const unsigned int buttonConvertHeight = 50;
    const DWORD buttonConvertStyle = WS_CHILD | WS_VISIBLE;

    *hButtonConvert = CreateWindowEx(WS_EX_CLIENTEDGE, "Button", buttonConvertCaption, buttonConvertStyle, buttonConvertXPosition, buttonConvertYPosition, buttonConvertWidth, buttonConvertHeight, hWindow, (HMENU)IDB_CONVERT, NULL, NULL);;
}

void CreateEditControls(HWND hWindow, HWND* hEditNumber)
{
    const char* editNumberCaption = NULL;
    const unsigned int editNumberXPosition = 10;
    const unsigned int editNumberYPosition = 10;
    const unsigned int editNumberWidth = 50;
    const unsigned int editNumberHeight = 30;
    const DWORD editNumberStyle = WS_CHILD | WS_VISIBLE | WS_BORDER;

    *hEditNumber = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", editNumberCaption, editNumberStyle, editNumberXPosition, editNumberYPosition, editNumberWidth, editNumberHeight, hWindow, (HMENU)IDE_NUMBER, NULL, NULL);
}

hButton 和 hEdit 不应声明为指针,如果您打算在其他地方使用它们,则可能需要将它们放在全局范围内。

HWND hButtonConvert = NULL;
HWND hEditNumber = NULL;

LRESULT CALLBACK WindowProc(HWND hWindow...)
{
    switch (message)
    {
        case WM_CREATE:
        {
        CreateEditControls(hWindow, &hEdit);
        CreateButtonControls(hWindow, &hButtonConvert);
        ...
        break;
        }
}

选择:

如果child window有自己的ID那么你不需要保存它的HWND

void CreateEditControls(HWND hWindow)
{
    const int x = 10;
    const int y = 10;
    const int w = 50;
    const int h = 30;
    const DWORD style = WS_CHILD | WS_VISIBLE | WS_BORDER;
    CreateWindowEx(0, "Edit", 0, style, x, y, w, h, hWindow, (HMENU)IDE_NUMBER, NULL, NULL);
}

在另一个范围内:

HWND temporary = GetDlgItem(hWindow, IDE_NUMBER);
SetWindowText(temporary, "hey there");