Windows Programming: WNDCLASS structure: hIcon member Problem - c++

I've Created a custom .ico with VS2010 for a game I'm making with DirectX
When I set the .ico file as hIcon member of my wndClass, it will show in the taskbar, but not in the title bar.
I've read the other threads about this, I've searched google, msdn: no luck... I've tried just about anything I could come up with, it still wouldn't show.
The weird thing is: when I switch the hIcon to a default icon (like IDI_ERROR) it will show both in taskbar and in title bar, but not with my custom made .ico
Can anyone help me?
Here is my code:
HICON Icon = LoadIcon( NULL, MAKEINTRESOURCE(ID_ICON_MYTETRIS) );
WNDCLASS wndClass;
ZeroMemory(&wndClass,sizeof(wndClass));
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndClass.hIcon = Icon;
wndClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = _T("WindowClass0");
if (RegisterClass(&wndClass) == false) {
return -1;
}
RECT rc = {0,0,300,225};
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
HWND hWnd = CreateWindow(_T("WindowClass0")
,_T("2D_DirectX_Tetris")
,WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX
,CW_USEDEFAULT
,CW_USEDEFAULT
,rc.right - rc.left
,rc.bottom - rc.top
,NULL
,NULL
,hInstance
,NULL );
if( hWnd == false) {
return -1;
}
ShowWindow( hWnd, iCmdShow );

You have to specify an instance handle in your call to LoadIcon so it knows which module's resources to use.
The standard icons require a NULL instance, that's why they work for you.

Related

How to create a window with CreateWindowEx but ignoring the scale settings on Windows

When I create a window with CreateWindowEx it will follow the resolution but also use the scale settings from the display settings. So, in 1920 x 1080 if I try to create the window, the size is actually 1200 something when scale is at 150%. Is there a way to get around this limitation? If I just set the size manually to 1920 x 1080 I get a cropped window. Thanks.
auto activeWindow = GetActiveWindow();
HMONITOR monitor = MonitorFromWindow(activeWindow, MONITOR_DEFAULTTONEAREST);
//
// Get the logical width and height of the monitor
MONITORINFOEX monitorInfoEx;
monitorInfoEx.cbSize = sizeof(monitorInfoEx);
GetMonitorInfo(monitor, &monitorInfoEx);
auto cxLogical = monitorInfoEx.rcMonitor.right - monitorInfoEx.rcMonitor.left;
auto cyLogical = monitorInfoEx.rcMonitor.bottom - monitorInfoEx.rcMonitor.top;
vScreenSize = { cxLogical, cyLogical };
WNDCLASS wc;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpfnWndProc = olc_WindowEvent;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpszMenuName = nullptr;
wc.hbrBackground = nullptr;
wc.lpszClassName = olcT("Wusik SQ480 Engine");
RegisterClass(&wc);
DWORD dwExStyle = 0;
DWORD dwStyle = WS_VISIBLE | WS_POPUP;
olc::vi2d vTopLeft = vWindowPos;
// Keep client size as requested
RECT rWndRect = { 0, 0, vWindowSize.x, vWindowSize.y };
AdjustWindowRectEx(&rWndRect, dwStyle, FALSE, dwExStyle);
int width = rWndRect.right - rWndRect.left;
int height = rWndRect.bottom - rWndRect.top;
olc_hWnd = CreateWindowEx(dwExStyle, olcT("Wusik SQ480 Engine"), olcT(""), dwStyle,
vTopLeft.x, vTopLeft.y, width, height, NULL, NULL, GetModuleHandle(nullptr), this);
Thanks to Remy Lebeau it was as simple as adding the following call to the main initialization of the app. Now I always get a window with the physical resolution and not a logic scaled resolution.
SetProcessDPIAware();

DirectX window media keys not responding

