资源对话框返回-1

发布于 2024-12-16 20:11:12 字数 1750 浏览 0 评论 0原文

我使用资源编辑器(对话框编辑器?)将对话框创建为资源,

没什么特别的,只是两个组框,两个按钮,一个拆分按钮,两个图片框,3个静态标签,3个编辑框,3个旋转控件,3个系统链接,和一个进度条。

当我按 Ctrl+T 测试对话框时,它似乎工作正常,但是当我按 F5 调试程序时,对话框永远不会出现,程序退出并显示代码 -1 (0xffffffff)

这是我正在使用的代码调用对话框:

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

BOOL CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch (Msg)
    {
        case (WM_COMMAND):
            switch (LOWORD(wParam))
            {
                default:
                    break;
            }
            break;

        default:
            return FALSE;
    }

    return FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    int ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAINWINDOW), NULL, DlgProc);

    int err = GetLastErro(); // This gives 0

    return ret; // This gives -1
}

我在 DlgProc 中还没有任何内容,因为我只想首先显示对话框。

如果我在 DlgProc 中设置断点,那么它会收到以下消息:

48、85、297、273、273、144、2 和 130。

我已经查找了这些消息,它们会转换为:

WM_SETFONT
WM_NOTIFYFORMAT
???
WM_COMMAND
WM_COMMAND
???
WM_DESTROY
WM_NCDESTROY

有人知道我做错了什么吗?


编辑:
我找到了解决问题的方法!我们最初认为这是一个损坏的 RC 文件,但我意识到我制作的测试 RC 文件具有除 SysLink 之外的所有类型的控件。当我添加 SysLink 时,发生了同样的事情。我尝试在链接器中包含 ComCtl32.lib,还尝试包含公共控件标头并调用 InitCommonControls 但这没有任何作用。

我把所有东西都恢复到原来的样子,并添加了这个预处理器指令来修复它:

#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

这就是问题所在。如果没有该指令,由于系统链接,对话框将不会初始化!

I've created my dialog as a resource using the resource editor (dialog editor?)

It's nothing special, just two groupboxes, two buttons, a split button, two pictureboxes, 3 static labels, 3 edit boxes, 3 spin controls, 3 syslinks, and a progressbar.

When I press Ctrl+T to test the dialog, it appears to work fine, but when I press F5 to debug the program, the dialog never appears and the program exits with code -1 (0xffffffff)

Here is the code I'm using to call the dialog:

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

BOOL CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch (Msg)
    {
        case (WM_COMMAND):
            switch (LOWORD(wParam))
            {
                default:
                    break;
            }
            break;

        default:
            return FALSE;
    }

    return FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    int ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAINWINDOW), NULL, DlgProc);

    int err = GetLastErro(); // This gives 0

    return ret; // This gives -1
}

I don't have anything yet in the DlgProc yet because I just wanted the dialog to show to begin with.

If I set a breakpoint in DlgProc then these are the messages it receives:

48, 85, 297, 273, 273, 144, 2, and 130.

I've looked these up and they translate to:

WM_SETFONT
WM_NOTIFYFORMAT
???
WM_COMMAND
WM_COMMAND
???
WM_DESTROY
WM_NCDESTROY

Anybody know what I'm doing wrong?


Edit::
I found a solution to the problem! We originally thought it was a corrupted RC file but I realised the the test RC file I made has every type of control except a SysLink. When I added a SysLink the same thing happened. I tried including ComCtl32.lib in the linker, and I also tried including the common controls header and calling InitCommonControls but that did nothing.

I put everything back the way it originally was, and added this pre-processor directive which fixed it:

#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

That was the problem. Without that directive, the dialog won't initialise because of the SysLinks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老街孤人 2024-12-23 20:11:12

您不需要为对话框过程调用 DefWindowProc。它们是由操作系统为您完成的。只需返回FALSE;

You don't need to call DefWindowProc for Dialog procs. They are done for you by the OS. simply return FALSE;

-柠檬树下少年和吉他 2024-12-23 20:11:12

通用控件!例如,进度条 - 您需要与 commctrl dll 链接、包含 commctrl 标头并调用 InitCommonControlsEx。这会导致您的程序执行并立即终止。


从资源编辑器中测试对话框实际上并不执行任何其余代码 - 它只是绘制对话框,在适当的位置绘制子窗口,然后坐在那里盯着您看。你的 dlgproc 可能非常不正确,资源编辑器也不会抱怨。

默认情况下,您的对话框过程应该返回 0,而不是交给默认的窗口过程。

一旦开始滚动,您将使用如下方式处理对话框收到的(部分)消息:

 switch(msg) {
      case WM_COMMAND:{
           switch(LOWORD(wParam)) {
              // do stuff here when a child window is manipulated
      }
      break;
 }

Common controls! Progress bar, as an example - you'd need linkage with commctrl dll, inclusion of commctrl header, and call to InitCommonControlsEx. This is causing your program to execute and immediately terminate.


Testing your dialog from within the resource editor doesn't actually execute any of the rest of your code - it merely draws the dialog, draws the child windows in the appropriate places, and then sits there and stares at you. Your dlgproc can be terribly incorrect and the resource editor won't so much as whimper.

Your dialog procedure should return 0 by default, not hand off to the default window proc.

Once you're rolling, you would handle (some of) the messages your dialog receives with something like this:

 switch(msg) {
      case WM_COMMAND:{
           switch(LOWORD(wParam)) {
              // do stuff here when a child window is manipulated
      }
      break;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文