No icon in title bar after switching from WNDCLASS to WNDCLASSEX - c++

I just switched the game to use WNDCLASSEX so I can change the hIconSm, but for some reason there isn't an icon in the title bar anymore. The cursor and icon on the task bar and the icon for the EXE are all working fine though. Here is the main part of the window creation code:
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WinProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_ERROR);
wc.hIconSm = LoadIcon(NULL, IDI_ASTERISK);
wc.hCursor = LoadCursorFromFile((LPCSTR)"FierceCursor.cur");
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
wc.lpszClassName = "FierceWindow";
RegisterClassEx(&wc);
game->hinstance = hInstance;
hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,
"FierceWindow", "Fierce",
WS_OVERLAPPED, 400, 100,
game->SCREEN_WIDTH, game->SCREEN_HEIGHT,
NULL, NULL, hInstance, NULL);
I've tried a few different settings for CreateWindowEx in hopes that maybe I used a style without an icon, but no luck. I'm using default icons right now to test.

Won't compile:
error: cast from 'CHAR*' to 'WORD' loses precision
Thanks for the edit btw.
Edit - Got it to compile by changing it to:
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(32513));
wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(32513));

It's because of your window style. The icon appears only if you add WS_SYSMENU style.
WS_OVERLAPPED|WS_SYSMENU

Related

How to Stop C/C++ WINAPI Window Showing on all Desktops?

I am writing a C/C++ windows app which calls CreateWindow as follows:
HWND hWnd = CreateWindow(pszClassName, title_.c_str(), WS_OVERLAPPED | WS_VISIBLE | WS_POPUP | WS_SIZEBOX, 50, 50, 400, 100, NULL, NULL, hInst, this);
Nothing particularly complex about it, however, it is appearing on every desktop on my computer when I switch from one desktop to another with CTRL-WIN < or >.
How do you make window you create in an app remain only on the desktop that you created it on?
I am compiling this app in 64bit and running it on Windows 10.
Code:
Global:
WNDCLASSEX wc;
Window procedure:
case WM_KEYDOWN:
if(wParam == VK_F11)
{
wc.style = HWND_DESKTOP;//The window is child
//of desktop
}
break;
WinMain:
LPCSTR windowName = "Answer on StackOwerflow";
MSG messages;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "The classname";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
wc.lpszClassName,
windowName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
ShowWindow(hWnd);
UpdateWindow(hWnd);
while(GetMessage(&messages, NULL, 0, 0) > 0)
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return 0;

How to set icon for c++ application window and icon for task bar?

How to set these icons for application?
I am using visual studio.
The icon on application window is empty.
I have already added a project.rc file to project and have added icon resource into it
And it shows the correct icon on the task bar when I dock it to the task bar.
I've found 2 methods of doing this after a quick search, assuming you are dealing the the WIN32 API in some form. This is by no measure a complete list of available methods.
1.Set the hIcon member of the WNDCLASSEX structure used to register your main window to an appropriate value like the following code does. (Where EXAMPLE_ICON is your icon resource)
WNDCLASSEX wcex = {};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(EXAMPLE_ICON));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = "ExampleIconWindowClass";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
2.You can send a WM_SETICON message to your main window like the following (Again, where EXAMPLE_ICON is your icon resource)
HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(EXAMPLE_ICON));
SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
Here is the end result of either of these methods:
Sorry about the poor formatting, I'm still unfamiliar with this website.

How to tell which parameters are required and which are not? (visual c++)

I am following an MSDN tutorial on Visual C++ on creating windows. It's using this code to register a window class.
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
When I look up the WNDCLASS structure on MSDN: WNDCLASS Structure it gives this implementation:
typedef struct tagWNDCLASS {
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
} WNDCLASS, *PWNDCLASS;
How can you tell by the documentation that you only need these three parameters?
lpfnWndProc
hInstance
lpszClassName
All parameters are required, but most of them can be set to default values.
WNDCLASS wc;
wc.style = CS_BYTEALIGNWINDOW | CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = DefWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(NULL);
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = 0;
wc.lpszClassName = "MYCLASS";

Create Child window on windows API

I'm trying to add a child window to my main window, but the function CreatWindow is throwing an exception saying that can't access address at 0x00000, but it works fine when I try to create a button, I tracked the variables and none of them are null, here it is:
WNDCLASSEX windowClass;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.hInstance = hInstance;
windowClass.lpfnWndProc = NULL;
windowClass.lpszClassName = className;
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.lpszMenuName = NULL;
if (!RegisterClassEx(&windowClass)){
return;
}
window = CreateWindowEx(0,
className,
(LPCTSTR)NULL,
WS_CHILD | WS_BORDER,
0, 0, 0, 0,
owner,
(HMENU)ID,
hInstance,
NULL);
the code above giving the error, the important variables come from here:
gl = new OpenGLContainer("hellogl", hInstance);
addChild(gl);
the first parameter is the className, the constructor only performs an attribution, the addChild method call gl->setOwner(window_handler) and gl->create() which is the first piece of code I posted.
I also saw the stack list, and the problem is after the program enter in the CreateWindow function, which is very strange because the debugger shows that none of the values(pointers) are null.
Could it be failing because lpfnWndProc is NULL?

Can't load ICON resource in VC++ 2012 Ultimate

I am having problem adding icon resource to C++ Win32 Project. I followed the steps in pictures down under but we I run program.exe there is no my icon in menu bar (only default icon). *.exe file has new icon as it should.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
LPCWSTR className = L"MyWindow";
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON1));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = className;
wc.hIconSm = LoadIcon(NULL, MAKEINTRESOURCE( IDI_ICON1));
if(!RegisterClassEx(&wc))
{
MessageBox(NULL,L"Error, registration of class faild", L"ERROR", MB_OK);
return 0;
}
Your window style is 0.
Try:
wc.style = WS_SYSMENU;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));