Author Topic: Changing executable's icon  (Read 31449 times)

Offline Maciek

  • Multiple posting newcomer
  • *
  • Posts: 16
Changing executable's icon
« on: September 30, 2005, 12:15:50 am »
The title pretty much describes what I'd like to know. How do I change the icon of my .exe file? It must be something I'm just missing, but I can't find a way to do this.

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
Re: Changing executable's icon
« Reply #1 on: September 30, 2005, 12:20:05 am »
You need to add an icon as a resource, and on startup, set the icon using the appropriate function. Check the code::Blocks source to see how we do this for codeblocks.exe

Offline Maciek

  • Multiple posting newcomer
  • *
  • Posts: 16
Re: Changing executable's icon
« Reply #2 on: September 30, 2005, 12:38:15 am »
I was thinking about something that is in project options in dev-cpp. I want to change the way the file looks in the explorer before running it.

Offline Urxae

  • Regular
  • ***
  • Posts: 376
Re: Changing executable's icon
« Reply #3 on: September 30, 2005, 12:44:40 am »
The first icon in your resource file will be used as the icon of the executable. You'll have to create your resource file manually though, AFAIK Code::Blocks does not have a dialog for it like Dev-cpp. It's not extremely hard, just put something like this in it:
Code
MY_ICON ICON "my_icon.ico"
The first word is the name of the icon (not important unless you want to refer to it in your code), the second word indicates it's an icon and then comes the name of the file to use.

Offline Maciek

  • Multiple posting newcomer
  • *
  • Posts: 16
Re: Changing executable's icon
« Reply #4 on: September 30, 2005, 12:46:05 am »
OK, thanks a lot.
EDIT: Yep, already done it. Thanks again for your quick answer.
« Last Edit: September 30, 2005, 12:55:25 am by Maciek »

Offline Vampyre_Dark

  • Regular
  • ***
  • Posts: 255
  • Hello!
    • Somewhere Over The Rainbow...
Re: Changing executable's icon
« Reply #5 on: October 07, 2005, 08:02:26 am »
I discovered this on my own today, and got my icon to work in my executable. But doesn't anyone know how to get the icon to go into the title bar? After the window has been created? I use a small framework to open my game / graphics windows and I'd like to just make some kind of call to switch the existing default icon?
C::B Wishlist
~BOYCOTT THE EVIL YELLOW BOXES~

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Re: Changing executable's icon
« Reply #6 on: October 07, 2005, 11:19:27 am »
if you use wxWidgets, its as easy as calling SetIcon() in the frame-constructor.

Code
// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

// frame constructor
MyFrame::MyFrame(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title)
{
    // set the frame icon
    SetIcon(wxICON(sample));

    ...

}

if you are using plain win32 API, you have to give it as parameter to your WindowsClass structure
see IDR_ICO_MAIN resource ID application-icon, defined in the resource.h file.
Code
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,
LPSTR lpcmdline,int ncmdshow)
{
HWND    hwnd;
MSG    msg;

WNDCLASSEX winclass;

winclass.cbSize         = sizeof(WNDCLASSEX);
winclass.style       = CS_DBLCLKS;
winclass.lpfnWndProc    = MainWindowProc;
winclass.cbClsExtra   = 0;
winclass.cbWndExtra   = 0;
winclass.hInstance   = hinstance;
winclass.hIcon       = LoadIcon(hinstance, MAKEINTRESOURCE(IDR_ICO_MAIN));
winclass.hCursor     = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
winclass.lpszMenuName   = NULL;
winclass.lpszClassName = WIN_CLASS_NAME;
winclass.hIconSm        = NULL;

hinstance_main = hinstance;

if (!RegisterClassEx(&winclass))
return(0);

if (!(hwnd = CreateWindowEx(0,WIN_CLASS_NAME,
"Tutorial Window",
WS_TILEDWINDOW | WS_MAXIMIZEBOX | WS_SIZEBOX ,
0 ,0 ,200,500,NULL,NULL,hinstance,NULL)))
return(0);

InitCommonControls();

ShowWindow(hwnd, SW_SHOW);

main_window_handle = hwnd;

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

return(msg.wParam);
}



And you put your icon in the resource.rc file
Code
// resource.rc
IDR_ICO_MAIN ICON "main.ico"

that's it
« Last Edit: October 07, 2005, 11:20:58 am by tiwag »

Offline Vampyre_Dark

  • Regular
  • ***
  • Posts: 255
  • Hello!
    • Somewhere Over The Rainbow...
Re: Changing executable's icon
« Reply #7 on: October 08, 2005, 08:23:21 am »
Hmmm. Isn't WXWidgets just a wrapper around win32 on windows... which would mean you've loaded the icon after window creation in that snippet. I wonder what the win32 method is. I've been looking.

-edit-

Okay, I've found the function to do it. But I'm not sure how to define my icon in the header file?

I have this in the RC file:
IDR_ICO_MAIN ICON "c:\code\somefolder\someicon.ico"

How do I define that in the .h file? Because I can't compile it otherwise.
« Last Edit: October 08, 2005, 08:39:57 am by Vampyre_Dark »
C::B Wishlist
~BOYCOTT THE EVIL YELLOW BOXES~

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Re: Changing executable's icon
« Reply #8 on: October 08, 2005, 10:06:02 am »
Quote from: Vampyre_Dark
But I'm not sure how to define my icon in the header file?

I have this in the RC file:
IDR_ICO_MAIN ICON "c:\code\somefolder\someicon.ico"

How do I define that in the .h file? Because I can't compile it otherwise.

the identifier is just a unique number in your program
Code
#define IDR_ICO_MAIN 1234


please work through some tutorials regarding windows programming...
start here
http://www.winprog.org/tutorial/

Offline Vampyre_Dark

  • Regular
  • ***
  • Posts: 255
  • Hello!
    • Somewhere Over The Rainbow...
Re: Changing executable's icon
« Reply #9 on: October 08, 2005, 10:41:05 am »
 :lol: I don't need a tutorial. I can code windows fine. I've been making win32 api programs for years now. I was just used to having resources defined for me by an editor. I did not know if the values meant anything or not. I already found that out and got it compiling a awhile ago, but forgot to repost. Wasn't easy to track that down on MSDN, it's like a maze sometimes.

Does the icon for the titlebar have format requirements? Because the icon I've been compiling in for 2 days shows up with the .exe in explorer and becomes part of the .exe but doesn't show up when I send the message to change it.

This does nothing... :oops:

//change the icon
SetClassLong(GetHWND(),GCL_HICON,(LONG)  LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_ICO_MAIN)));
C::B Wishlist
~BOYCOTT THE EVIL YELLOW BOXES~

Offline Panarchy

  • Single posting newcomer
  • *
  • Posts: 4
Re: Changing executable's icon
« Reply #10 on: April 22, 2009, 11:51:10 pm »
The first icon in your resource file will be used as the icon of the executable. You'll have to create your resource file manually though, AFAIK Code::Blocks does not have a dialog for it like Dev-cpp. It's not extremely hard, just put something like this in it:
Code
MY_ICON ICON "my_icon.ico"
The first word is the name of the icon (not important unless you want to refer to it in your code), the second word indicates it's an icon and then comes the name of the file to use.

 8)

That worked for me!

SWEET - My project has now been perfected!