Note that there are some explanatory texts on larger screens.

plurals
  1. POWin32 window fails to create?
    primarykey
    data
    text
    <p>I did my research but couldn't find an answer. The closest thing I found is "<a href="https://stackoverflow.com/questions/12475131/cannot-create-window">Cannot create window</a>", but it didn't help me. So, here it is!</p> <p><strong>Basic info</strong></p> <p>I have a static library and an application using a static library. I hooked up the application to the static library correctly (Include directories, library directories, actual library dependencies, etc). In the static library i have 1 file: <code>IWindow.h</code>. In the application I have 3 files: <code>Main.cpp</code>, <code>WindowMain.h</code> and <code>WindowMain.cpp</code>. <code>IWindow.h</code> defines an abstract window class:</p> <pre><code>#ifndef IWINDOW_H #define IWINDOW_H #include &lt;Windows.h&gt; namespace NamespaceName { template&lt;class T&gt; class IWindow { public: static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); virtual ~IWindow(){} virtual LRESULT handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) = 0; virtual VOID paint(HDC hDC) = 0; VOID create(HINSTANCE hI, LPCWSTR title, LPCWSTR className, DWORD dwStyle, DWORD dwExStyle = 0, int x = CW_USEDEFAULT, int y = CW_USEDEFAULT, int nWidth = CW_USEDEFAULT, int nHeight = CW_USEDEFAULT, HWND hWndParent = 0, HMENU hMenu = 0); HWND getHWND(); BOOL isCreated(); protected: HWND m_hWnd; BOOL created; }; template&lt;class T&gt; LRESULT CALLBACK IWindow&lt;T&gt;::windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { T* pThis = NULL; if(uMsg == WM_NCCREATE) { CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam; pThis = (T*)pCreate-&gt;lpCreateParams; SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pThis); } else { pThis = (T*)GetWindowLongPtr(hWnd, GWLP_USERDATA); } if(pThis) { return pThis-&gt;handleMessage(uMsg, wParam, lParam); } else { return DefWindowProc(hWnd, uMsg, wParam, lParam); } } template&lt;class T&gt; VOID IWindow&lt;T&gt;::create(HINSTANCE hI, LPCWSTR title, LPCWSTR className, DWORD dwStyle, DWORD dwExStyle = 0, int x = CW_USEDEFAULT, int y = CW_USEDEFAULT, int nWidth = CW_USEDEFAULT, int nHeight = CW_USEDEFAULT, HWND hWndParent = 0, HMENU hMenu = 0) { WNDCLASS windowClass = {0}; windowClass.hInstance = hI; windowClass.lpszClassName = className; windowClass.style = CS_HREDRAW | CS_VREDRAW; windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); windowClass.lpfnWndProc = IWindow::windowProc; RegisterClass(&amp;windowClass); m_hWnd = CreateWindowEx(dwExStyle, className, title, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this); created = (m_hWnd ? TRUE : FALSE); } template&lt;class T&gt; HWND IWindow&lt;T&gt;::getHWND() { return m_hWnd; } template&lt;class T&gt; BOOL IWindow&lt;T&gt;::isCreated() { return created; } } #endif </code></pre> <p>Then,</p> <p><code>WindowMain.h</code> defines a subclass of <code>IWindow.h</code></p> <p>Code:</p> <pre><code>#ifndef WINDOWMAIN_H #define WINDOWMAIN_H #include &lt;FolderName\Video\GUI\IWindow.h&gt; class WindowMain : public NamespaceName::IWindow&lt;WindowMain&gt; { public: ~WindowMain(){} LRESULT handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam); VOID paint(HDC hDC); }; #endif </code></pre> <p>And,</p> <p>it's accompanying .cpp file</p> <p>completes it:</p> <pre><code>#include "WindowMain.h" LRESULT WindowMain::handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT ps; switch(uMsg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_PAINT: { hDC = BeginPaint(m_hWnd, &amp;ps); FillRect(hDC, &amp;ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1)); paint(hDC); EndPaint(m_hWnd, &amp;ps); } return 0; } return DefWindowProc(m_hWnd, uMsg, wParam, lParam); } VOID WindowMain::paint(HDC hDC) { } </code></pre> <p>And finally,</p> <p>the <code>Main.cpp</code></p> <p>Code:</p> <pre><code>#include &lt;Windows.h&gt; #include &lt;tchar.h&gt; #include &lt;GdiPlus.h&gt; #include "WindowMain.h" #pragma comment(lib, "Gdiplus.lib") int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { WindowMain window; MSG msg; Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; Gdiplus::GdiplusStartup(&amp;gdiplusToken, &amp;gdiplusStartupInput, NULL); window.create(hInstance, L"Test Window", L"Test Window Class", WS_OVERLAPPEDWINDOW); if(!window.isCreated()) return 1; ShowWindow(window.getHWND(), nCmdShow); UpdateWindow(window.getHWND()); while (GetMessage(&amp;msg, NULL, 0, 0)) { TranslateMessage(&amp;msg); DispatchMessage(&amp;msg); } Gdiplus::GdiplusShutdown(gdiplusToken); return 0; } </code></pre> <p>The <code>window.isCreated()</code> always returns <code>false</code>, thus making the <code>if</code> statement in the <code>Main.cpp</code> invert it to <code>true</code> and always returning 1 and exiting the application. If I omit the <code>if</code> statement from <code>Main.cpp</code> the window does not show up and the application goes on forever, until i force-stop it in the IDE.</p> <p><strong>Additional questions (answer in comments if you want, these are optional and do not relate to the former question in any way)</strong></p> <p>I don't like Visual Studio at all. I prefer using NetBeans so much more. I tried to use it for Windows programming but I failed. Do I have to use Visual Studio for Windows programming? Does it have some magical compiler that can compile Win32 programs in a special way? Or am I just doing something wrong?</p> <p>What are these Gdiplus tokens and startup input? A link to somewhere that explains it would be awesome.</p> <p><strong>Finally</strong></p> <p>Thank you for taking your time to read this, and potentially trying to help me. If you need any more information I'll be happy to provide it. If the question was poorly constructed please let me know how to improve it and I will. ;)</p> <p><strong>Edit #1</strong></p> <p>Found this: "<a href="https://stackoverflow.com/questions/15080129/win32-api-window-wont-open?rq=1">Win32 API window won&#39;t open</a>", just for the record, it didn't help my case either.</p> <p><strong>Edit #2</strong></p> <p>In <code>IWindow.h</code>, when I was creating a <code>WNDCLASS</code>, for the window process, I tried to use <code>T::windowProc</code> instead of <code>IWindow::windowProc</code>, but it didn't help either.</p> <p><strong>Edit #3</strong></p> <p>Found "<a href="https://stackoverflow.com/questions/6541953/winapi-window-doesnt-appear?rq=1">WinAPI window doesn&#39;t appear</a>", but didn't help either.</p> <p><strong>Edit #4</strong></p> <p>"Try setting windowClass.cbWndExtra to sizeof(LONG_PTR) before registering the class." -suggestion from the comments. Tried it too, and also didn't help.</p> <p><strong>Edit #5</strong></p> <p>Tried replacing <code>WNDCLASS</code> with <code>WNDCLASSEX</code> and <code>RegisterClass</code> with <code>RegisterClassEx</code> and added <code>windowClassEx.cbSize = sizeof(WNDCLASSEX)</code> (I changed the variable name from <code>windowClass</code> to <code>windowClassEx</code> too), but didn't help either...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload