Changing the WinAPI icon

get error 1073741819 in Dev-C++ when running

Page 1 of 1

4 Replies - 6815 Views - Last Post: 09 October 2008 - 12:57 PM Rate Topic: -----

#1 mcamardo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 29-June 08

Changing the WinAPI icon

Posted 08 October 2008 - 06:20 PM

I am new to C++. I am trying to change the icon for the app window but get error 1073741819 in Dev-C++. Here is my code..

ICON_MAIN.rc
#include "resource.h"

ICON ICON_MAIN "myicon.ico"



main.cpp
#include <windows.h>
#define ICON_MAIN 1

const char g_szClassName[] = myWindowClass";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch(msg)
   {
	  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(hInstance, MAKEINTRESOURCE(ICON_MAIN));
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  wc.lpszMenuName = NULL;
  wc.lpszClassName = g_szClassName;
  wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(ICON_MAIN));

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

  hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "My App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 250, 325, NULL, NULL, hInstance, NULL);
  if(hwnd == NULL)
  {
	MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
	return 0;
  }

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

  while(GetMessage(&Msg, NULL, 0, 0) > 0)
  {
	TranslocateMessage(&Msg);
	DispatchMessage(&Msg);
  }
  return Msg.wParam;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Changing the WinAPI icon

#2 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5165
  • View blog
  • Posts: 26,840
  • Joined: 10-May 07

Re: Changing the WinAPI icon

Posted 08 October 2008 - 06:56 PM

I don't think you need MAKEINTRESOURCE, just the following should be fine :

wc.hIcon = LoadIcon(hInstance, ICON_MAIN);



... of course, you'll need to include your header file, where it is defined. You have defined it in the rc, define it in a header file.
Was This Post Helpful? 0
  • +
  • -

#3 mcamardo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 29-June 08

Re: Changing the WinAPI icon

Posted 09 October 2008 - 11:25 AM

Quote

... of course, you'll need to include your header file, where it is defined. You have defined it in the rc, define it in a header file.


This is the real question.. I am still new to C++. I have added the resource.h file to my project and have added this code.

#include <ICON_MAIN.rc>



I now get errors like:

[Warning]In function 'int WinMain(HINSTANCE_*, HINSTANCE_*, CHAR*, int)':

40 invalid conversation from `int to' `const CHAR*'

45 invalid conversation from `int to' `const CHAR'

[Build Error] [main.0] Error 1

Also it keeps adding this file MyApp_private.rc

This post has been edited by mcamardo: 09 October 2008 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5165
  • View blog
  • Posts: 26,840
  • Joined: 10-May 07

Re: Changing the WinAPI icon

Posted 09 October 2008 - 12:25 PM

You can't #include rc files.

Just put this line into your main.cpp

#define ICON_MAIN 1
ICON ICON_MAIN "myicon.ico"


Was This Post Helpful? 0
  • +
  • -

#5 mcamardo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 29-June 08

Re: Changing the WinAPI icon

Posted 09 October 2008 - 12:57 PM

View Postno2pencil, on 9 Oct, 2008 - 12:25 PM, said:

You can't #include rc files.

Just put this line into your main.cpp

#define ICON_MAIN 1
ICON ICON_MAIN "myicon.ico"


so as per your message I could do this:


#include <windows.h>
#define ICON_MAIN 1

ICON ICON_MAIN "MyIcon.ico"

const char g_szClassName[] = myWindowClass";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch(msg)
   {
	  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(hInstance, MAKEINTRESOURCE(ICON_MAIN));
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  wc.lpszMenuName = NULL;
  wc.lpszClassName = g_szClassName;
  wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(ICON_MAIN));

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

  hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "My App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 250, 325, NULL, NULL, hInstance, NULL);
  if(hwnd == NULL)
  {
	MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
	return 0;
  }

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

  while(GetMessage(&Msg, NULL, 0, 0) > 0)
  {
	TranslocateMessage(&Msg);
	DispatchMessage(&Msg);
  }
  return Msg.wParam;
}



and it should work? I did that and it gave me allkinds of errors. Do I need to keep th resource file and if so what should I do with the header file? I hate to ask all these questions but this is the way I learn.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1