CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2010
    Posts
    6

    Api static LRESULT WndProc in class, Window appears, can't move Window

    Hi Everyone,

    I've read a lot of win32 api wrapper in c++, i don't have something like a pair list.
    I only want to create a window in OOP, that i can move.... and change the WndProc
    Funktion with inheriting.

    I tried the solution of "Super KOKO" to get the pointer in the wm_create message.

    The problem is, that I want to return my own virtual WndProc function.
    But when i try to create the stuff with the getwindowlong and setwindowlong,
    short, it doensn't work. I've tried to create the WNDCLASSEX in the class.....
    When you look at the end i've there suggestions, where the problem could be

    Code:
    #ifndef _WINDOW_H_
    #define _WINDOW_H_
    
    #include <windows.h>
    
    class Window {
    public:
      Window();
      virtual ~Window();
      operator::HWND();
      virtual int setWindow(HWND hWnd,
                            WNDCLASSEX wc,
                            HINSTANCE hInstance,
                            LPSTR lpCmdLine, int nCmdShow);
    
      virtual LRESULT WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
        { return DefWindowProc(hWnd, msg, wParam, lParam); } 
      //bool ShowWindow(hWnd, nCmdShow);
      //bool UpdateWindow(hWwnd);
    
      //getter
      HWND getmhWnd();
      WNDCLASSEX getmwc();
      MSG getmmsg();
    
      //setter
      setmhWnd(HWND hWnd);
      setmwc(WNDCLASSEX wc);
      setmmsg(MSG msg);
    protected:
      WNDCLASSEX m_wc;
      HWND m_hWnd;
      MSG m_msg;
      
    private:
      static LRESULT CALLBACK _WndProc(HWND, UINT, WPARAM, LPARAM);
    };
    
    #endif
    Code:
    /*============================window.cpp===============================/*
     */
    #include "window.h"
    
    /*------------------------------KONSTRUKTOR----------------------------/*
     */
    Window::Window() {
      //m_hWnd = hWnd;
    } 
    
    /*---------------------------------GETTER------------------------------/*
     */
    
    HWND Window::getmhWnd() {
      return m_hWnd;
    }
    
    WNDCLASSEX Window::getmwc() {
      return m_wc;
    }
    
    MSG Window::getmmsg() {
      return m_msg;
    }
    
    /*--------------------------------SETTER-------------------------------/*
     */
    
    Window::setmhWnd(HWND hWnd) {
      m_hWnd = hWnd;
    }
    
    Window::setmwc(WNDCLASSEX wc) {
      m_wc = wc;
    }
    
    /*---------------------------PUBLIC FUNKCTIONS-------------------------/*
     */
    
    Window::operator HWND() {
      return m_hWnd;
    }
    
    int Window::setWindow(HWND hWnd, // dont need this, only an option
                          WNDCLASSEX wc, // dont need this, only an option
                          HINSTANCE hInstance,
                          LPSTR lpCmdLine, int nCmdShow) {
    
      ZeroMemory(&m_wc, sizeof m_wc);
      
      //Step 1: Registering the Window Class
      m_wc.cbSize        = sizeof(WNDCLASSEX);
      m_wc.style         = 0;
      m_wc.lpfnWndProc   = (WNDPROC)Window::_WndProc;
      m_wc.cbClsExtra    = 0;
      m_wc.cbWndExtra    = 0;
      m_wc.hInstance     = hInstance;
      m_wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
      m_wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
      m_wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
      m_wc.lpszMenuName  = NULL;
      m_wc.lpszClassName = "myWindowClass";
      m_wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);/**/
    
      if(!RegisterClassEx(&m_wc)) {
    		MessageBox(NULL, "Window Registration Failed!", "Error!",
    			         MB_ICONEXCLAMATION | MB_OK);
    		return 0;
      }/**/
    
      // Step 2: Creating the Window
    	m_hWnd = CreateWindowEx(
    		WS_EX_CLIENTEDGE,
                    "myWindowClass",
    		classN,
    		WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
    		NULL, NULL, hInstance, this);
    
      if(hWnd == NULL) {
    		MessageBox(NULL, "Window Creation Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
      }/**/
    
      return 0;
    }
    
    LRESULT Window::_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
     
      Window* firstWindow = NULL;
      if (msg == WM_CREATE) {      
        CREATESTRUCT* m_lParam = (CREATESTRUCT*)(lParam);
        SetWindowLong(hWnd, GWL_USERDATA, (LONG)m_lParam->lpCreateParams);
        if(!m_lParam->lpCreateParams) {
          MessageBox(NULL, "kein Pointer wurde angelegt", "INFO", MB_OK);
        }
        /*firstWindow->setmhWnd(hWnd);*/
      } else { 
        Window* firstWindow = (Window*) (GetWindowLong(hWnd, GWL_USERDATA));
      }
    
      /**/
      if( firstWindow ) {
        return firstWindow->WndProc( hWnd, msg, wParam, lParam );
      } else {
        //MessageBox(hWnd, "no own window", "INFO", MB_OK);
        return DefWindowProc(hWnd, msg, wParam, lParam);
      }
    }
    
    Window::~Window() {
      if (m_hWnd != NULL) {
        //delete theWindow;
        ::DestroyWindow(m_hWnd);
      }
    }
    Code:
    ==============WINMAIN===============
      Window firstWindow;
      MessageBox(NULL, "Hier vor Error", "Error", MB_OK);
      firstWindow.setWindow(hwnd, wc, hInstance, lpCmdLine, nCmdShow);
      
      ShowWindow(firstWindow.getmhWnd(), nCmdShow);//hwnd, nCmdShow);//
      UpdateWindow(firstWindow.getmhWnd());//hwnd);//
    	
      // Step 3: The Message Loop
      while(GetMessage(&firstWindow.getmmsg(), NULL, 0, 0) > 0)
      {
    		TranslateMessage(&firstWindow.getmmsg());//&Msg);//
    		DispatchMessage(&firstWindow.getmmsg());//&Msg);//
      }
      return firstWindow.getmmsg().wParam;//Msg.wParam;//
    I don't know why the window doesn't react it was only drawn.
    Maybe it's the GetMessage LOOP. or the link to the lpfnWndProc

    IN THE window.cpp in the LRESULT Window::_WndProc(....)
    there is a row with a MESSAGEBOX with the string "no own window"
    when i commend it out, then i only get the MessageBox the whole time.

    I ALSO have to exit my program always with the Task Manager.....

    Sorry, when i get on your nerves, yeah many articles have been written about
    these stuff. But i don't get it.......

    Best regards
    RiggiT

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Api static LRESULT WndProc in class, Window appears, can't move Window

    Do you have errors or warnings when you compile your project?
    Code:
    LRESULT Window::_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    The CALLBACK keyword is missing after LRESULT.

    I think CALLBACK is important, because it tells the compiler to use __stdcall instead of __cdecl which is the default convention for C/C++. See http://msdn.microsoft.com/en-us/libr...ks(VS.71).aspx

  3. #3
    Join Date
    Mar 2010
    Posts
    6

    Re: Api static LRESULT WndProc in class, Window appears, can't move Window

    Thank you for your answer

    I've declared in my header LRESULT CALLBACK _WndProc......
    I've correct this in my cpp, but there was no change......

    No compile or linker errors.

    I think the problem is in the WNDCLASSEX structure, which i register in my
    class. And the .lpfnWndProc = _WndProc....

    My _WndProc ist static, maybe that is the fault......
    that the .lpfnWndProc links to a static class function.

    I only can register the WNDCLASSEX structure when I've
    linked the .lpfnWndProc ist that right?

    And I store in my class the handle with m_hWnd,
    it's confunsing.....

    Best Regards
    RiggiT

  4. #4
    Join Date
    Mar 2010
    Posts
    6

    Re: Api static LRESULT WndProc in class, Window appears, can't move Window

    I've found the fault,

    it's quite confusing for me......

    I've created a public var MSG msg
    and then i could move the Window.

    in my WINMAIN i've done something like this.....
    firstWindow.msg and it worked, BUT

    That ist not very OOP i think .
    I want a protected member, maybe
    I've to create another DispatchMessage(......)

    I DONT KNOW when msg ist set in WINMAIN, can give me
    anybody a short HELP?

  5. #5
    Join Date
    Mar 2010
    Posts
    6

    Re: Api static LRESULT WndProc in class, Window appears, can't move Window

    I think that i cannot have a protected MSG in my
    class, to handle the WINMAIN.

    MSG must be created in the WINMAIN.

    MAYBE there is another solution.

    If anybody knows it, he maybe post

    Best Regards
    RiggiT

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured