Jump to content
  • Advertisement
Sign in to follow this  

Win32 C++ Setting the window icon

This topic is 2523 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So i looked at my CreateWindow() function, and saw this line:

wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);

and started researching... because before i was using just opengl and glut... but now im using the Win32 API, so i need a way to use my resource file:

MyIcon ICON "nicon.ico"

with the LoadIcon() function. What do i need to do?

Share this post

Link to post
Share on other sites
MyIcon should just be a #define'd number. Do you have a resource.h file?

Here's a guide on how to load an icon from a resource file.

Share this post

Link to post
Share on other sites
nothing is working... DX

here are the files:


#include "source/Resources.h"
MyIcon ICON "icon.ico"


#define MyIcon 100001


#include "Functions.h"
#include <windows.h>
#include "App.h"
extern myApp APP;
#include "Resources.h"
void Create_Window()
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc; // Windows Class Structure
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=(long)APP.Screenw; // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=(long)APP.Screenh; // Set Bottom Value To Requested Height
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
wc.lpfnWndProc = (WNDPROC) MainWindowProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = APP.hInstance; // Set The Instance
wc.hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(MyIcon)); // Load The Default Icon
// wc.hIconSm = LoadIcon(APP.hInstance,MAKEINTRESOURCE(MyIcon));
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
wc.hbrBackground = NULL; // No Background Required For GL
wc.lpszMenuName = NULL; // We Don't Want A Menu
wc.lpszClassName = "Survivor"; // Set The Class Name
if (!RegisterClass(&wc)) // Attempt To Register The Window Class
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
// Create The Window
if (!(APP.hWnd=CreateWindowEx(dwExStyle, // Extended Style For The Window
"Survivor", // Class Name
"Survivor", // Window Title
dwStyle | // Defined Window Style
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN, // Required Window Style
CW_USEDEFAULT, 0, // Window Position
WindowRect.right-WindowRect.left, // Calculate Window Width
WindowRect.bottom-WindowRect.top, // Calculate Window Height
NULL, // No Parent Window
NULL, // No Menu
APP.hInstance, // Instance
NULL))) // Dont Pass Anything To WM_CREATE
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
if (!(APP.hDC=GetDC(APP.hWnd)))// Did We Get A Device Context?
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
if (!(PixelFormat=ChoosePixelFormat(APP.hDC,&pfd)))// Did Windows Find A Matching Pixel Format?
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
if(!SetPixelFormat(APP.hDC,PixelFormat,&pfd))// Are We Able To Set The Pixel Format?
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
if (!(APP.hRC=wglCreateContext(APP.hDC)))// Are We Able To Get A Rendering Context?
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
if(!wglMakeCurrent(APP.hDC,APP.hRC))// Try To Activate The Rendering Context
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
// APP.hWnd.hIcon=LoadIcon(APP.hInstance,"icon.ico");
// APP.hWnd.hIconsm=LoadIcon(APP.hInstance,"icon.ico");
ShowWindow(APP.hWnd,SW_SHOWMAXIMIZED); // Show The Window
SetForegroundWindow(APP.hWnd); // Slightly Higher Priority
SetFocus(APP.hWnd); // Sets Keyboard Focus To The Window

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!