News:

As usual while waiting for the next release - don't forget to check the nightly builds in the forum.

Main Menu

Changing executable's icon

Started by Maciek, September 30, 2005, 12:15:50 AM

Previous topic - Next topic

Maciek

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.

rickg22

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

Maciek

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.

Urxae

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:
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.

Maciek

#4
OK, thanks a lot.
EDIT: Yep, already done it. Thanks again for your quick answer.

Vampyre_Dark

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~

tiwag

#6
if you use wxWidgets, its as easy as calling SetIcon() in the frame-constructor.

// ----------------------------------------------------------------------------
// 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.
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
// resource.rc
IDR_ICO_MAIN ICON "main.ico"


that's it

Vampyre_Dark

#7
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.
C::B Wishlist
~BOYCOTT THE EVIL YELLOW BOXES~

tiwag

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
#define IDR_ICO_MAIN 1234


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

Vampyre_Dark

 :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~

Panarchy

Quote from: Urxae 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:
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!