CreateWindow“编辑”键入的字符是'invisible'?

CreateWindow “Edit” typed characters are 'invisible'?

在研究了所有我能找到的 CreateWindow("Edit") 调用的正确语法之后,我不得不认输:当我 运行 程序时,我得到的只是编辑框是“隐形字符”。光标在我键入时向右移动,但我输入的字符无处可见。只有当我用鼠标 select 框内容时,我才能看到文本。但是一松开鼠标就什么都看不到了

这是在编辑控件中导致 'text not showing' 的完整代码:

#include <windows.h>

#define FILE_MENU_NEW 1
#define FILE_MENU_OPEN 2
#define FILE_MENU_QUIT 3
#define CHANGE_TITLE 4

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void AddMenus(HWND);
void AddControls(HWND);     
HMENU hMenu;
HWND hEdit;

int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nShowCmd)
{
    WNDCLASS wc = {0}; // Assign 0 to all its elements initialy
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hInstance = hInstance;
    wc.lpszClassName = L"myWindowClass";
    wc.lpfnWndProc = WindowProc; // this is a pointer to a function

    if(!RegisterClass(&wc))
        return -1;

    HWND hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,                               
        L"myWindowClass",                           
        L"Learn to Program Windows - Roger Breton",     
        WS_OVERLAPPEDWINDOW | WS_VISIBLE ,              
        100, 100, 800, 600,
        NULL,       
        NULL,       
        hInstance, 
        NULL        
    );

    if (hwnd == NULL)
    {
        return 0;
    }

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

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_COMMAND:
        switch(wParam)
        {
        case FILE_MENU_QUIT:
            DestroyWindow(hWnd);    
            break;

        case FILE_MENU_NEW:
            MessageBeep(MB_ICONINFORMATION);
            break;

        case CHANGE_TITLE:
            wchar_t text[100];
            GetWindowTextW(hEdit, text, 100);   
            SetWindowTextW(hWnd, text);
            break;
        }

    case WM_CREATE:             
        AddMenus(hWnd);
        AddControls(hWnd);
        break;

    case WM_DESTROY:                
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);
        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW));
        EndPaint(hWnd, &ps);
    }

    return 0;
    }

    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

void AddMenus(HWND hWnd)    
{
    hMenu = CreateMenu(); 
    HMENU hFileMenu = CreateMenu();
    HMENU hSubMenu = CreateMenu(); 
    AppendMenu(hSubMenu, MF_STRING, NULL, L"Sub-Menu");
    AppendMenu(hFileMenu, MF_STRING, FILE_MENU_NEW, L"New");
    AppendMenu(hFileMenu, MF_POPUP, (UINT_PTR)hSubMenu, L"Sub-menu ");
    AppendMenu(hFileMenu, MF_SEPARATOR, NULL, NULL);
    AppendMenu(hFileMenu, MF_STRING, FILE_MENU_QUIT, L"Quit");
    AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, L"File");
    AppendMenu(hMenu, MF_STRING, NULL, L"Aide");
    SetMenu(hWnd, hMenu);    
}

void AddControls(HWND hWnd) 
{
    CreateWindowW(L"Static", L"Enter text here:", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER , 200, 100, 150, 50, hWnd, NULL, NULL, NULL);
        hEdit = CreateWindowW(L"Edit", NULL, WS_VISIBLE | WS_CHILD | WS_BORDER , 200, 152, 100, 50, hWnd, NULL, NULL, NULL);
    CreateWindowW(L"Button", L"Changez title", WS_VISIBLE | WS_CHILD, 200, 204, 150, 50, hWnd, (HMENU)CHANGE_TITLE, NULL, NULL); 
}

我多次尝试重新创建源文件,但无济于事。

您的 case WM_COMMAND: 块末尾缺少 break; 语句。按照目前的情况,您的代码将在处理 any WM_COMMAND.

后 'fall through' 到 case WM_CREATE: 代码

添加 break; 语句似乎可以修复您的代码(当我测试它时):

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)     {
        case WM_COMMAND:
            switch (wParam)         {
                case FILE_MENU_QUIT:
                    DestroyWindow(hWnd);
                    break;
                case FILE_MENU_NEW:
                    MessageBeep(MB_ICONINFORMATION);
                    break;
                case CHANGE_TITLE:
                    wchar_t text[100];
                    GetWindowTextW(hEdit, text, 100);
                    SetWindowTextW(hWnd, text);
                    break;
            }
            break; // ** You missed this line! **
        case WM_CREATE:
            AddMenus(hWnd);
            AddControls(hWnd);
            break;
//...

如果您启用所有编译器警告,则可以发现此类错误:MSVC 中的静态代码分析器给出以下消息:

warning C26819: Unannotated fallthrough between switch labels (es.78).