[win32] - creating controls using class's - Page 8
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 8 of 11 FirstFirst ... 567891011 LastLast
Results 106 to 120 of 155

Thread: [win32] - creating controls using class's

  1. #106
    Join Date
    Apr 2009
    Posts
    660

    Re: [win32] - creating controls using class's

    Quote Originally Posted by 2kaud View Post
    If you really want the code to set the text/background colour to be in the child procedure window, you could do something like this

    in child window procedure
    Code:
    	case WM_CTLCOLORSTATIC:
                {
                    HDC hdc = (HDC)wParam;
                    SetTextColor(hdc, RGB(0, 0, 0));
                    SetBkColor(hdc, RGB(255,0,0));
                    return (LRESULT)GetStockObject(GRAY_BRUSH);
                }
    in parent window procedure
    Code:
             case WM_CTLCOLORSTATIC:
    		return SendMessage((HWND)lParam, WM_CTLCOLORSTATIC, wParam, lParam);
    This works because for the WM_CTLCOLORSTATIC message sent to the control parent window, lParam is a handle to the child window from which the request came.
    thanks for that.
    let me ask 1 thing: when i use WM_PAINT in my child window(my label class), why the mouse messages aren't activated?

  2. #107
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,461

    Re: [win32] - creating controls using class's

    How are you using WM_PAINT?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #108
    Join Date
    Apr 2009
    Posts
    660

    Re: [win32] - creating controls using class's

    Quote Originally Posted by 2kaud View Post
    How are you using WM_PAINT?
    i have tryied before.. that's how i know that the mouse messages aren't working(only when i use the WM_PAINT message)

  4. #109
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,461

    Re: [win32] - creating controls using class's

    Quote Originally Posted by Cambalinho View Post
    i have tryied before.. that's how i know that the mouse messages aren't working(only when i use the WM_PAINT message)
    But how were you using WM_PAINT?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #110
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,461

    Re: [win32] - creating controls using class's

    For your MouseButtons enum, if you changed some of the values, you could somewhat simply the code for the WM_xBUTTONDOWN cases in the control window procedure.

    Consider

    Code:
    enum MouseButtons
    {
        none = -1,
        Left = WM_LBUTTONUP,
        Right = WM_RBUTTONUP,
        Middle = WM_MBUTTONUP,
        X1 = WM_XBUTTONUP,
        X2
    };
    In child window procedure
    Code:
    	    case WM_LBUTTONUP:
                case WM_RBUTTONUP:
                case WM_MBUTTONUP:
                case WM_XBUTTONUP:
                {
                    SetFocus(inst->hwnd);
    
                    int xPos = (int)(short) LOWORD(lParam);
                    int yPos = (int)(short) HIWORD(lParam);
    
                    bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
                    bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
    
    		MouseButtons MBButtons = none;
    
    	        if (msg == WM_XBUTTONUP) {
    			if (wParam & MK_XBUTTON1)
    				MBButtons = X1;
    			else 
    				if (wParam & MK_XBUTTON2)
    					MBButtons = X2;
    		} else
    			MBButtons = (MouseButtons)msg;
    
                    inst->MouseUp(MBButtons, blControl, blshift, xPos, yPos);
              }
              break;
    Last edited by 2kaud; January 2nd, 2014 at 04:39 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #111
    Join Date
    Apr 2009
    Posts
    660

    Re: [win32] - creating controls using class's

    Quote Originally Posted by 2kaud View Post
    For your MouseButtons enum, if you changed some of the values, you could somewhat simply the code for the WM_xBUTTONDOWN cases in the control window procedure.

    Consider

    Code:
    enum MouseButtons
    {
        none = -1,
        Left = WM_LBUTTONUP,
        Right = WM_RBUTTONUP,
        Middle = WM_MBUTTONUP,
        X1 = WM_XBUTTONUP,
        X2
    };
    In child window procedure
    Code:
    	    case WM_LBUTTONUP:
                case WM_RBUTTONUP:
                case WM_MBUTTONUP:
                case WM_XBUTTONUP:
                {
                    SetFocus(inst->hwnd);
    
                    int xPos = (int)(short) LOWORD(lParam);
                    int yPos = (int)(short) HIWORD(lParam);
    
                    bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
                    bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
    
    		MouseButtons MBButtons = none;
    
    	        if (msg == WM_XBUTTONUP) {
    			if (wParam & MK_XBUTTON1)
    				MBButtons = X1;
    			else 
    				if (wParam & MK_XBUTTON2)
    					MBButtons = X2;
    		} else
    			MBButtons = (MouseButtons)msg;
    
                    inst->MouseUp(MBButtons, blControl, blshift, xPos, yPos);
              }
              break;
    ok... thanks for all
    tomorrow or something i will continue with these class
    thanks to all

  7. #112
    Join Date
    Apr 2009
    Posts
    660

    Re: [win32] - creating controls using class's

    i have 1 problem with my class... heres the enetire class code:
    Code:
    #include <windows.h>
    #include <string>
    #include <functional>
    #define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname
    
    typedef COLORREF color;
    
    using namespace std;
    
    const char *labelpropname = "Cambalinho";
    const char *labelclassprop = "classaddr";
    
    enum MouseButtons
    {
        None=-1,
        Left=0,
        Right=1,
        Middle=2,
        X1=3,
        X2=4
    };
    
    struct Position
    {
    	int X;
    	int Y;
    };
    
    struct Size
    {
    	int Width;
    	int Height;
    };
    
    class label
    {
    private:
        HWND hwnd;
        color clrTextColor=RGB(0,0,0);
        color clrBackColor=RGB(255,255,255);
        bool blnTransparent=false;
        string caption="label";
    
        void TrackMouse(HWND hwnd)
        {
            TRACKMOUSEEVENT tme;
            tme.cbSize = sizeof(TRACKMOUSEEVENT);
            tme.dwFlags = TME_HOVER | TME_LEAVE; //Type of events to track & trigger.
            tme.dwHoverTime = 5000; //How long the mouse has to be in the window to trigger a hover event.
            tme.hwndTrack = hwnd;
            TrackMouseEvent(&tme);
        }
    
        static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
        {
            WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), labelpropname);
            label *inst = (label*)GetProp(GetParent(hwnd), labelclassprop);
    
    
            if (oldproc == NULL || inst == NULL)
                MessageBox(NULL, "null", "null", MB_OK);
    
            static bool TrackingMouse = false;
            static bool WindowStopMoving = NULL;
            static bool WindowStopResize = NULL;
            static const UINT_PTR MouseStopTimerID = 1;
            HBRUSH g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
    
            switch(msg)
            {
                case WM_ERASEBKGND:
                {
                    if (inst->blnTransparent==true)
                    {
                        HDC mdc = CreateCompatibleDC(NULL);//creating the doublebuffer
                        Size a=inst->GetSize();//get the control size
                        RECT c;
                        GetWindowRect(inst->hwnd,&c);
                        Position b=inst->GetPosition();//get ocntrol position
                        HBITMAP mbmp =  CreateBitmap(a.Width,a.Height,1,24,NULL); //create the bimap with a size
                        HBITMAP moldbmp = (HBITMAP)SelectObject(mdc,mbmp);//select the DC from that bitmap
                        HDC labeldc=GetDC(inst->hwnd);//get the label DC
                        BitBlt(mdc,0,0,a.Width,a.Height,GetDC(GetParent(inst->hwnd)),b.X, b.Y,SRCCOPY);//copy the parent control
                        TransparentBlt(mdc,0,0,a.Width,a.Height,labeldc,0,0,a.Width,a.Height,(UINT)inst->clrBackColor);//copy the label DC to mdc(double buffer), without backcolor
                        BitBlt(GetDC(inst->hwnd),0,0,a.Width,a.Height,mdc,b.X, b.Y,SRCCOPY);//now copy the double buffer to label dc
                        //clean objects
                        SelectObject(mdc,moldbmp);
                        DeleteObject(mbmp);
                        DeleteDC(mdc);
                        return 1;
                    }
    
                }
                break;
    
                case WM_CTLCOLORSTATIC:
                {
                    HDC hdcStatic = (HDC)wParam;
                    if (inst->blnTransparent==false)
                    {
    
                        /*SetTextColor(hdcStatic, inst->clrTextColor);
                        SetBkMode(hdcStatic, TRANSPARENT);
                        SetBkColor(hdcStatic,inst->clrBackColor);
                        g_hbrBackground = CreateSolidBrush(inst->clrBackColor);
                        return (LONG)g_hbrBackground;*/
                    }
                    else
                    {
                        SetTextColor(hdcStatic, inst->clrTextColor);
                        SetBkMode(hdcStatic, TRANSPARENT);
                        SetBkColor(hdcStatic,inst->clrBackColor);
                        g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
                        return (LONG)g_hbrBackground;
                    }
                }
                break;
                case WM_CREATE:
                {
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    inst->Create(xPos,yPos);
                }
                break;
    
                case WM_MOVE:
                {
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    inst->Move(xPos,yPos);
                    WindowStopMoving=false;
                    WindowStopResize=true;
                }
                break;
                case WM_SIZE :
                {
                    WindowStopResize=false;
                    WindowStopMoving=true;
                }
                break;
                case WM_EXITSIZEMOVE:
                {
                    if(WindowStopResize==false)
                    {
                        WindowStopResize=true;
                        inst->NotResize();
                    }
                    else if (WindowStopMoving==false)
                    {
                        WindowStopMoving=true;
                        inst->Stop();
                    }
                }
                break;
    
                case WM_NCHITTEST:
                    return DefWindowProc(hwnd, msg, wParam, lParam);
    
                case WM_LBUTTONDOWN:
                case WM_RBUTTONDOWN:
                case WM_MBUTTONDOWN:
                case WM_XBUTTONDOWN:
                {
                    SetFocus(inst->hwnd);
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
                    bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
                    MouseButtons MBButtons;
                    if((wParam & MK_LBUTTON)!= false)
                        MBButtons=Left;
                    else if (wParam & MK_RBUTTON)
                        MBButtons=Right;
                    else if (wParam & MK_MBUTTON)
                        MBButtons=Middle;
                    else if (wParam & MK_XBUTTON1)
                        MBButtons=X1;
                    else if (wParam & MK_XBUTTON2)
                        MBButtons=X2;
                    else
                        MBButtons=None;
                    inst->MouseDown(MBButtons,blControl,blshift,xPos,yPos);
                }
                break;
                case WM_LBUTTONUP:
                case WM_RBUTTONUP:
                case WM_MBUTTONUP:
                case WM_XBUTTONUP:
                {
                    SetFocus(inst->hwnd);
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
                    bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
                    MouseButtons MBButtons;
                    if (msg == WM_LBUTTONUP)
                        MBButtons=Left;
                    else if (msg == WM_RBUTTONUP)
                        MBButtons=Right;
                    else if (msg == WM_MBUTTONUP)
                        MBButtons=Middle;
    				else if (msg == WM_XBUTTONUP)
                    {
                        if (wParam & MK_XBUTTON1)
                            MBButtons=X1;
                        else if (wParam & MK_XBUTTON2)
                            MBButtons=X2;
                    }
                    else
                        MBButtons=None;
                    inst->MouseUp(MBButtons,blControl,blshift,xPos,yPos);
                }
                break;
                case WM_NCMOUSELEAVE:
                case WM_MOUSELEAVE :
                {
                    TrackingMouse = false;
                    inst->MouseLeave();
                }
                break;
                case WM_NCMOUSEMOVE:
                case WM_MOUSEMOVE:
                {
                    if (!TrackingMouse)
                    {
                        inst->TrackMouse(hwnd);
                        TrackingMouse = true;
                        inst->MouseEnter();
                    }
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    static bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
                    static bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
                    static MouseButtons MBButtons;
                    if((wParam & MK_LBUTTON)!= false)
                        MBButtons=Left;
                    else if ((wParam & MK_RBUTTON)!= false)
                        MBButtons=Right;
                    else if ((wParam & MK_MBUTTON)!= false)
                        MBButtons=Middle;
                    else if ((wParam & MK_XBUTTON1)!= false)
                        MBButtons=X1;
                    else if ((wParam & MK_XBUTTON2)!= false)
                        MBButtons=X2;
                    else
                        MBButtons=None;
    
                    inst->MouseMove(MBButtons,blControl,blshift,xPos,yPos);
                    KillTimer(hwnd, MouseStopTimerID);
                    SetTimer(hwnd, MouseStopTimerID, 500, NULL);
                }
    			break;
                case WM_MOUSEHOVER:
                {
                    inst->MouseHover();
                }
                break;
                case WM_TIMER:
                switch(wParam)
                {
                    case MouseStopTimerID:
                    {
                        KillTimer(hwnd, MouseStopTimerID);
                        inst->MouseStoped();
                    }
                }
                break;
                case WM_KEYUP:
                    inst->KeyUp(lParam,wParam);
    			break;
                case WM_KEYDOWN:
                    inst->KeyDown(lParam,wParam);
    			break;
    
                default:
    			break;
            }
    
            return oldproc ? CallWindowProc(oldproc, hwnd, msg, wParam, lParam) : DefWindowProc(hwnd, msg, wParam, lParam);
        }
    
    public:
    
        //is these 2 lines ok?
        //see the #define on top
        event(NotResize)=[](){;};
        event(Stop)=[](){;};
        event(MouseEnter)=[](){;};
        event(MouseLeave)=[](){;};
        event(Create,(int x, int y))=[](int x, int y){;};
        event(Move,(int x, int y))=[](int x, int y){;};
        event(MouseDown,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
        event(MouseUp,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
        event(MouseMove,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
        event(MouseStoped)=[](){;};
        event(MouseHover)=[](){;};
        event(MouseClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
        event(MouseDoubleClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
        event(MouseWhell,(int Whell, MouseButtons Button,bool control, bool shift, int x, int y))=[](int Whell, int Button, bool alt, bool shift,int x, int y){;};
        event(KeyDown,(int repeat,int Key))=[](int repeat,int Key){;};
        event(KeyUp,(int repeat,int Key))=[](int repeat,int Key){;};
    
    	~label()
    	{
    		DestroyWindow(hwnd);
    		hwnd = 0;
    	}
        label()
        {
            ;
        }
    
        void setparent(HWND parent)
        {
            static int i=i+1;
            string strclass=labelclassprop + to_string(i);
            labelclassprop=strclass.c_str();
            WNDCLASS wc;
    		HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
    
    	    ZeroMemory(&wc, sizeof(WNDCLASS));
    		GetClassInfo(mod, "STATIC", &wc);
    
    		wc.hInstance = mod;
    		wc.lpszClassName = labelclassprop;
    		wc.hbrBackground =  CreateSolidBrush(RGB(255,0,0));
    
    		// store the old WNDPROC of the EDIT window class
    		SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
    		SetProp(parent, labelclassprop, (HANDLE)this);
    
    		// replace it with local WNDPROC
    		wc.lpfnWndProc = WndProc;
    
    		// register the new window class, "ShEdit"
    		if (!RegisterClass(&wc))
    			MessageBox(NULL, "error in register", "error", MB_OK);
    
            hwnd = CreateWindowEx(
                WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
                labelclassprop,
                caption.c_str(),
                SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
                100, 100, 100, 100,
                parent,
                NULL,
                mod,
                NULL);
    		if (hwnd == NULL)
    			MessageBox(NULL, "error in create", "error", MB_OK);
            UpdateWindow(hwnd);
        }
    
        Size GetSize()
        {
            RECT LabelSize;
            GetWindowRect(hwnd,&LabelSize);
            Size crdSize = {LabelSize.right-LabelSize.left,LabelSize.bottom-LabelSize.top};
            return crdSize;
        }
    
        void SetSize(int Width, int Height)
        {
            RECT LabelSize;
            GetWindowRect(hwnd,&LabelSize);
            LabelSize.right=LabelSize.left+Width;
            LabelSize.bottom=LabelSize.top+Height;
            SetWindowPos(hwnd,HWND_TOP,LabelSize.left,LabelSize.top,Width,Height,SWP_NOMOVE|SWP_NOZORDER);
        }
    
        Position GetPosition()
        {
            RECT LabelSize;
            GetWindowRect(hwnd,&LabelSize);
            Position crdSize = {LabelSize.left,LabelSize.top};
            return crdSize;
        }
    
        void SetPosition(int x, int y)
        {
            RECT LabelSize;
            GetWindowRect(hwnd,&LabelSize);
            SetWindowPos(hwnd,HWND_TOP,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER);
        }
    
        void Transparent(bool transparent)
        {
            blnTransparent=transparent;
        }
    
        void SetText(string text)
        {
            caption=text;
            SetWindowText(hwnd,text.c_str());
        }
    
        string GetText()
        {
            return caption;
        }
    
        void SetBackColor(COLORREF color)
        {
            clrBackColor=color;
            ShowWindow(hwnd,SW_HIDE);
            ShowWindow(hwnd,SW_SHOW);
        }
    
        color GetBackColor()
        {
            return clrBackColor;
        }
    
        void SetTransparency(std::uint8_t Transperancy)
        {
            long wAttr = GetWindowLong(hwnd, GWL_EXSTYLE);
            SetWindowLong(hwnd, GWL_EXSTYLE, wAttr | WS_EX_LAYERED);
            SetLayeredWindowAttributes(hwnd, 0, Transperancy, 0x2);
            const char *text;
            text=to_string( GetLastError()).c_str();
            MessageBoxA(NULL,text,"erro",MB_OK);
        }
    
        void SetColorText(COLORREF color)
        {
            clrTextColor=color;
            ShowWindow(hwnd,SW_HIDE);
            ShowWindow(hwnd,SW_SHOW);
        }
    
        color GetColorText()
        {
            return clrTextColor;
        }
    
        HFONT GetFont()
        {
            HFONT actual;
            actual=(HFONT)SendMessage(hwnd,WM_GETFONT,0,0);
            return actual;
        }
        void SetFont(HFONT newfont)
        {
            SendMessage(hwnd,WM_SETFONT,(WPARAM)newfont,0);
        }
        void transparent(bool bltransparent)
        {
            blnTransparent=bltransparent;
        }
    
        HWND GetHWND()
        {
            return hwnd;
        }
    };
    i think i miss something, because when i create a 2nd label, the second label recives from the 1st label.
    maybe is because of hwnd and maybe i need use the 'this' too.
    can you advice me?

  8. #113
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,461

    Re: [win32] - creating controls using class's

    i think i miss something, because when i create a 2nd label, the second label recives from the 1st label.
    How are you creating the labels? Are you actually seeing the two label windows? From looking at your code you create all static windows at position 100, 100 so it looks like you are creating each label window on top of each other? Are you calling SetPosition() somewhere for the second label?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #114
    Join Date
    Apr 2009
    Posts
    660

    Re: [win32] - creating controls using class's

    Quote Originally Posted by 2kaud View Post
    How are you creating the labels? Are you actually seeing the two label windows? From looking at your code you create all static windows at position 100, 100 so it looks like you are creating each label window on top of each other? Are you calling SetPosition() somewhere for the second label?
    the labels are shopwed in diferent places(like i choose). the problem is:
    Code:
     label1.MouseEnter=[]()
        {
            label1.SetBackColor(RGB(255,0,0));
            label1.SetColorText(RGB(0,255,0));
            label1.SetText("mouse enter");
    
    
        };
    
        label1.MouseLeave=[]()
        {
            label1.SetBackColor(RGB(0,255,0));
            label1.SetColorText(RGB(0,0,255));
            label1.SetText("mouse leave");
    
    
        };
    
        label1.setparent(hwnd);
    
        label1.SetSize(100,100);
        label1.SetPosition(3,5);
        //label1.transparent(true);
    
        label2.MouseEnter=[]()
        {
            label2.SetBackColor(RGB(255,0,0));
            label2.SetColorText(RGB(0,255,0));
            label2.SetText("mouse enter");
    
    
        };
    
        label2.MouseLeave=[]()
        {
            label2.SetBackColor(RGB(0,255,0));
            label2.SetColorText(RGB(0,0,255));
            label2.SetText("mouse leave");
    
    
        };
    
        label2.setparent(hwnd);
        label2.SetPosition(300,5);
    the text is changed on label2 even when i move the mouse on label1.

  10. #115
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,461

    Re: [win32] - creating controls using class's

    Because you are setting the labelclass property for the control parent window rather than the control window. So every control has the same class instance rather than a different one. These changes should do it.

    Code:
    // store the old WNDPROC of the EDIT window class
    SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
    //SetProp(parent, labelclassprop, (HANDLE)this);
    Code:
    SetProp(hwnd, labelclassprop, (HANDLE)this);
    UpdateWindow(hwnd);
    Code:
    label *inst = (label*)GetProp(hwnd, labelclassprop);
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #116
    Join Date
    Apr 2009
    Posts
    660

    Re: [win32] - creating controls using class's

    Quote Originally Posted by 2kaud View Post
    Because you are setting the labelclass property for the control parent window rather than the control window. So every control has the same class instance rather than a different one. These changes should do it.

    Code:
    // store the old WNDPROC of the EDIT window class
    SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
    //SetProp(parent, labelclassprop, (HANDLE)this);
    Code:
    SetProp(hwnd, labelclassprop, (HANDLE)this);
    UpdateWindow(hwnd);
    Code:
    label *inst = (label*)GetProp(hwnd, labelclassprop);
    heres the code:
    Code:
    #include <windows.h>
    #include <string>
    #include <functional>
    #define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname
    
    typedef COLORREF color;
    
    using namespace std;
    
    const char *labelpropname = "Cambalinho";
    const char *labelclassprop = "classaddr";
    
    enum MouseButtons
    {
        None=-1,
        Left=0,
        Right=1,
        Middle=2,
        X1=3,
        X2=4
    };
    
    struct Position
    {
    	int X;
    	int Y;
    };
    
    struct Size
    {
    	int Width;
    	int Height;
    };
    
    class label
    {
    private:
        HWND hwnd;
        color clrTextColor=RGB(0,0,0);
        color clrBackColor=RGB(255,255,255);
        bool blnTransparent=false;
        string caption="label";
    
        void TrackMouse(HWND hwnd)
        {
            TRACKMOUSEEVENT tme;
            tme.cbSize = sizeof(TRACKMOUSEEVENT);
            tme.dwFlags = TME_HOVER | TME_LEAVE; //Type of events to track & trigger.
            tme.dwHoverTime = 5000; //How long the mouse has to be in the window to trigger a hover event.
            tme.hwndTrack = hwnd;
            TrackMouseEvent(&tme);
        }
    
        static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
        {
            WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), labelpropname);
            label *inst = (label*)GetProp(hwnd, labelclassprop);
    
    
            if (oldproc == NULL || inst == NULL)
                MessageBox(NULL, "null", "null", MB_OK);
    
            static bool TrackingMouse = false;
            static bool WindowStopMoving = NULL;
            static bool WindowStopResize = NULL;
            static const UINT_PTR MouseStopTimerID = 1;
            HBRUSH g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
            //if(hwnd!=inst->hwnd) return DefWindowProc(hwnd, msg, wParam, lParam);
            switch(msg)
            {
                case WM_ERASEBKGND:
                {
                    if (inst->blnTransparent==true)
                    {
                        HDC mdc = CreateCompatibleDC(NULL);//creating the doublebuffer
                        Size a=inst->GetSize();//get the control size
                        RECT c;
                        GetWindowRect(inst->hwnd,&c);
                        Position b=inst->GetPosition();//get ocntrol position
                        HBITMAP mbmp =  CreateBitmap(a.Width,a.Height,1,24,NULL); //create the bimap with a size
                        HBITMAP moldbmp = (HBITMAP)SelectObject(mdc,mbmp);//select the DC from that bitmap
                        HDC labeldc=GetDC(inst->hwnd);//get the label DC
                        BitBlt(mdc,0,0,a.Width,a.Height,GetDC(GetParent(inst->hwnd)),b.X, b.Y,SRCCOPY);//copy the parent control
                        TransparentBlt(mdc,0,0,a.Width,a.Height,labeldc,0,0,a.Width,a.Height,(UINT)inst->clrBackColor);//copy the label DC to mdc(double buffer), without backcolor
                        BitBlt(GetDC(inst->hwnd),0,0,a.Width,a.Height,mdc,b.X, b.Y,SRCCOPY);//now copy the double buffer to label dc
                        //clean objects
                        SelectObject(mdc,moldbmp);
                        DeleteObject(mbmp);
                        DeleteDC(mdc);
                        return 1;
                    }
    
                }
                break;
    
                case WM_CTLCOLORSTATIC:
                {
                    HDC hdcStatic = (HDC)wParam;
                    if (inst->blnTransparent==false)
                    {
    
                        /*SetTextColor(hdcStatic, inst->clrTextColor);
                        SetBkMode(hdcStatic, TRANSPARENT);
                        SetBkColor(hdcStatic,inst->clrBackColor);
                        g_hbrBackground = CreateSolidBrush(inst->clrBackColor);
                        return (LONG)g_hbrBackground;*/
                    }
                    else
                    {
                        SetTextColor(hdcStatic, inst->clrTextColor);
                        SetBkMode(hdcStatic, TRANSPARENT);
                        SetBkColor(hdcStatic,inst->clrBackColor);
                        g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
                        return (LONG)g_hbrBackground;
                    }
                }
                break;
                case WM_CREATE:
                {
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    inst->Create(xPos,yPos);
                }
                break;
    
                case WM_MOVE:
                {
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    inst->Move(xPos,yPos);
                    WindowStopMoving=false;
                    WindowStopResize=true;
                }
                break;
                case WM_SIZE :
                {
                    WindowStopResize=false;
                    WindowStopMoving=true;
                }
                break;
                case WM_EXITSIZEMOVE:
                {
                    if(WindowStopResize==false)
                    {
                        WindowStopResize=true;
                        inst->NotResize();
                    }
                    else if (WindowStopMoving==false)
                    {
                        WindowStopMoving=true;
                        inst->Stop();
                    }
                }
                break;
    
                case WM_NCHITTEST:
                    return DefWindowProc(hwnd, msg, wParam, lParam);
    
                case WM_LBUTTONDOWN:
                case WM_RBUTTONDOWN:
                case WM_MBUTTONDOWN:
                case WM_XBUTTONDOWN:
                {
                    SetFocus(inst->hwnd);
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
                    bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
                    MouseButtons MBButtons;
                    if((wParam & MK_LBUTTON)!= false)
                        MBButtons=Left;
                    else if (wParam & MK_RBUTTON)
                        MBButtons=Right;
                    else if (wParam & MK_MBUTTON)
                        MBButtons=Middle;
                    else if (wParam & MK_XBUTTON1)
                        MBButtons=X1;
                    else if (wParam & MK_XBUTTON2)
                        MBButtons=X2;
                    else
                        MBButtons=None;
                    inst->MouseDown(MBButtons,blControl,blshift,xPos,yPos);
                }
                break;
                case WM_LBUTTONUP:
                case WM_RBUTTONUP:
                case WM_MBUTTONUP:
                case WM_XBUTTONUP:
                {
                    SetFocus(inst->hwnd);
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
                    bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
                    MouseButtons MBButtons;
                    if (msg == WM_LBUTTONUP)
                        MBButtons=Left;
                    else if (msg == WM_RBUTTONUP)
                        MBButtons=Right;
                    else if (msg == WM_MBUTTONUP)
                        MBButtons=Middle;
    				else if (msg == WM_XBUTTONUP)
                    {
                        if (wParam & MK_XBUTTON1)
                            MBButtons=X1;
                        else if (wParam & MK_XBUTTON2)
                            MBButtons=X2;
                    }
                    else
                        MBButtons=None;
                    inst->MouseUp(MBButtons,blControl,blshift,xPos,yPos);
                }
                break;
                case WM_MOUSELEAVE :
                {
                    TrackingMouse = false;
                    inst->MouseLeave();
                }
                break;
                case WM_NCMOUSEMOVE:
                case WM_MOUSEMOVE:
                {
                    if (!TrackingMouse)
                    {
                        inst->TrackMouse(hwnd);
                        TrackingMouse = true;
                        inst->MouseEnter();
                    }
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    static bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
                    static bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
                    static MouseButtons MBButtons;
                    if((wParam & MK_LBUTTON)!= false)
                        MBButtons=Left;
                    else if ((wParam & MK_RBUTTON)!= false)
                        MBButtons=Right;
                    else if ((wParam & MK_MBUTTON)!= false)
                        MBButtons=Middle;
                    else if ((wParam & MK_XBUTTON1)!= false)
                        MBButtons=X1;
                    else if ((wParam & MK_XBUTTON2)!= false)
                        MBButtons=X2;
                    else
                        MBButtons=None;
    
                    inst->MouseMove(MBButtons,blControl,blshift,xPos,yPos);
                    KillTimer(hwnd, MouseStopTimerID);
                    SetTimer(hwnd, MouseStopTimerID, 500, NULL);
                }
    			break;
                case WM_MOUSEHOVER:
                {
                    inst->MouseHover();
                }
                break;
                case WM_TIMER:
                switch(wParam)
                {
                    case MouseStopTimerID:
                    {
                        KillTimer(hwnd, MouseStopTimerID);
                        inst->MouseStoped();
                    }
                }
                break;
                case WM_KEYUP:
                    inst->KeyUp(lParam,wParam);
    			break;
                case WM_KEYDOWN:
                    inst->KeyDown(lParam,wParam);
    			break;
    
                default:
    			break;
            }
    
            return oldproc ? CallWindowProc(oldproc, hwnd, msg, wParam, lParam) : DefWindowProc(hwnd, msg, wParam, lParam);
        }
    
    public:
    
        //is these 2 lines ok?
        //see the #define on top
        event(NotResize)=[](){;};
        event(Stop)=[](){;};
        event(MouseEnter)=[](){;};
        event(MouseLeave)=[](){;};
        event(Create,(int x, int y))=[](int x, int y){;};
        event(Move,(int x, int y))=[](int x, int y){;};
        event(MouseDown,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
        event(MouseUp,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
        event(MouseMove,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
        event(MouseStoped)=[](){;};
        event(MouseHover)=[](){;};
        event(MouseClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
        event(MouseDoubleClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
        event(MouseWhell,(int Whell, MouseButtons Button,bool control, bool shift, int x, int y))=[](int Whell, int Button, bool alt, bool shift,int x, int y){;};
        event(KeyDown,(int repeat,int Key))=[](int repeat,int Key){;};
        event(KeyUp,(int repeat,int Key))=[](int repeat,int Key){;};
    
    	~label()
    	{
    		DestroyWindow(hwnd);
    		hwnd = 0;
    	}
        label()
        {
            ;
        }
    
        void setparent(HWND parent)
        {
            static int i=i+1;
            string strclass=labelclassprop + to_string(i);
            labelclassprop=strclass.c_str();
            WNDCLASS wc;
    		HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
    
    	    ZeroMemory(&wc, sizeof(WNDCLASS));
    		GetClassInfo(mod, "STATIC", &wc);
    
    		wc.hInstance = mod;
    		wc.lpszClassName = labelclassprop;
    		wc.hbrBackground =  CreateSolidBrush(RGB(255,0,0));
    
    		// store the old WNDPROC of the EDIT window class
    		// store the old WNDPROC of the EDIT window class
            SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
            //SetProp(parent, labelclassprop, (HANDLE)this);
            SetProp(hwnd, labelclassprop, (HANDLE)this);
            UpdateWindow(hwnd);
    		// replace it with local WNDPROC
    		wc.lpfnWndProc = WndProc;
    
    		// register the new window class, "ShEdit"
    		if (!RegisterClass(&wc))
    			MessageBox(NULL, "error in register", "error", MB_OK);
    
            hwnd = CreateWindowEx(
                WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
                labelclassprop,
                labelclassprop,
                SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
                100, 100, 100, 100,
                parent,
                NULL,
                mod,
                NULL);
    		if (hwnd == NULL)
    			MessageBox(NULL, "error in create", "error", MB_OK);
            UpdateWindow(hwnd);
        }
    
        Size GetSize()
        {
            RECT LabelSize;
            GetWindowRect(this->hwnd,&LabelSize);
            Size crdSize = {LabelSize.right-LabelSize.left,LabelSize.bottom-LabelSize.top};
            return crdSize;
        }
    
        void SetSize(int Width, int Height)
        {
            RECT LabelSize;
            GetWindowRect(this->hwnd,&LabelSize);
            LabelSize.right=LabelSize.left+Width;
            LabelSize.bottom=LabelSize.top+Height;
            SetWindowPos(this->hwnd,HWND_TOP,LabelSize.left,LabelSize.top,Width,Height,SWP_NOMOVE|SWP_NOZORDER);
        }
    
        Position GetPosition()
        {
            RECT LabelSize;
            GetWindowRect(this->hwnd,&LabelSize);
            Position crdSize = {LabelSize.left,LabelSize.top};
            return crdSize;
        }
    
        void SetPosition(int x, int y)
        {
            RECT LabelSize;
            GetWindowRect(this->hwnd,&LabelSize);
            SetWindowPos(this->hwnd,HWND_TOP,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER);
        }
    
        void Transparent(bool transparent)
        {
            this->blnTransparent=transparent;
        }
    
        void SetText(string text)
        {
            this->caption=text;
            SetWindowText(this->hwnd,text.c_str());
        }
    
        string GetText()
        {
            return this->caption;
        }
    
        void SetBackColor(COLORREF color)
        {
            clrBackColor=color;
            ShowWindow(this->hwnd,SW_HIDE);
            ShowWindow(this->hwnd,SW_SHOW);
        }
    
        color GetBackColor()
        {
            return clrBackColor;
        }
    
        void SetTransparency(std::uint8_t Transperancy)
        {
            long wAttr = GetWindowLong(hwnd, GWL_EXSTYLE);
            SetWindowLong(hwnd, GWL_EXSTYLE, wAttr | WS_EX_LAYERED);
            SetLayeredWindowAttributes(hwnd, 0, Transperancy, 0x2);
            const char *text;
            text=to_string( GetLastError()).c_str();
            MessageBoxA(NULL,text,"erro",MB_OK);
        }
    
        void SetColorText(COLORREF color)
        {
            clrTextColor=color;
            ShowWindow(hwnd,SW_HIDE);
            ShowWindow(hwnd,SW_SHOW);
        }
    
        color GetColorText()
        {
            return clrTextColor;
        }
    
        HFONT GetFont()
        {
            HFONT actual;
            actual=(HFONT)SendMessage(hwnd,WM_GETFONT,0,0);
            return actual;
        }
        void SetFont(HFONT newfont)
        {
            SendMessage(hwnd,WM_SETFONT,(WPARAM)newfont,0);
        }
    
        void transparent(bool bltransparent)
        {
            blnTransparent=bltransparent;
        }
    
        HWND GetHWND()
        {
            return hwnd;
        }
    };
    i get several problems on running

  12. #117
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,461

    Re: [win32] - creating controls using class's

    Code:
    SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
            //SetProp(parent, labelclassprop, (HANDLE)this);
            SetProp(hwnd, labelclassprop, (HANDLE)this);
            UpdateWindow(hwnd);
    No wonder! I meant you to put the SetProp(hwnd..) just before the existing UpdateWindow() function and after the If (hwnd == NULL...) test.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #118
    Join Date
    Apr 2009
    Posts
    660

    Re: [win32] - creating controls using class's

    Quote Originally Posted by 2kaud View Post
    Code:
    SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
            //SetProp(parent, labelclassprop, (HANDLE)this);
            SetProp(hwnd, labelclassprop, (HANDLE)this);
            UpdateWindow(hwnd);
    No wonder! I meant you to put the SetProp(hwnd..) just before the existing UpdateWindow() function and after the If (hwnd == NULL...) test.
    now i did my friend:
    Code:
    void setparent(HWND parent)
        {
            static int i=i+1;
            string strclass=labelclassprop + to_string(i);
            labelclassprop=strclass.c_str();
            WNDCLASS wc;
    		HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
    
    	    ZeroMemory(&wc, sizeof(WNDCLASS));
    		GetClassInfo(mod, "STATIC", &wc);
    
    		wc.hInstance = mod;
    		wc.lpszClassName = labelclassprop;
    		wc.hbrBackground =  CreateSolidBrush(RGB(255,0,0));
    
    		// store the old WNDPROC of the EDIT window class
    		// store the old WNDPROC of the EDIT window class
            SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
            SetProp(parent, labelclassprop, (HANDLE)this);
    
            UpdateWindow(hwnd);
    		// replace it with local WNDPROC
    		wc.lpfnWndProc = WndProc;
    
    		// register the new window class, "ShEdit"
    		if (!RegisterClass(&wc))
    			MessageBox(NULL, "error in register", "error", MB_OK);
    
            hwnd = CreateWindowEx(
                WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
                labelclassprop,
                labelclassprop,
                SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
                100, 100, 100, 100,
                parent,
                NULL,
                mod,
                NULL);
    		if (hwnd == NULL)
    			MessageBox(NULL, "error in create", "error", MB_OK);
    			SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
                SetProp(hwnd, labelclassprop, (HANDLE)this);
            UpdateWindow(hwnd);
        }
    but i recive 1 messagebox with 'NULL' like a loop and i must close the application with windows tell me that the application stops working

  14. #119
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,461

    Re: [win32] - creating controls using class's

    Change
    Code:
    SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
            SetProp(parent, labelclassprop, (HANDLE)this);
    
            UpdateWindow(hwnd);
    to
    Code:
    //SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
    //SetProp(parent, labelclassprop, (HANDLE)this);
    
    //UpdateWindow(hwnd);
    You'll find in the WndProc() that you'll now get inst to be NULL sometimes. This is because you can't set the property of the label window until after the window has been created - but some messages such as WM_CREATE are sent before you can set the property. This is going to cause you a problem as you are using the property in WM_CREATE etc!

    I think you are going to have to pass this also via the lpParam parameter of CreateWindowsEx (the last param). You can then pick it up in the WM_CREATE message via the lParam parameter which points to a CREATESTRUCT struct which contains the value set in CreateWindow.

    See
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  15. #120
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,461

    Re: [win32] - creating controls using class's

    Code:
    hwnd = CreateWindowEx(
                WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
                labelclassprop,
                labelclassprop,
                SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
                100, 100, 100, 100,
                parent,
                NULL,
                mod,
                (LPVOID)this);
    Code:
    WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), labelpropname);
         if (oldproc == NULL)
                MessageBox(NULL, "oldprocnull", "oldprocnull", MB_OK);
    
    label *inst = (label*)GetProp(hwnd), labelclassprop);
         if (inst == NULL && msg == WM_CREATE)
             inst = (label*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
    
         if (inst == NULL)
              MessageBox(NULL, "instnull, "instnull", MB_OK);
    oldproc needs to be set/get from the label parent as previously as this value doesn't change between different label windows - only inst does.

    I hope you can follow this.
    Last edited by 2kaud; January 4th, 2014 at 11:45 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 8 of 11 FirstFirst ... 567891011 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center