Thread: What's wrong with my Win32 Wrapper code?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    sorry, I spent a whole day cleaning up my disk so I can install a new big MSVC .Net, I thought the code does not compile because I am using MSVC 6.0, so I tried to compile it with .Net, unfortunately, the problems still isn't solved, CreateWindow() still fails and returns 0. I then tried in a yet another different compiler, Dev-C++ 4.9.8.0, the result still is the same.

    "Does the WM_CREATE message box ever come up?"
    no. I changed my code as Ken Fitlike suggested, this time the program run but it does no show, I can only see it in Windows Task Manager, the Processes tab, it is not in the Applications tab.

    "Did you replace the default of the switch statement with a call to DefWindowProc instead?"
    Is this just what I need to do? I did it, but the test.Create() still returns 0.

    "After modifying his code using Ken's suggestions, my version of his program runs properly."
    Dante Shamest, what compiler are you using? I wonder why mine doesn't works properly.

    This is my revised code:

    main.cpp
    Code:
    #include <windows.h>
    
    #include "Window.h"
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
        int returnvalue;
        CWindow test(hInstance);
        returnvalue = test.RegisterWindow();
        returnvalue = test.Create();
    
        // I checked the return value in debugging, found that
        // test.RegisterWindow() returns 1 while
        // test.Create() returns 0.
    
        MSG msg;
    
        while((returnvalue = GetMessage(&msg, NULL, 0, 0)) > 0)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return 0;
    }
    [edit]
    sorry, forget this:

    Window.cpp
    Code:
    #include "Window.h"
    
    LRESULT CALLBACK CWindow::staticWinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        CWindow* pWnd;
    
        if(uMsg == WM_NCCREATE)
        {
            SetWindowLong(hwnd, GWL_USERDATA, (long)((LPCREATESTRUCT(lParam))->lpCreateParams));
        }
    
        pWnd = (CWindow *)GetWindowLong(hwnd, GWL_USERDATA);
    
        if(pWnd)
            return pWnd->WinMsgHandler(hwnd, uMsg, wParam, lParam);
        else
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    
    LRESULT CALLBACK CWindow::WinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch(uMsg)
        {
            case WM_CREATE:
                MessageBox(NULL, "Create", "test", MB_OK);
                break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
                PostQuitMessage(0);
                break;
            default:
                DefWindowProc(hwnd,uMsg,wParam,lParam);
        }
        return 0;
    }
    
    BOOL CWindow::RegisterWindow()
    {
        WNDCLASS wcx;
    
        wcx.style         = CS_HREDRAW | CS_VREDRAW;
        wcx.lpfnWndProc   = CWindow::staticWinMsgHandler;
        wcx.cbClsExtra    = 0;
        wcx.cbWndExtra    = 0;
        wcx.hInstance     = m_hInstance;
        wcx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wcx.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wcx.lpszMenuName  = NULL;
        wcx.lpszClassName = "Window";
    
        return (RegisterClass(&wcx) != 0);
    }
    
    BOOL CWindow::Create()
    {
        m_hwnd = CreateWindow("Window", "Hello",
            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
            NULL, NULL, m_hInstance, (void *)this);
    
        return (m_hwnd != NULL);
    }
    Window.h hasn't changed.
    Last edited by miica; 02-22-2005 at 01:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 12:02 PM
  2. need example code for a win32 window
    By deian in forum Windows Programming
    Replies: 18
    Last Post: 09-29-2007, 06:33 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  5. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM