好的,所以我有一个应该有自定义图标的 C++ Win32 程序。我已经创建了一个 resource.h 文件和一个 resource.rc 文件(我已经将它们编译成一个 resource.o 文件)。现在我需要编译我的 .cpp 文件并将我的源对象文件和资源对象文件链接到最终的可执行文件中。但是我的 cpp 看不到资源文件中的资源。我的意思是在编译阶段(g++ -c mine.cpp)它不知道我的自定义图标是什么。
这是我的 resource.h 文件:
#define IDR_MYMENU 101
#define IDI_MYICON 201
#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
这是我的 resource.rc 文件:
#include "a.h"
IDR_MYMENU MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", ID_FILE_EXIT
END
POPUP "&Stuff"
BEGIN
MENUITEM "&Go", ID_STUFF_GO
MENUITEM "G&o somewhere else", 0, GRAYED
END
END
IDI_STAR ICON "star.ico"
最后是 .cpp 文件:
#include <windows.h>
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_LBUTTONDOWN:
MessageBox(NULL, "You pressed the left mouse button! :O", "MousePress", MB_OK | MB_ICONINFORMATION);
break;
case WM_RBUTTONDOWN:
MessageBox(NULL, "You pressed the right mouse button! :O", "MousePress", MB_OK | MB_ICONINFORMATION);
break;
case WM_MBUTTONDOWN:
{
char fileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, fileName, MAX_PATH);
MessageBox(hwnd, fileName, "The name of the program is:", MB_OK | MB_ICONINFORMATION);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_STAR));
wc.hCursor = LoadCursor(NULL, IDC_CROSS);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
wc.lpszClassName = g_szClassName;
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_STAR), IMAGE_ICON, 16, 16, 0);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, "Registration Failed!", NULL, MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_DLGMODALFRAME,
g_szClassName,
"This is a window!",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 450,
NULL, NULL, hInstance, NULL
);
if (hwnd == NULL) {
MessageBox(NULL, "HWND failed!", NULL, MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}