Im making a game with a custom game engine and when you have selected the window that it creates it doesn't allow you to use media keys e.g. changing volume or playing/pausing music or anything that has to do with windows like getting the windows start menu and alt+tab behaves weird
It feels like my window is "blocking" all system specific keys and commands
The code is written in c++
Heres the code i'm using for creating the window:
bool FrameWork::CreateDXWnd(int x, int y, int width, int height)
{
HWND hwnd;
WNDCLASSEX wc;
m_hInstance = GetModuleHandle(nullptr);
//setup window class with default setings:
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = m_hInstance;
//wc.hIcon = LoadIcon(nullptr, IDI_WINLOGO);
wc.hIcon = (HICON)LoadImage(m_hInstance, ".\\Assets\\Icons\\NgineIcon512.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
wc.hIconSm = wc.hIcon;
wc.hCursor = LoadCursor(nullptr, IDC_HAND);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = nullptr;
wc.lpszClassName = applicationName.c_str();
wc.cbSize = sizeof(WNDCLASSEX);
if (!RegisterClassEx(&wc))
{
Error(1);
return false;
}
//Style of window
//int nStyle = WS_OVERLAPPED | WS_SYSMENU | WS_VISIBLE | WS_CAPTION | WS_MINIMIZEBOX;
int nStyle = WS_OVERLAPPED | WS_SYSMENU | WS_VISIBLE | WS_CAPTION | WS_MINIMIZEBOX;
SettingsManager::GetInstance()->SetNativeResolution(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
if (SettingsManager::GetInstance()->GetDisplayMode() == FULLSCREEN)
{
DEVMODE dmScreenSettings;
memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
dmScreenSettings.dmSize = sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = (unsigned long)SettingsManager::GetInstance()->GetScreenWidth();
dmScreenSettings.dmPelsHeight = (unsigned long)SettingsManager::GetInstance()->GetScreenHeight();
dmScreenSettings.dmBitsPerPel = 32;
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
}
else
{
}
if ((SettingsManager::GetInstance()->GetDisplayMode() == BORDERLESS))
{
hwnd = CreateWindowEx(WS_EX_APPWINDOW, applicationName.c_str(), applicationName.c_str(), WS_POPUP, x, y, SettingsManager::GetInstance()->GetScreenWidth(), SettingsManager::GetInstance()->GetScreenHeight(), nullptr, nullptr, m_hInstance, nullptr);
}
else
{
hwnd = CreateWindowEx(WS_EX_APPWINDOW, applicationName.c_str(), applicationName.c_str(), nStyle, x, y, SettingsManager::GetInstance()->GetScreenWidth(), SettingsManager::GetInstance()->GetScreenHeight(), nullptr, nullptr, m_hInstance, nullptr);
}
if (hwnd == nullptr)
{
Error(2);
Ngine::GetInstance()->Release();
PostQuitMessage(0);
return false;
}
if (!Ngine::GetInstance()->InitGraphics(hwnd))
{
Error(hwnd, 30);
Ngine::GetInstance()->Release();
PostQuitMessage(0);
UnregisterClass(applicationName.c_str(), m_hInstance);
m_hInstance = nullptr;
DestroyWindow(hwnd);
return false;
}
Ngine::GetInstance()->GetGraphics()->SetHwnd(hwnd);
ShowWindow(hwnd, SW_SHOW);
SetForegroundWindow(hwnd);
SetFocus(hwnd);
return true;
}
Tim. The code you are showing deals with creating a window, not with how to handle the input to the window. To handle input, you need to set up a Message handling loop in your code.
Typically, in a game engine, you will have a main loop or "Game Loop", with each pass through the loop typically resulting in a single frame drawn. The first thing the Game Loop does is handle window messages. This allows you to handle the typical windows functions. Then, once you have no more messages to deal with, you will move on to handling your game's logic and rendering.
I recommend you take a look at Braynzarsoft's tutorials. The tutorial I linked deals with setting up your window and how to make a bare-bones Windows Message system.
Once you understand the basics of that, you can refine your post if needed to get more information.

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;

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?

C++ Create popup window

I want to make a popup window appear when I hit a button that is located on the main window of my program. I have looked everywhere for popup window examples, and most answers just showed me how to make a window, buttt I already have a main window in my application.
Basically the difficulty I am having is knowing where to put the new window class and code for my popup window(since I want the pop window to have different properties than my main one). Should it also be in the WinMain function or should it be in one of the cases in the CALLBACK WndProcedure section?
some of the code I have for my window is:
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, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
I just am not sure where to put it... Should it be placed in the code that is activated when the button is pressed, or should it stay in the WinMain function, together with the information of the other window?
I hope you can understand my predicament, I am somewhat new to programming.
Register window at main() together with main window. And in case statement of WndProc call CreateWindowEx().
But I don't know how make animation of popup. I can imagine that code should put somewhere in the WM_CREATE, but this I don't know.
if you mean just another window for example window for settings you should use DialogBoxes.
P.S. If you are russian, then I strongly recommend the book "Win32 API. Effective Application Development" by Yuri Shchupak on russian
The new window should be created in the case statement in your wndProc that handles the button click.