Thread: Simple win32 window class

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124

    Simple win32 window class

    There is no compilation errors. Though, the program is unable to create window after registering it. Any suggestions?

    Code:
    #include <windows.h>
    #include <string>
    using namespace std;
    
    #define DisplayError(text, caption) MessageBox(NULL, text,caption, MB_ICONEXCLAMATION | MB_OK)
    
    class Wnd
    {
        WNDPROC WinControls;
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
        string  WindowName;
        bool child;
    
        public:
    
    		Wnd() { }
    
       void start(char * Tile , WNDPROC  Procedure  )
        {
            WindowName =  Tile;
    
        }
    
        bool WndPrepare( HINSTANCE hInstance, int menuID, bool Menubuild = false )
        {
            WNDCLASSEX wcex;
    
            wcex.cbSize = sizeof(WNDCLASSEX);
            wcex.style          = CS_NOCLOSE | CS_DBLCLKS;
            wcex.lpfnWndProc    = WinControls;
            wcex.cbClsExtra     = 0;
            wcex.cbWndExtra     = 0;
            wcex.hInstance      = hInstance;
            wcex.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
            wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
            wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
            if( Menubuild)
            wcex.lpszMenuName   = MAKEINTRESOURCE(menuID);
            else
            wcex.lpszMenuName   = NULL;
            wcex.lpszClassName  = (LPCWSTR)WindowName.c_str();
            wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    
            if(!RegisterClassEx(&wcex))
            {
                DisplayError( L"HUGE ERROR MAN WINDOW CAN'T REGISTER)", L"ALERT" );
                return false;
            }
                return true;
        }
    
        void WndBuild( HINSTANCE * hInstance, int nCmdShow)
        {
            hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, L"NarraDream",(LPCWSTR)WindowName.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, *hInstance, NULL);
            if(!hwnd)
            {
                DisplayError(L"Could not create Window",L"ALERT");
            }
    
            ShowWindow(hwnd, nCmdShow);
            UpdateWindow(hwnd);
        }
    
        int Run(void)
        {
            while (GetMessage(&Msg, NULL, 0, 0) > 0)
            {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
            }
    
            return (int)  Msg.wParam;
        }
    };
    
    
    LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
    
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
    
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
    {
     Wnd wnd;
    	 
      wnd.start("hey", WinProc);
    
     wnd.WndPrepare(0, false);
    
     wnd.WndBuild(&hInstance,nCmdShow);
      return ( wnd.Run() );
    
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> (LPCWSTR)WindowName.c_str()
    Square pegs do not fit in round holes.

    LPCTSTR operations/ convertion

    You should understand what TCHAR's are, and how "UNICODE" and "_UNICODE" affect Win32 builds. But don't use tchars. Just make all your strings wide:
    > wstring WindowName;
    > void start(const wchar_t *Title

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Main window in Win32 programming
    By guitarist809 in forum Windows Programming
    Replies: 5
    Last Post: 01-11-2010, 03:32 PM
  2. Win32 Window Position
    By AMcIntyre in forum Windows Programming
    Replies: 7
    Last Post: 10-09-2009, 10:53 PM
  3. Adding a console window to a Win32 app
    By VirtualAce in forum Game Programming
    Replies: 7
    Last Post: 02-01-2009, 01:09 PM
  4. need example code for a win32 window
    By deian in forum Windows Programming
    Replies: 18
    Last Post: 09-29-2007, 06:33 AM
  5. Create a Window in a Win32 Dll?
    By RedLenses in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2002, 02:21 PM