此问题与Using an icon on a dialog box window C++ Win32 API不同,我正在尝试将自定义图标添加到对话框的客户区中。当我运行代码时,我得到一个默认的Windows图标,而不是我想要的自定义图标。如果我将IDI_ICON1
的值更改为80到120之间的任何数字,我将获得不同的Windows图标;我尝试过更高的数字但是什么都没有出现。我看到cplusplus.com的某个人遇到了同样的错误,我做了同样的事情,但没有什么区别。
Microsoft's有关图标的参考。
Source.cpp
#pragma comment(lib,"ole32.lib")
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
_TCHAR szClassName[] = "StackOverflow";
HWND hwnd;
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK DialogProc(HWND handle, UINT message, WPARAM wparam, LPARAM lparam);
int _stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR CmdLine, int nShowCmd)
{
MSG messages;
WNDCLASSEX wincl;
HWND about;
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.style = CS_DBLCLKS;
wincl.lpfnWndProc = WindowProcedure;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hInstance = hInstance;
wincl.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.hbrBackground = (HBRUSH)COLOR_WINDOW;//COLOR_BACKGROUND;
wincl.lpszMenuName = NULL;
wincl.lpszClassName = szClassName;
wincl.hIconSm = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
if(!RegisterClassEx(&wincl))
return 0;
hwnd = CreateWindowEx(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_WINDOWEDGE, //dwExStyle
szClassName, //ClassName
"StackOverflow", //WindowName
WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_EX_PALETTEWINDOW, //Window Style
CW_USEDEFAULT, //Horizontal Pos
CW_USEDEFAULT, //Verticals Pos
100, //Width
100, //Height
HWND_DESKTOP, //Parent
NULL, //Menu
hInstance, //Module Assoc
NULL); //CreateStruct
about = CreateDialog( NULL,
MAKEINTRESOURCE(IDD_DIALOG),
hwnd,
DialogProc);
while (GetMessage(&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
INT_PTR CALLBACK DialogProc(HWND handle, UINT message, WPARAM wparam, LPARAM lparam)
{
switch (message)
{
case WM_INITDIALOG:
//This changes the Icon in the TitleBar
SendMessage(handle, WM_SETICON, ICON_SMALL,
(LPARAM)LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1)));
return TRUE;
case WM_CLOSE:
DestroyWindow(handle);
return TRUE;
case WM_DESTROY:
PostQuitMessage(0);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wparam))
{
case IDCOK:
//TODO
case IDCANCEL:
//EndDialog(handle,wparam);
DestroyWindow(handle);
return TRUE;
default:
return FALSE;
}
default:
return FALSE;
}
return FALSE;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, message, wParam, lParam);
}
StackOverflow.rc
#include "resource.h"
IDD_DIALOG DIALOGEX 0, 0, 245, 57
STYLE DS_SETFONT | DS_MODALFRAME | DS_SETFOREGROUND | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "About Dialog"
FONT 10, "MS Shell Dlg", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,166,7,50,14
PUSHBUTTON "Cancel",2,166,30,50,14
LTEXT "The Icon here needs to match the one in the Titlebar",IDC_STATIC,50,15,107,28
ICON IDI_ICON1,103,19,16,20,20, SS_ICON
END
// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDI_ICON1 ICON "C:\\Users\\pixel\\Desktop\\MEIN.ico"
RESOURCE.H
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by StackOverflow.rc
//
#define IDD_DIALOG 101
#define IDI_ICON1 103
#define IDCOK 105
#define IDCANCEL 106
答案 0 :(得分:4)
问题是您对CreateDialog
的调用没有提供模块句柄,这会阻止图标控件在程序的资源中找到指定的图标。
显然在这种情况下,它默认查看系统图像列表,这就是您看到系统图标出现的原因(并在您更改ID时更改)。
将您的电话改为:
about = CreateDialog(hInstance,
MAKEINTRESOURCE(IDD_DIALOG),
hwnd,
DialogProc);