Jump to content
  • Advertisement
Sign in to follow this  
hankjrfan00

Win32 Wrapper Class Not Working.

This topic is 4280 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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.

Share this post


Link to post
Share on other sites
Advertisement
I feel stupid. I do not see how I could have missed that one. I bet I went through that code a hundred times and did not see it.

I really thank the both of you for your help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!