我正在尝试在我的 WIN32 GUI 应用程序中嵌入一个图标,以用作使用 Visual Studio 的按钮的背景。我可以毫无问题地添加资源,并且可以在“资源 View ”选项卡中查看它。当我编译和运行我的程序时,没有出现任何错误,我可以正常运行 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 中,这样我就不必再四处发送图像了。
最佳答案
您没有设置hInstMain
。如果它为零,则 LoadImage 加载 OEM 图像。另外,为什么要在 WM_PAINT 上创建控件?
关于c++ - 使用资源时将错误的图标加载到 Visual Studio 项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55647654/