Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Win32 Wrapper Class Not Working.

Featured Replies

I am tryint to create a wrapper class for creating a window in c++. The code compiles and links fine, but does not create a window. The problem seems to be in the CreateWindow function but I cannot seem to find it. I am new to windows gui programming, so any help would be greatly appreciated.

#include <windows.h>
#include <stdio.h>

#define WIN32_LEAN_AND_MEAN

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

class Window
{
private:
HWND hwnd;
HINSTANCE hInstance;

protected:
char winClassName[MAX_PATH];
char winCaption[MAX_PATH];

WNDCLASSEX wndclass;

DWORD winStyle;
int winXPos;
int winYPos;
int winWidth;
int winHeight;

public:
Window(HINSTANCE hInstance);

HWND GethWnd();
HINSTANCE GethInst();

BOOL Run();

BOOL Move(long XPos, long YPos);
BOOL Resize(long Width, long Height);

BOOL ShowMouse(BOOL Show = TRUE);
};


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
Window main(hInstance);
return main.Run();
}

Window::Window(HINSTANCE hInstance)
{
strcpy(winClassName, "AppClass");
strcpy(winCaption, "Application Caption");

winStyle = WS_OVERLAPPEDWINDOW;
winXPos = 10;
winYPos = 10;
winWidth = 640;
winHeight = 480;
hwnd = NULL;

wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = NULL;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = winClassName;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wndclass.lpfnWndProc = WndProc;
}

HWND Window::GethWnd()
{
return hwnd;
}

HINSTANCE Window::GethInst()
{
return hInstance;
}

BOOL Window::Run()
{
MSG msg;

if(!RegisterClassEx(&wndclass))
{
MessageBox (NULL, TEXT ("Class not registered!"), winClassName, MB_ICONERROR);
return FALSE;
}
hwnd = CreateWindowEx(0,
winClassName,
winCaption,
winStyle,
winXPos,
winYPos,
winWidth,
winHeight,
NULL,
NULL,
hInstance,
NULL);
if(!hwnd)
{
MessageBox (NULL, TEXT ("Error createing window"), winClassName, MB_ICONERROR);
return FALSE;
}

ShowWindow (hwnd, SW_SHOWNORMAL);
UpdateWindow (hwnd);

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}

return TRUE;
}

BOOL Window::Move(long XPos, long YPos)
{
RECT ClientRect;

GetClientRect(hwnd, &ClientRect);
MoveWindow(hwnd, XPos, YPos, ClientRect.right, ClientRect.bottom, TRUE);

return TRUE;
}

BOOL Window::Resize(long Width, long Height)
{
RECT WndRect, ClientRect;
long WndWidth, WndHeight;

GetWindowRect(hwnd, &WndRect);
GetClientRect(hwnd, &ClientRect);

WndWidth = (WndRect.right - (ClientRect.right - Width)) - WndRect.left;
WndHeight = (WndRect.bottom - (ClientRect.bottom - Height)) - WndRect.top;

MoveWindow(hwnd, WndRect.left, WndRect.top, WndWidth, WndHeight, TRUE);

return TRUE;
}

BOOL Window::ShowMouse(BOOL Show)
{
ShowCursor(Show);
return TRUE;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;

switch (message)
{
case WM_CREATE:
PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);
return 0;

case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);

GetClientRect (hwnd, &rect);

DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);

EndPaint (hwnd, &ps);
return 0;

case WM_DESTROY:
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}

Thanks Again.


  • Author

Just an update I figured it out. It was stupid of me. I never set the hInstance member variable. I do not know how I missed it, but I bet I went through the code a hundred times and did not see it.

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.