c++ - 我的 WIN32 API 对话框不显示 - C++

标签 c++ winapi dialog

您好,我正在尝试学习如何使用 WIN32 API 创建 Windows 应用程序。为此,我正在阅读有关该主题的 The Forgers 教程。单击我的菜单项时,我一直试图让我的第一个对话框显示出来。我查看了 MSDN 站点并查看了有关我的代码中所有相关功能的文档,一切对我来说都是正确的。但是,当然,我不知道,因为我只是在学习 我想知道是否有人可以指出我的错误,或者指出我在文档中可能遗漏的方向。我确定这真的很愚蠢(因为这些事情总是如此) 无论如何这是我的小脚本。希望你们中的任何人都能提供帮助。谢谢

#include <windows.h>
#include "resource.h"
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                HINSTANCE hPrevInstance,
                LPSTR lpszArgument,
                int nFunsterStil)

{
HWND hwnd;               /* This is the handle for our window */
MSG messages;            /* Here messages to the application are saved */
WNDCLASSEX wincl;        /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;                 /* No menu */
wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
wincl.cbWndExtra = 0;                      /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
    return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
       0,                   /* Extended possibilites for variation */
       szClassName,         /* Classname */
       "Windows App",       /* Title Text */
       WS_OVERLAPPEDWINDOW, /* default window */
       CW_USEDEFAULT,       /* Windows decides the position */
       CW_USEDEFAULT,       /* where the window ends up on the screen */
       544,                 /* The programs width */
       375,                 /* and height in pixels */
       HWND_DESKTOP,        /* The window is a child-window to desktop */
       NULL,                /* No menu */
       hThisInstance,       /* Program Instance handler */
       NULL                 /* No Window Creation data */
       );

/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
    /* Translate virtual-key messages into character messages */
    TranslateMessage(&messages);
    /* Send message to WindowProcedure */
    DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)                  /* handle the messages */
{
    case WM_CREATE:
         {
             HMENU  hMenu, hSubMenu, hOtherMenu, hOtherSubMenu;

             hMenu = CreateMenu();
             hSubMenu = CreatePopupMenu();
             hOtherMenu = CreateMenu();
             hOtherSubMenu = CreatePopupMenu();
             AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
             AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
             AppendMenu(hSubMenu, MF_STRING, ID_STUFF, "S&tuff");


             AppendMenu(hOtherSubMenu, MF_STRING,ID_OTHER_SUB, "O&ther");
             AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hOtherSubMenu, "&Other");


             SetMenu(hwnd, hMenu); 
             //SetMenu(hwnd, hOtherMenu);

             }
             break;

    case WM_LBUTTONDOWN:
         MessageBox(hwnd, "this is my program", "program box", 0);
         break;
    case WM_CLOSE:
         DestroyWindow(hwnd);
         break;
    case WM_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;
             case WM_COMMAND:
         switch(LOWORD(wParam))
         {
             case ID_STUFF:
                 DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
                 break;

             case ID_FILE_EXIT:
                  PostQuitMessage(0);
                  break;
         } 
    default:                      /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
 switch(message)
 {
     case WM_INITDIALOG:
              return TRUE;
     case WM_COMMAND:
              switch(LOWORD(wParam))
              {
                  case IDOK:
                   EndDialog(hwnd, IDOK);
break;
case IDCANCEL:
EndDialog(hwnd, IDCANCEL);
break;
}     
break;
default:
return FALSE; 
}
return TRUE;
}

这是.rc

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

    IDR_MYMENU MENU
    BEGIN
    POPUP "F&ile"
    BEGIN
       MENUITEM "E&xit", ID_FILE_EXIT
       MENUITEM "stuff I like", ID_STUFF
    END
END    


IDD_ABOUT DIALOG DISCARDABLE 0,0,239,66
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About Box"
FONT 8, "MS Sans Serif"
BEGIN
    DEFPUSHBUTTON "&ok", IDOK, 174,18,50,14
    PUSHBUTTON "&Cancel", IDCANCEL, 174,18,50,14
    GROUPBOX "About this program...",IDC_STATIC,7,7,225,52
    CTEXT "This is a Modular Database program", IDC_STATIC, 16,18,144,33
END

这是 .rc 的 header

#define IDR_MYMENU 101

#define ID_FILE_EXIT 4001
#define ID_STUFF 4002

#define IDC_STATIC -1
#define IDD_ABOUT 102
#define ID_OTHER_SUB 4003

附言我是第一次发帖到任何论坛,所以如果我没有清楚地输入代码,我深表歉意,请让我知道再次感谢!

最佳答案

我认为您没有将 resource.hrecource.rc 添加到您的项目中。

如果您不添加它们,您的项目将编译但不会显示对话框。

我编译了你的代码,它运行良好。

我使用 mingw4.7 和 devc++ IDE。

After add

Before add

关于c++ - 我的 WIN32 API 对话框不显示 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10559450/

相关文章:

c# - 使用已安装产品的升级代码获取产品版本

Android - 如何将列表框实现为模态对话框/弹出窗口

c++ - 整数环绕还没有真正理解

c++ - 如何在 C++ 中创建一个适用于 Windows 和 Linux 的文件夹(目录)

c++ - 在 C/C++ 中是否有类似极度优化的 memcpy2d 之类的东西?

jquery - JQuery UI 对话框中的日期选择器在打开的对话框中显示日历

android - 对话框的 onSaveInstanceState/onRestoreInstanceState

c++ - 为什么我的用于运行带障碍物传感器的蓝牙控制机器人的 Arduino 代码无法正常工作?

c# - 如何在 Windows 10(十月更新)中获取剪贴板历史记录?

C++ 奇怪的 c0000005 错误