Icon im Fenster
-
ich habe eine Icon Datei (MyIcon.ico) im gleichen verzeichnis liegen wie die EXE - nun versuche ich das Icon meinem Fenster zuzuweisen:
winclass.hIcon = (HICON)LoadImage(hInstance,TEXT("MyIcon.ico"),IMAGE_ICON, 32, 32, LR_DEFAULTSIZE);funzt aber nicht. Was mache ich falsch bzw. hat jemand einen Beispielcode rumliegen...
-
Hallo Vertexwahn,
arbeitest Du mit VS? Dann gibt es abhängig davon, wie Du Dein Icon im Resourcen Editor eingebunden hast mehrere Möglichkeiten.
a) Wenn Du ihm eine Nummer gegeben hast
winclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));oder
b) wenn Du ihm einen Namen gegeben hast
winclass.hIcon = LoadIcon(hInstance, TEXT("zugeweisenerName"));Viele Grüße
KnechtPs.: Beim einbinden der Icons weicht das Forgers-Tutorial von der im Petzold vorgeschlagenen Methode ab!!!
-
arbeitest Du mit VS?
ja - aber ich möchte das icon aus einer Datei laden und nicht mit resourcen arbeiten
-
Einfach LoadIcon als zweiten Parameter den Pfad der Icon-Datei angeben.
LoadIcon(NULL,"icon.ico"); // Ohne Pfadangabe wird das Icon im Verzeichnis der Exe Datei verwendet
-
funzt irgendwie nicht hier der Code - die EXE und die ICO sind im gleichen Ordner
#define WIN32_LEAN_AND_MEAN // instructs the compiler to not include extraneous MFC overhead #include <windows.h> #include <commctrl.h> LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch(msg) { case WM_DESTROY: PostQuitMessage(0); return 0; default: break; } return (DefWindowProc(hwnd, msg, wparam, lparam)); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdline, int nCmdShow) { InitCommonControls(); WNDCLASSEX winclass; winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; // extra class info space winclass.cbWndExtra = 0; // extra window info space winclass.hInstance = hInstance; // assign the application instance winclass.hIcon = (HICON)LoadImage(hInstance,TEXT("MyIcon.ico"),IMAGE_ICON, 16, 16, LR_DEFAULTSIZE|LR_LOADFROMFILE); int error = GetLastError(); winclass.hCursor = LoadCursor(hInstance, IDC_ARROW); winclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);//(HBRUSH)GetStockObject(WHITE_BRUSH); winclass.lpszMenuName = NULL; // the name of the menu to attach winclass.lpszClassName = TEXT("WINCLASS1"); // the name of the class itself winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&winclass); HWND hwnd; hwnd = CreateWindowEx(NULL, TEXT("WINCLASS1"), TEXT("Fenstertitel"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 400, 400, NULL, // handle to parent NULL, // handle to menu hInstance, // instance of this application NULL); if(hwnd==NULL) return -10; //UpdateWindow(); MSG msg; while(GetMessage(&msg, NULL, 0, 0)) { // translate any accelerator keys TranslateMessage(&msg); // send the message to the window proc DispatchMessage(&msg); } return 0; }
-
Naja in deinem Code verwendest du ja immer noch LoadImage und nicht LoadIcon
außerdem solltest genauer angeben was nicht funktioniert weil unter "funzt irgendwie nicht" kann man viel verstehen.wndClass.hIcon = LoadIcon(NULL,"icon1.ico");
-
Kuldren schrieb:
Naja in deinem Code verwendest du ja immer noch LoadImage und nicht LoadIcon
außerdem solltest genauer angeben was nicht funktioniert weil unter "funzt irgendwie nicht" kann man viel verstehen.[cpp]
#define WIN32_LEAN_AND_MEAN // instructs the compiler to not include extraneous MFC overhead
#include <windows.h>
#include <commctrl.h>LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;default: break;
}
return (DefWindowProc(hwnd, msg, wparam, lparam));
}int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdline,
int nCmdShow)
{
InitCommonControls();WNDCLASSEX winclass;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0; // extra class info space
winclass.cbWndExtra = 0; // extra window info space
winclass.hInstance = hInstance; // assign the application instance
winclass.hIcon = LoadIcon(NULL,TEXT("MyIcon.ico"));int error = GetLastError(); // <- error == 0 ! aber kein Icon Sichtbar
winclass.hCursor = LoadCursor(hInstance, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);//(HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = NULL; // the name of the menu to attach
winclass.lpszClassName = TEXT("WINCLASS1"); // the name of the class itself
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&winclass);
HWND hwnd;
hwnd = CreateWindowEx(NULL,
TEXT("WINCLASS1"),
TEXT("Fenstertitel"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,
0,
400,
400,
NULL, // handle to parent
NULL, // handle to menu
hInstance, // instance of this application
NULL);
if(hwnd==NULL)
return -10;//UpdateWindow();
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);
}
return 0;
}
[/cpp]