| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 479 人关注过本帖
标题:C语言WM_COMMAND接收不到控件消息,只显示窗口不显示对话框?
只看楼主 加入收藏
himdonkin
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2023-4-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
C语言WM_COMMAND接收不到控件消息,只显示窗口不显示对话框?
#include<windows.h>
#include<stdio.h>
#include"resource.h"
static HINSTANCE hInst;
BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_INITDIALOG:
        return TRUE;
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case IDOK:
        case IDCANCEL:
            EndDialog(hDlg, 0);
            return TRUE;
        }
        break;
    }
    return FALSE;
}
void Dialog(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
   
    switch (LOWORD(wParam))
    {
    case IDD_DIALOG1:
        DialogBox(hInst, (char*)IDD_DIALOG1, hWnd, DlgProc);
        
        break;
    }
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam)
{
   
    switch (msgID)
    {
    case WM_COMMAND:
        Dialog(hWnd,msgID,wParam,lParam);
        break;
    case WM_CREATE:
        hInst = ((LPCREATESTRUCT)lParam)->hInstance;
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hWnd, msgID, wParam, lParam);
}
int main()
{
    {
        HINSTANCE hInstance = GetModuleHandle(NULL);

        WNDCLASS wc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 3);
        wc.hCursor = NULL;
        wc.hIcon = NULL;
        wc.hInstance = hInstance;
        wc.lpfnWndProc = WndProc;
        wc.lpszClassName = "main";
        wc.lpszMenuName = NULL;
        wc.style = CS_HREDRAW | CS_VREDRAW;
        RegisterClass(&wc);
        HWND hWnd = CreateWindowEx(0, "main", "Mordem Scanning", WS_OVERLAPPEDWINDOW, 100, 100, 900, 900, NULL, NULL, hInstance, NULL);
        
        ShowWindow(hWnd, SW_SHOW);
        UpdateWindow(hWnd);
        MSG nMsg = { 0 };
        while (1)
        {
            if (PeekMessage(&nMsg, hWnd, 0, 0, PM_NOREMOVE))
            {
                if (GetMessage(&nMsg, hWnd, 0, 0))
                {
                    TranslateMessage(&nMsg);
                    DispatchMessage(&nMsg);
                }
                else
                {
                    return 0;
                }
            }
            else
            {

            }
        }
    }
}
搜索更多相关主题的帖子: HWND break case return NULL 
2023-04-19 17:29
东海ECS
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:Python
等 级:版主
威 望:32
帖 子:412
专家分:1646
注 册:2023-1-24
收藏
得分:20 
在Dialog函数中,需要使用HIWORD函数获取控件ID,而不是LOWORD函数,因为控件ID位于wParam的高16位,而LOWORD函数只获取低16位。因此,修改Dialog函数如下:

void Dialog(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (HIWORD(wParam)) { case BN_CLICKED: switch (LOWORD(wParam)) { case IDD_DIALOG1: DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, DlgProc); break; } break; } }


此外,在DialogBox函数中,应该使用MAKEINTRESOURCE宏来转换IDD_DIALOG1为资源ID,而不是强制类型转换。修改代码如下:

DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, DlgProc);

这样就可以正常显示对话框并接收控件消息了。

会当凌绝顶,一览众山小.
2023-04-19 19:20
快速回复:C语言WM_COMMAND接收不到控件消息,只显示窗口不显示对话框?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015778 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved