未创建对话框 WinAPI

Posted

技术标签:

【中文标题】未创建对话框 WinAPI【英文标题】:Dialog boxes are not created WinAPI 【发布时间】:2013-06-21 10:33:49 【问题描述】:

如果我在主窗口中创建了一个子窗口(在本例中为“关于”窗口),则由于某种原因不会调用该对话框。如果您不这样做,则子窗口是一个称为正常的对话框并且可以正常工作。 GetLastError 返回错误号 1812(指定的图像文件不包含资源部分。)。但是从资源文件来看,一切都很好。正如我所说,如果您不创建子窗口,那么一切正常。有什么问题?

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

LONG WINAPI WndProc(HWND, UINT, WPARAM,LPARAM);
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK PointsProc(HWND hPoints, UINT message, WPARAM wParam,LPARAM lParam);

HINSTANCE hInst;
HWND hPoints;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

    hInst = hInstance;
    HWND hwnd;
    MSG msg;
    WNDCLASS w;
    memset(&w,0,sizeof(WNDCLASS));
    w.style = CS_HREDRAW | CS_VREDRAW;
    w.lpfnWndProc = WndProc;
    w.hInstance = hInstance;
    w.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    w.lpszClassName = L"My Class";
    RegisterClass(&w);
    hwnd = CreateWindow(L"My Class", L"My title", WS_OVERLAPPEDWINDOW,
    300, 200, 200, 180, NULL, NULL, hInstance, NULL);

    HMENU main_menu = CreateMenu();
    AppendMenu(main_menu, MF_STRING, 1111, L"Box");

    WNDCLASS w2;
    memset(&w2, 0, sizeof(WNDCLASS));
    w2.lpfnWndProc = (WNDPROC)PointsProc;
    w2.hInstance = hInst;
    w2.lpszClassName = L"About";
    w2.hCursor = LoadCursor(NULL, IDC_ARROW); 
    w2.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(111, 111, 111));
    RegisterClass(&w2);

    hPoints = CreateWindowEx(0, L"About", (LPCTSTR) NULL,
    WS_CHILD | WS_BORDER | WS_VISIBLE | WS_DISABLED, 10, 10,
    100, 100, hwnd, (HMENU)1112, hInst, NULL);

    ShowWindow(hPoints,SW_NORMAL);
    UpdateWindow(hPoints);

    SetMenu(hwnd, main_menu);

    ShowWindow(hwnd,nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg,NULL,0,0))
    
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    
    return msg.wParam;


LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)

    switch (Message)
    
        case WM_COMMAND:
            switch(LOWORD(wparam)) 
            
                case 1111:
                    DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, About);
                    return 0;
            
            break;
        case WM_DESTROY: 
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, Message, wparam, lparam);
    
    return 0;


LRESULT CALLBACK PointsProc(HWND hPoints, UINT message, WPARAM wParam,LPARAM lParam)
   
    switch(message)
       
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_PAINT:
            break;
        default:
            return DefWindowProc(hPoints, message, wParam, lParam);
    
    return 0;


INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)

    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    
        case WM_INITDIALOG:
            return (INT_PTR)TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDCANCEL)
            
                EndDialog(hDlg, LOWORD(wParam));
                return (INT_PTR)TRUE;
            
        break;
    
    return (INT_PTR)FALSE;

【问题讨论】:

【参考方案1】:

对话框调用,只是需要很长时间,因为PointsProc()正在循环...

问题是break; 在处理PointsProc() 中的WM_PAINT 时,它会跳过调用DefWindowProc(),因此窗口不断收到WM_PAINT 消息,因为窗口仍然无效。

//        case WM_PAINT:
//            break;

【讨论】:

以上是关于未创建对话框 WinAPI的主要内容,如果未能解决你的问题,请参考以下文章

Win API,如何在对话框中写入文本?

WinAPI 的 Groupbox 字体问题

使用 WinAPI 创建具有透明背景的文本标签

winapi查找网页对话框的子窗口标题(属性)

如何预先测量静态尺寸? WINAPI

为对话框启用关闭按钮 c++ winapi