好的,我有一个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;
}
答案 0 :(得分:1)
我认为您需要在.cpp文件中包含resource.h
,因为它定义了您的资源标识符。