我试图在我的WIN32 GUI应用程序中嵌入一个图标,以用作使用Visual Studio的按钮的背景。我能够毫无问题地添加资源,并且可以在“资源视图”选项卡中进行查看。编译并运行程序时,没有错误,并且可以正常运行EXE,但是按钮上方显示的图标似乎是Windows的默认图标之一。
当我将其添加到Visual Studio时,图标的默认标识符为102。我尝试将其更改为其他数字,但最终仍然在按钮上显示错误的图标。奇怪的是,使用对图标资源的相同引用,我可以将应用程序图标设置得很好
从“ WinUser.h”文件中:(这些是我的评论):
#define IDI_APPLICATION MAKEINTRESOURCE(32512) // Shows when my iconid=100
#define IDI_HAND MAKEINTRESOURCE(32513) // " " = 101
#define IDI_QUESTION MAKEINTRESOURCE(32514) // " " = 102
#define IDI_EXCLAMATION MAKEINTRESOURCE(32515) // " " = 103
#define IDI_ASTERISK MAKEINTRESOURCE(32516) // " " = 104
#define IDI_WINLOGO MAKEINTRESOURCE(32517) // " " = 105
#define IDI_SHIELD MAKEINTRESOURCE(32518) // " " = 106
如果将标识符更改为超出此范围,则LoadImage函数将失败,并且不会加载任何图标。我无法解释这一点,也不知道如何进行。
我的resource.h文件:
#define IDI_ICON1 102
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 103
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
我的resource.rc文件(由Visual Studio生成。我删除了注释):
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
#include "winres.h"
#undef APSTUDIO_READONLY_SYMBOLS
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#ifdef APSTUDIO_INVOKED
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
IDI_ICON1 ICON "angery.ico"
#endif
我的源文件:
// main.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c
#include "resource.h"
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <CommCtrl.h>
#include <ShlObj.h>
#include <ShlObj_core.h>
#include <stdio.h>
HINSTANCE hInstMain;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
// Define and set parameters for the windows class attribute
WNDCLASSEX wcex;
HWND hWnd;
MSG msg;
hInstMain = hInstance; // Adding this line solved my problem.
// ZeroMemory(&wcex, sizeof(WNDCLASSEX));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstMain;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = _T("Dummy");
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_ICON1));
if (!RegisterClassEx(&wcex)) {
MessageBox(NULL, _T("Failed"), _T("Failed"), MB_OK);
return -1;
}
hWnd = CreateWindow(_T("Dummy"), _T("Example"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
100, 100, NULL, NULL, hInstMain, NULL);
if (!hWnd) {
MessageBox(NULL, _T("AHH"), _T("AHH"), MB_OK);
return -1;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
HDC hdc;
HWND myButton;
HICON myIcon;
switch (message) {
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_CREATE:
myButton = CreateWindow(_T("Button"), NULL, WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | WS_TABSTOP | BS_ICON,
10, 10, 50, 50, hWnd, NULL, NULL, NULL);
//myIcon = (HICON)LoadImage(hInstMain, _T("angery.ico"), IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
myIcon = (HICON)LoadImage(hInstMain, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
if (!myIcon) {
MessageBox(NULL, _T("Icon fail"), _T("Icon fail"), MB_OK);
return -1;
}
SendMessage(myButton, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)myIcon);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
为您提供方便的图标(现在描述我对VS的心情)。 https://drive.google.com/file/d/1HoPInZukIvAVkWs38l3MnfUkQ7HpLktR/view?usp=sharing 我不认为该图标已损坏,因为我尝试过使用来自不同来源的多个图标。
我希望能够将自己的图标嵌入到exe文件中时将其加载到按钮中,因此我也不必发送图像。
答案 0 :(得分:2)
您未设置hInstMain
。如果为零,则LoadImage加载OEM映像。另外,为什么要在WM_PAINT上创建控件?