Jump to content

Win32 Wrapper Class Not Working.


Recommended Posts

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.

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...