【问题标题】:Win32 GUI : failing to create win32 GUI dialogWin32 GUI:无法创建 win32 GUI 对话框
【发布时间】:2013-10-08 19:03:34
【问题描述】:

我正在尝试创建一个简单的 dll,其中包含一个 .rc 文件,其中包含简单的对话框和列表框。我在 Visual Studio 的帮助下通过拖放控件创建了资源。我已经公开了一个实习生调用 DialogBox() API 的函数。

我正在从示例 Windows 应用程序动态加载 dll 并调用公开的函数。对话框创建失败,错误代码为 126

任何人都可以帮助我为什么它会这样!?

代码如下:

INT_PTR CALLBACK WndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {

    case WM_INITDIALOG:
        {
               InitCommonControls();
               PopulateList(hwndDlg);
               return TRUE;
        }
    case WM_COMMAND:
        {
          switch(wParam)
          {
          case IDOK:
              SaveSelectedItem(hwndDlg);
               EndDialog(hwndDlg,0);    
               return TRUE;
         case IDCANCEL:
               EndDialog(hwndDlg, 0);
               return TRUE;

          }

        }   
    default:
        DefWindowProc(hwndDlg, uMsg, wParam, lParam);

    }
}
HINSTANCE gInstance;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow) 
{
DialogBox(gInstance, MAKEINTRESOURCE(IDD_DIALOG), hwnd, WndProc);

return TRUE;
}

【问题讨论】:

  • 你的程序在调用InitCommonControlsEx()吗?
  • 你能显示一两行代码吗?
  • 你的代码在哪里?我看不到代码。
  • @MarkRansom:我想看看整个函数!
  • 请注意,您致电InitcommonControls() 为时已晚。您需要在调用DialogBox 之前调用它,因为对话框上的控件是在您收到WM_INITDIALOG 消息之前创建的。

标签: c++ windows winapi user-interface win32gui


【解决方案1】:

您永远不会分配给gInstance,因此它默认初始化为NULL。然后将其传递给DialogBox

WinMain 中将hInstance 分配给gInstance

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow) 
{
    gInstance = hInstance;
    DialogBox(gInstance, MAKEINTRESOURCE(IDD_DIALOG), hwnd, WndProc);
    return TRUE;
}

或者干脆完全取消gInstance,因为您不会在其他任何地方使用它。删除变量并使您的WinMain 像这样:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow) 
{
    DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), hwnd, WndProc);
    return TRUE;
}

您省略了更多代码,因为我看不到hwnd 的声明或初始化。如果可能的话,最好展示一个完整的 SSCCE,这显然是可能的。

还要注意 Raymond 对该问题的评论,并将对 InitCommonControls 的调用移至 WinMain。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多