gpt4 book ai didi

c - 未创建对话框

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:19 25 4
gpt4 key购买 nike

在一个简单的 WINAPI 程序中,我制作了一个对话框(我用 ResEdit 制作的)。

但是,对话框未显示,DialogBox() 调用返回 -1。 GetLastError() 返回 1812(指定的图像文件不包含资源部分。)

主要.cpp

/*default stuff, like including windows.h, etc... */
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
hInst = hThisInstance;
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 */

/*default stuff, initializing fields of WNDCLASSEX struct */

hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Hello World"), /* 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 */
0, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Creating controls ... */

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

/* Default : message loop. It will run until GetMessage() returns 0 */

/* 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;
HICON hIcon, hIconSm;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();

AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT) hSubMenu, "&File");

hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_HELP_ABT, "&About");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT) hSubMenu, "&Help");

SetMenu(hwnd, hMenu);

hIcon = (HICON) LoadImage(NULL, "iconBIG.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
if (hIcon)
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hIcon);
hIconSm = (HICON) LoadImage(NULL, "iconSMALL.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
if (hIconSm)
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);

break;
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_ACCEPTBTN:
/* handling the controls */
break;
case ID_FILE_EXIT:
/* handling more controls ... */
PostQuitMessage(0);
break;
case ID_HELP_ABT:
int ret = DialogBox(GetModuleHandle, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, myDialog);
/*PROBLEM: DialogBox returns -1*/

if (ret == -1) {
DWORD dw = GetLastError(); // error 1812
char buffer[70];
sprintf(buffer, "Failed with %d", dw);


MessageBox(hwnd, buffer, "", MB_OK);
}
break;
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
BOOL CALLBACK myDialog(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
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;
}
}

资源对话框.rc

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

LANGUAGE 0, SUBLANG_NEUTRAL
IDD_DIALOG1 DIALOG 0, 0, 186, 95
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg"
{
GROUPBOX "Static", 0, 3, 7, 118, 82, 0, WS_EX_LEFT
LTEXT "hello,\nworld!", 0, 16, 21, 20, 17, SS_LEFT, WS_EX_LEFT
PUSHBUTTON "Cancel", IDCANCEL, 129, 24, 50, 14, 0, WS_EX_LEFT
DEFPUSHBUTTON "OK", IDOK, 129, 7, 50, 14, 0, WS_EX_LEFT
}

资源.h

#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define IDD_DIALOG1 100
/* Dont redefine IDOK and IDCANCEL */
/*#define IDOK 40000
#define IDCANCEL 40001*/

为什么对话框不显示?我正在遵循 winprog.org 的教程并按照他们说的去做。

想法:代码块编译器:GCC

最佳答案

GetModuleHandle是一个函数。您不能将函数名称传递给 HINSTANCE DialogBox() 的参数并期望它起作用。事实上,我什至对它编译感到惊讶!为当前可执行文件调用它的正确方法是 GetModuleHandle(NULL) .

但是有一个更好的方法:第一个参数给WinMain()也是HINSTANCE你的可执行文件。只需将其存储在全局变量中并直接使用即可。


在包含 <windows.h> 之前添加以下行应该有助于捕获类似这样的 future 错误:

#define STRICT

关于c - 未创建对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29111502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com