Sign in to follow this  
Padraic Hickey

Resources in Visual C++ Express

Recommended Posts

Padraic Hickey    122
Hello everyone, I am trying to add a resource in Visual Studio C++ Express and I am getting some strange errors. I have a resource.h file where I declare my resources. --------------------------------- #define MYICON 101 --------------------------------- And the the Resource.rc file --------------------------------- #include "resource.h" IDI_MYICON ICON "my_icon.ico" --------------------------------- This is where i call the Icon --------------------------------- HICON hMyIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON)); --------------------------------- When I compile this the compiler thinks that IDI_MYICON is an integer. It does not seem to be compiling the resource file. I then compiled the resource file with borland and included the RES file in the solution. Again I got the same problem. Do any settings have to me set in visual studio express so that it will compile resource files?, or are there any other settings which need to be set. Thanks for any help. padraic

Share this post


Link to post
Share on other sites
jouley    805
I'm not sure about Express, but this is how .NET 03 works: #define has to define the same name as your resource, in this case "IDI_MYICON". Try adding that into your define and seeing what happens.

Best of luck!
-jouley

Share this post


Link to post
Share on other sites
MrAccident    232
Shouldn't that first part (in resource.h) be like the following instead?
---------------------------------
#define IDI_MYICON 101
---------------------------------
Other than that, just make sure the .rc file is included in your project, and not excluded from your current build configuration (it should be included by default).

EDIT: yeah, what jouley said. [smile]

Share this post


Link to post
Share on other sites
jouley    805
After double-checking what MrAccident pointed out, can you post the specific error messages you're getting and any other relevant code (what else is in your resource file and header)? That'll help a lot in pointing you in the right direction. You also might try adding the resource via Visual Studio's resource adding capabilities, rather than writing the resource file yourself. Visual Studio is picky about the format, so it might like it better if you just let it do it.

Also, one small note -- it's a good thing the compiler thinks "IDI_MYICON" is an integer, because it is =0! The macro MAKEINTRESOURCE converts that integer value (that you've defined as something else) into the resource's string value ("my_icon.ico", in this case) so it can be loaded. Read more here.

Hope this helps!
-jouley

Share this post


Link to post
Share on other sites
Padraic Hickey    122
One small mistake and you get branded mrAccitent, i mean accident.

Here is the exact error:
error C2664: 'LoadIconA' : cannot convert parameter 2 from 'int' to 'LPCSTR'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

This error comes from the LoadIcon line.
Here is the main code.

const char g_szClassName[] = "myWindowClass";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(NULL, szFileName, "This programme 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 lpszCmdLine, 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(NULL, IDI_MYICON);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_MYICON);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed", "Problem", MB_OK);
return 0;
}

hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "Snowball",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
240, 120, NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed", "Problem", MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

return Msg.wParam;
};


Share this post


Link to post
Share on other sites
Padraic Hickey    122
Thanks very much Colin.
It is taking me a while to get familiar with win32 programming.
The program is compiling now but the icon now has what looks like a road works sign in its place. A yellow triangle with an exclamation mark through it.

Whats the best way to include resources.
I also noticed that my solution folder contains a debug folder. Where should I store the image.

As you can tell I no nothing about resources.

Share this post


Link to post
Share on other sites
Padraic Hickey    122
Well I solved the problem.
The debug folder contains your executables. The Icon was attached to the exe here when I checked. The icon in the top left of the window must be 16 X 16 and resources can only be 32 X 32. Therefore you must do some work on your resource to change its size :

wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this