CPP: Load Icons from dll and drawing them on Window

Loading icons and showing them on the screen is often required in applications. This concept is funny and interesting also.

Icons are loaded using LoadIcon function. LoadIcon function returns Handle to the icon. using this handle we can draw them on the required device context (eg: window). Here we will discuss about drawing standard icons and icons from dlls on the application window.

Standard Icons are those which are defined in windows header file.(eg: IDI_APPLICATION) and the icons which you have added into your project. We can draw them on the window very easily using LoadIcon() and DrawIcon() functions. The Example is given below:
<br /> // after creating window.. (Assuming window handle is: hWnd)<br /> {<br /> HDC reqDC = GetDC(hWnd); // to draw icons on a window.<br /> HICON reqICO = LoadIcon(NULL,IDI_APPLICATION); // Default Application Icon.<br /> DrawIcon(reqDC,10,10,reqICO);<br /> ReleaseDC(reqDC);<br /> }<br />
A very important thing should be noted here is when loading icons that are added to the project, you should use MAKEINTRESOURCE() macro. instead of using them directly like this:
<br /> LoadIcon(NULL,IDI_ICON1); // will cause error.<br />
In the previous code, i didn't used MAKEINTRESOURCE macro because IDI_APPLICATION is already defined as

<br /> #define IDI_APPLICATION MAKEINTRESOURCE(32512)<br />

When loading standard icons, first parameter(hInstance) can be NULL. In case of external files like dll or exe, it is the module handle of that file obtained from LoadLibrary() function.

To load icons from dll files, first load required dll, and then use MAKEINTRESOURCE macro as secound parameter of LoadIcon().

An example of loading all the icons from a dll and showing them in a window is given below:

<br /> #include <windows.h><br /> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);<br /> void UpdateWindow();<br /> HWND hWnd;<br /> #define PROG_NAME "ICON Loading Example"<br /> int _stdcall WinMain(HINSTANCE hInst, HINSTANCE hPInst, LPSTR lPCmdLine, int nCmdShow)<br /> {<br /> MSG Msg;<br /> WNDCLASSEX wc;<br /> wc.cbClsExtra = 0;<br /> wc.cbWndExtra = 0;<br /> wc.hbrBackground = (HBRUSH)16; <br /> wc.hCursor = LoadCursor(NULL,IDC_ARROW);<br /> wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);<br /> wc.hInstance = NULL;<br /> wc.lpfnWndProc = WndProc;<br /> wc.lpszClassName = "Class_Win";<br /> wc.lpszMenuName = NULL;<br /> wc.style = 0;<br /> wc.cbSize = sizeof(WNDCLASSEX);<br /> wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);<br /> if(!RegisterClassEx(&wc)) {return 0;}<br /> hWnd = CreateWindowEx(0,<br /> "Class_Win",<br /> PROG_NAME,<br /> WS_OVERLAPPEDWINDOW,<br /> 0,0,<br /> 1300,<br /> 700,<br /> HWND_DESKTOP,<br /> NULL,<br /> NULL,<br /> NULL);<br /> <br /> if (!hWnd) {return 0;}<br /> ShowWindow(hWnd,nCmdShow);<br /> <br /> while(GetMessage(&Msg,NULL,0,0))<br /> {<br /> TranslateMessage(&Msg);<br /> DispatchMessage(&Msg);<br /> }<br /> return Msg.wParam;<br /> }<br /> <br /> LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg,WPARAM wParam, LPARAM lParam)<br /> {<br /> switch(Msg)<br /> {<br /> case WM_CLOSE:<br /> PostQuitMessage(0);<br /> break;<br /> case WM_PAINT:<br /> UpdateWindow();<br /> default:<br /> return DefWindowProc(hwnd,Msg,wParam,lParam);<br /> }<br /> }<br /> <br /> void UpdateWindow()<br /> {<br /> HDC dc = GetDC(hWnd);<br /> HMODULE sh32 = LoadLibrary("shell32.dll");<br /> HICON ic;<br /> <br /> int c=1;<br /> for (int x=10; x<550; x +=35) { for (int y=10; y<1250; y=y+35) { ic = LoadIcon(sh32,MAKEINTRESOURCE(c)); DrawIcon(dc,y,x,ic); c++; } } ReleaseDC(hWnd,dc); FreeLibrary(sh32); }
cpp icons, cpp gdi, cpp win32 gdi,load icons,gui

No comments:

Post a Comment

Thank you for commenting. Please keep visiting.