Create Child window on windows API - c++

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?

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;

C++ Windows link subsystem question

When using the windows SDK to compile a program writen in C++, if I specify -subsystem:windows,6.1,
the width of the window is smaller. If I don't, or do -subsystem:windows without the 6.1, the width is normal.
I'm curious why this does this, and if there's a way to make it stay the same width regardless of what command line I pass to link.
EDIT: So it's also height, height and width are both different. And if i look at it with Inspect.exe, it says the size is the same each time.
EDIT2: Also it's a window application created with CreateWindow, not a console.
EDIT3: Here's the full code that initializes my window:
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(wcex.hInstance, MAKEINTRESOURCE(SMALL_ICON));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = g_szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(SMALL_ICON));
g_hMainWnd = CreateWindow(
g_szWindowClass,
t_szWindowTitle,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
392, 250,
NULL,
NULL,
hInstance,
NULL
);
And I resize the window with this:
SetWindowPos(hWnd, NULL,
(GetSystemMetrics(SM_CXFULLSCREEN)/2)- (392/2),
(GetSystemMetrics(SM_CYFULLSCREEN)/2) - (250/2),
392, 120, SWP_NOZORDER);
I'm suspecting you are specifying some windows styles that are only supported in Windows7 (Win7 = 6.1). Post the full CreateWindow call with arguments and also try turning off Aero. The window border may be included in the width/height for example in one scenario.

Windows Programming: WNDCLASS structure: hIcon member Problem

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.

WinAPI: Create a window with a specified client area size

I was wondering how can I create a window using Win32 API with a specific client area size.
When trying to create a window using the following piece of code, the entire window is 640x480, with the window's chrome taking some of the client area:
HWND hWnd;
WNDCLASSEX WndClsEx;
ZeroMemory(&WndClsEx, sizeof(WNDCLASSEX));
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = DefWindowProc;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = TEXT("Title");
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&WndClsEx);
hWnd = CreateWindowEx( NULL,
TEXT("Title"),
TEXT("Title"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,
NULL,
hInstance,
NULL);
Assuming simple math won't quite solve the problem, how do I take the chrome size into account?
Note: I'm using SDL after creating the window, but I'm guessing it's bound to the window size and makes no difference to its size.
You can use the AdjustWindowRect or AdjustWindowRectEx function to calculate the window size given a desired client area size.

No icon in title bar after switching from WNDCLASS to WNDCLASSEX

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