3
您好,我正在嘗試學習如何使用WIN32 API創建Windows應用程序。爲此,我正在通過關於這個主題的Forgers教程。我被困在嘗試讓我的第一個對話框顯示時,我的菜單項被點擊。我已經檢查了MSDN網站,並查看了我的代碼中的所有相關功能的文檔,並且所有內容都正確地顯示給我。但是,當然,我不知道,因爲我只是在學習,我想知道是否有人可以指出我的錯誤,或者可能指向我可能在文檔中缺少的方向。我敢肯定,這真的很愚蠢(因爲它總是與這些東西) 無論如何這裏是我的小腳本。希望你們中的任何人都能幫忙。感謝我的WIN32 API對話框dosent show - C++
#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
#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
P.S.頭我是任何論壇的第一次海報,所以我很抱歉,如果我沒有明確地獲得代碼,請讓我知道再次感謝!
發送空的,而不是HWND_DESKTOP看看會發生什麼。也嘗試使用SW_SHOW而不是nFunsterStil,看看會發生什麼。 –
非常感謝您的迴應!我一起嘗試了兩個更改,並且單擊按鈕時對話框仍然不顯示。爲了安全起見,我嘗試了對MessageBox的簡單調用以及對DialogBox的調用,並且它工作正常。 – user1390368
我對什麼不工作感到困惑。當我編譯並運行此代碼時,完全如圖所示,我在屏幕上看到一個標題爲「Windows App」的窗口和一個包含「文件」和「其他」菜單的菜單欄。當我點擊「File」菜單,然後選擇「Stuff」時,我會看到一個標題爲「About Box」的對話框,它有一個「ok」按鈕。當你嘗試運行它時有什麼不同? –