User forums > General (but related to Code::Blocks)
Changing executable's icon
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?
tiwag:
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));
...
}
--- End code ---
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);
}
--- End code ---
And you put your icon in the resource.rc file
--- Code: ---// resource.rc
IDR_ICO_MAIN ICON "main.ico"
--- End code ---
that's it
Vampyre_Dark:
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.
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.
--- End quote ---
the identifier is just a unique number in your program
--- Code: ---#define IDR_ICO_MAIN 1234
--- End code ---
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)));
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version