Thread: how subclassing controls?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    how subclassing controls?

    i'm trying build a class for show a STATIC control and use my own windor procedure for it. i was searching in these forum and i found that i can use the procedure inside of class, but must be static.
    the procedure seems be activated (i have used a message box), but the msg const's values aren't correct...
    see the class:
    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    #include <process.h>
    
    using namespace std;
    
    
    
    class label
    {
    private:
    
    HWND hwnd;
    
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CREATE:
                ShowWindow(hwnd,SW_SHOW);
                SetWindowText(hwnd,"hello");
    
            break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    
    
    public:
    
        label(HWND value)
        {
    
            hwnd = CreateWindowEx(
                WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
                "STATIC",
                "",
                SS_LEFT|WS_CHILD|WS_VISIBLE,
                0, 0, 100, 100,
                value,
                NULL,
                GetModuleHandle(NULL),
                NULL);
    
                ShowWindow(hwnd,SW_SHOW);
                UpdateWindow(hwnd);
                SetWindowLong(hwnd, GWL_WNDPROC, (LONG)WndProc);//set your custom procedure :)
        }
    
        COORD GetSize()
        {
            RECT LabelSize;
            GetWindowRect(hwnd,&LabelSize);
            COORD crdSize={LabelSize.right-LabelSize.left,LabelSize.bottom-LabelSize.top};
            return crdSize;
        }
    
        void SetText(string text)
        {
            char* chrText=(char*)text.c_str();
            SetWindowText(hwnd, chrText);
        }
    
    
    };
    i see 2 problems with these code(and not error messages):
    1 - the control isn't showed. but if i take of the:
    Code:
    SetWindowLong(hwnd, GWL_WNDPROC, (LONG)WndProc);
    inside of constructor, the STATIC is showed.

    2 - the window procedure seems be activated, but, like i said, the msg values aren't correct

    i have search in these forum without sucess
    plase someone tell me what i'm doing wrong

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Haven't looked your code, but here is a thread on the subject of making a OO wrapper for the Win32 API.

    Member functions as child window procedures

    gg

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by Codeplug View Post
    Haven't looked your code, but here is a thread on the subject of making a OO wrapper for the Win32 API.

    Member functions as child window procedures

    gg
    before i build these thread i have seen that thread, but i didn't find my answer...
    seems that i can't do subclassing but window procedure super classing, for get the WM_CREATE and the others too

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    after more search in these forum i did more work:
    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    #include <process.h>
    
    using namespace std;
    
    
    
    
    
    
    class label
    {
    private:
        HWND hwnd;
        WNDCLASS wc;
        WNDPROC m_pfnPrevWndProc;
    
    
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        WNDPROC oldproc =(WNDPROC)GetWindowLongPtr(GetParent(hwnd),GWLP_USERDATA);
        UINT i=(UINT)GetWindowLongPtr(GetParent(hwnd),GWLP_USERDATA);
        switch(msg)
        {
            case WM_NCCREATE:
            {
                CREATESTRUCT *createstruct = (CREATESTRUCT*)lParam;
    
                SetWindowLong(hwnd, GWL_USERDATA, (long)createstruct->lpCreateParams);
            }
            break;
            case WM_CREATE:
            {
                SetWindowText(hwnd,"hello world");
    
                break;
            }
            case WM_MOUSEMOVE:
            {
                SetWindowText(hwnd,"mouse move");
                break;
            }
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return (CallWindowProc(oldproc, hwnd, msg, wParam, lParam));
        }
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    public:
    
        label(HWND value)
        {
    
    
        ZeroMemory(&wc, sizeof(WNDCLASS));
        GetClassInfo((HINSTANCE)GetModuleHandle(NULL), "STATIC", &wc);
    
        wc.hInstance = (HINSTANCE)GetModuleHandle(NULL);
        wc.lpszClassName = "CSTATIC";
    
        // store the old WNDPROC of the EDIT window class
        m_pfnPrevWndProc = wc.lpfnWndProc;
    
        // replace it with local WNDPROC
        wc.lpfnWndProc = WndProc;
    
        // register the new window class, "ShEdit"
        if (!RegisterClass(&wc)) MessageBox(NULL,"error","error",MB_OK);
            hwnd = CreateWindowEx(
                WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
                "CSTATIC",
                "hello",
                SS_LEFT|WS_CHILD|WS_VISIBLE,
                0, 0, 100, 100,
                value,
                NULL,
                GetModuleHandle(NULL),
                NULL);
                if(hwnd==NULL) MessageBox(NULL,"error","error",MB_OK);
                SetWindowLongPtr (GetParent(hwnd),GWLP_USERDATA,(LONG_PTR)m_pfnPrevWndProc);
                ShowWindow(hwnd,SW_SHOW);
                UpdateWindow(hwnd);
    
        }
    
        COORD GetSize()
        {
            RECT LabelSize;
            GetWindowRect(hwnd,&LabelSize);
            COORD crdSize={LabelSize.right-LabelSize.left,LabelSize.bottom-LabelSize.top};
            return crdSize;
        }
    
        void SetText(string text)
        {
            char* chrText=(char*)text.c_str();
            SetWindowText(hwnd, chrText);
        }
    
    
    };
    the control is showed, but the window procedure isn't right... because i don't have access to WM_CREATE and others

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    There are basically 2 methods for making an OO wrapper for Win32: the MFC way and the ATL way. The thunk'n method that I showed in the linked thread is the ATL way. The MFC way uses hooks and re-classing on CE. Here is another thread on that: Puting win32 callbacks into a class

    The links in that thread no longer work, but they exist in the internet archive:
    https://web.archive.org/web/20090131...ce_prefix.html
    https://web.archive.org/web/20060103...icles/wndproc/

    gg

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by Codeplug View Post
    There are basically 2 methods for making an OO wrapper for Win32: the MFC way and the ATL way. The thunk'n method that I showed in the linked thread is the ATL way. The MFC way uses hooks and re-classing on CE. Here is another thread on that: Puting win32 callbacks into a class

    The links in that thread no longer work, but they exist in the internet archive:
    https://web.archive.org/web/20090131...ce_prefix.html
    https://web.archive.org/web/20060103...icles/wndproc/

    gg
    sorry.. i'm using the Code Blocks IDE.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subclassing controls
    By filler_bunny in forum Windows Programming
    Replies: 3
    Last Post: 04-28-2004, 05:43 PM
  2. Subclassing controls in a class
    By filler_bunny in forum Windows Programming
    Replies: 9
    Last Post: 03-21-2004, 09:12 PM
  3. Subclassing
    By Mithoric in forum Windows Programming
    Replies: 5
    Last Post: 11-22-2003, 05:30 PM
  4. Subclassing
    By dhirst2880 in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2002, 10:25 AM
  5. Diff between window controls and dialog controls
    By Garfield in forum Windows Programming
    Replies: 13
    Last Post: 01-18-2002, 05:49 AM