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

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

  1. #136
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

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

    It will behave itself. It will behave itself. It will behave itself........

    No, you're not boring me. I'm intrigued as to why the problem still persists when it's now OK with my test version.

    OK. To get some diagnostics, change
    Code:
    if (inst == NULL)
                MessageBox(NULL, "instnull", "instnull", MB_OK);
    to
    Code:
    if (inst == NULL) {
         char num[20];
         ltoa((long)msg, num, 10);
         MessageBox(NULL, num, "instnull", MB_OK);
    }
    and see what numbers the message box says.
    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.

  2. #137
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by 2kaud View Post
    It will behave itself. It will behave itself. It will behave itself........

    No, you're not boring me. I'm intrigued as to why the problem still persists when it's now OK with my test version.

    OK. To get some diagnostics, change
    Code:
    if (inst == NULL)
                MessageBox(NULL, "instnull", "instnull", MB_OK);
    to
    Code:
    if (inst == NULL) {
         char num[20];
         ltoa((long)msg, num, 10);
         MessageBox(NULL, num, "instnull", MB_OK);
    }
    and see what numbers the message box says.
    the messagebox isn't showed, but see the image:
    (is like: 'the program stop to working: close it?')
    Name:  winddow procedure instance.gif
Views: 15
Size:  70.7 KB

  3. #138
    Join Date
    Apr 2009
    Posts
    605

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

    1 thot, maybe the problem is here:

    Code:
    const char *labelpropname = "Cambalinho";
    const char *labelclassprop = "classaddr";
    because are variables outside of the class

  4. #139
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

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

    Ok. This is a different problem as Inst is now being set OK. The code below is my test program which correctly creates two label windows with the right text going to the right window.

    Code:
    #include "win501.h"
    #include <windows.h>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    const char propname[] = "Cambalinho";
    const char classprop[] = "classaddr";
    const char debugname[] = "debug.txt";
    
    enum MouseButtons
    {
    	none = -1,
        Left = WM_LBUTTONUP,
        Right = WM_RBUTTONUP,
        Middle = WM_MBUTTONUP,
        X1 = WM_XBUTTONUP,
        X2
    };
    
    int pos = 0;
    
    const string lab[6] = {"qwerty", "asdfgh", "zxcvbn", "poiuyt", "lkjhgf", "mnbvcx"};
    
    class label
    {
    private:
    	string text;
    
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    static ofstream os(debugname, ios_base::app);
    
    static labind = 0;
    
    	os << "0x" << hex << setw(4) << setfill('0') << msg << endl;
    
    WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), propname);
         if (oldproc == NULL)
                MessageBox(NULL, "oldprocnull", "oldprocnull", MB_OK);
    
    label *inst = (label*)GetProp(hwnd, classprop);
    	if (inst == NULL && msg == WM_NCCREATE){
             inst = (label*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
    		 SetProp(hwnd, classprop, (HANDLE)inst);
    	}
    
    	if (inst == NULL)
    		MessageBox(NULL, "instnull", "instnull", MB_OK);
    
        switch(msg) {
    		case WM_CTLCOLORSTATIC:
                {
                    HDC hdc = (HDC)wParam;
                    SetTextColor(hdc, RGB(0, 0, 0));
                    SetBkColor(hdc, RGB(255,0,0));
    				inst->clrBackColor = /*RGB(200, 200,200)*/(UINT)GetStockObject(GRAY_BRUSH);
                    return (LRESULT)GetStockObject(GRAY_BRUSH);
                }
    
    		case WM_NCHITTEST:
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    
    			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;
    
    		case WM_MOUSEMOVE:
    			SetFocus(hwnd);
    			inst->SetText(lab[labind]);
    			if (++labind > 5)
    				labind = 0;
    
    			break;
    
    		case WM_CHAR:
    			inst->SetText(lab[labind]);
    			if (++labind > 5)
    				labind = 0;
    			break;
    
            default:
    			break;
        }
    
    	//If oldproc is NULL then use DefWindowProc - but shouldn't be!
    	return oldproc ? CallWindowProc(oldproc, hwnd, msg, wParam, lParam) : DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    public:
        HWND hwnd;
    
    
    	UINT clrBackColor;
    
    	~label() {
    		DestroyWindow(hwnd);
    		hwnd = 0;
    	}
    
        label(HWND parent) {
    		text = "qwerty";
    
    	    WNDCLASS wc;
    		HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
    
    	    ZeroMemory(&wc, sizeof(WNDCLASS));
    		GetClassInfo(mod, "STATIC", &wc);
    
    		wc.hInstance = mod;
    		wc.lpszClassName = "CSTATIC";
    
    		// store the old WNDPROC of the EDIT window class
    		SetProp(parent, propname, (HANDLE)wc.lpfnWndProc);
    
    		// replace it with local WNDPROC
    		wc.lpfnWndProc = WndProc;
    
    		// register the new window class, "ShEdit"
    		RegisterClass(&wc);
    
            hwnd = CreateWindowEx(
                WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,// | WS_EX_TRANSPARENT, 
                "CSTATIC",
                "hello",
                SS_LEFT|WS_CHILD|WS_VISIBLE,
                pos, pos, 100, 100,
                parent,
                NULL,
                mod,
                (LPVOID)this);
    
    		pos += 150;
    
    		if (hwnd == NULL)
    			MessageBox(NULL, "error in create", "error", MB_OK);
    
    		if (SetProp(hwnd, classprop, (HANDLE)this) == 0)
    			MessageBox(NULL, "set class prop error", "error", MB_OK);
    
    		UpdateWindow(hwnd);
        }
    
    	void MouseUp(MouseButtons mb, bool bcont, bool bshift, int xpos, int ypos) {
    		switch (mb) {
    			case Left:
    				SetText("Lefty");
    				break;
    
    			case Right:
    				SetText("Righty");
    				break;
    
    			case Middle:
    				SetText("Middley");
    				break;
    		}
    	}
    
    struct Position
    {
    	int X;
    	int Y;
    };
    
    struct Size
    {
    	int Width;
    	int Height;
    };
    
    	Position GetPosition()
        {
            RECT LabelSize;
            GetWindowRect(hwnd,&LabelSize);
            Position crdSize = {LabelSize.left,LabelSize.top};
            return crdSize;
        }
    
    	Size GetSize()
        {
            RECT LabelSize;
            GetWindowRect(hwnd,&LabelSize);
            Size crdSize = {LabelSize.right-LabelSize.left,LabelSize.bottom-LabelSize.top};
            return crdSize;
        }
    	
        void SetText(string text)
        {
            char* chrText = (char*)text.c_str();
            SetWindowText(hwnd, chrText);
        }
    
    };
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[] = "CodeBlocksWindowsApp";
    
    //creating the window
    int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE, LPSTR, int nCmdShow)
    {
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
    	DeleteFile(debugname);
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default colour as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
    			0,
               //WS_EX_COMPOSITED | WS_EX_TRANSPARENT,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Code::Blocks Template Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    
        label label1(hwnd);
    	label1.SetText("oi");
    
    	label label2(hwnd);
    	label2.SetText("yes");
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    static labind = 0;
    static HBRUSH hBrush = CreateSolidBrush(RGB(200,20,145));
    
        switch (message)                  /* handle the messages */
        {
    		case WM_CHAR:
    			SetWindowText(hwnd, lab[labind].c_str());
    			if (++labind > 5)
    				labind = 0;
    			break;
    
    		case WM_LBUTTONDOWN:
    			SetWindowText(hwnd, "left mouse button down");
    			SetFocus(hwnd);
    			break;
    
    		case WM_RBUTTONDOWN:
    			SetWindowText(hwnd, "right mouse button down");
    			SetFocus(hwnd);
    			break;
    
    		case WM_MBUTTONDOWN:
    			SetWindowText(hwnd, "middle mouse button down");
    			SetFocus(hwnd);
    			break;
    
    		case WM_MOUSEMOVE:
    			SetFocus(hwnd);
    			SetWindowText(hwnd, lab[labind].c_str());
    			if (++labind > 5)
    				labind = 0;
    			break;
    
    		case WM_CTLCOLORSTATIC:
    			return SendMessage((HWND)lParam, WM_CTLCOLORSTATIC, wParam, lParam);
    
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
    
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    See if you can trace through your code to see where the problem resides. Try just creating one label etc to see if you can pin down the problem. If you can't, post the whole of your program so that I can compile it. I'll try it on MSVC 2012 and if it'll compile I'll have a look at it.
    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. #140
    Join Date
    Apr 2009
    Posts
    605

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

    sorry.. where is the #include "win501.h"?

  6. #141
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

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

    Sorry. You probably don't need it. It just sets the version for <windows.h>
    Code:
    #ifdef WINVER
    	#undef WINVER
    #endif
    #define WINVER 0x0501
    
    #ifdef _WIN32_WINNT
    	#undef _WIN32_WINNT
    #endif
    #define _WIN32_WINNT WINVER
    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.

  7. #142
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by 2kaud View Post
    Sorry. You probably don't need it. It just sets the version for <windows.h>
    Code:
    #ifdef WINVER
    	#undef WINVER
    #endif
    #define WINVER 0x0501
    
    #ifdef _WIN32_WINNT
    	#undef _WIN32_WINNT
    #endif
    #define _WIN32_WINNT WINVER
    yes.. now works fine.. thanks

  8. #143
    Join Date
    Apr 2009
    Posts
    605

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

    when you have the time, please test the code.
    the code works, but i'm seen 1 bug or my system is crazy.
    the main window and the 2 labels have the same bug, but the mouse give them the focus.
    i have the mouse stoped on label 1(or other control... it's the same), after some milliseconds(in cyle way and the same time) the label text it's changed automatic, why these behavior? i have seen these bug in console too. it's only with mouse move.
    is these a code problem or the system?
    Last edited by Cambalinho; January 6th, 2014 at 06:15 AM.

  9. #144
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

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

    For my test code, every time a WM_MOUSEMOVE message is received, the text in the appropriate window/conrol is changed to the next text in the cycle. If the text is changing then it's because a WM_MOUSEMOVE message has been received. Even if you don't think you have moved the mouse, the system thinks its positon has been changed so issues the message.
    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.

  10. #145
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by 2kaud View Post
    For my test code, every time a WM_MOUSEMOVE message is received, the text in the appropriate window/conrol is changed to the next text in the cycle. If the text is changing then it's because a WM_MOUSEMOVE message has been received. Even if you don't think you have moved the mouse, the system thinks its positon has been changed so issues the message.
    but the time cycle it's the same

  11. #146
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

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

    You can 'desentitise' the mouse movement by only doing an action if the mouse has moved by say more than 2 pixels. The WM_MOUSEMOVE code for the label control could be changed to something like the below.

    Code:
    		case WM_MOUSEMOVE:
    			{
    				static WORD oldx = 0, oldy = 0;
    
    				SetFocus(hwnd);
    				if (abs(LOWORD(lParam) - oldx) > 2 || abs(HIWORD(lParam) - oldy) > 2) {
    					oldx = LOWORD(lParam);
    					oldy = HIWORD(lParam);
    					inst->SetText(lab[labind]);
    					if (++labind > 5)
    						labind = 0;
    				}
    			}
    			break;
    Note that it would probably be better if oldx and oldy are part of the class. I've just made them static here for ease of an example. The issue of having them static like this is that the same values are shared across all the label controls.
    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.

  12. #147
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by 2kaud View Post
    You can 'desentitise' the mouse movement by only doing an action if the mouse has moved by say more than 2 pixels. The WM_MOUSEMOVE code for the label control could be changed to something like the below.

    Code:
    		case WM_MOUSEMOVE:
    			{
    				static WORD oldx = 0, oldy = 0;
    
    				SetFocus(hwnd);
    				if (abs(LOWORD(lParam) - oldx) > 2 || abs(HIWORD(lParam) - oldy) > 2) {
    					oldx = LOWORD(lParam);
    					oldy = HIWORD(lParam);
    					inst->SetText(lab[labind]);
    					if (++labind > 5)
    						labind = 0;
    				}
    			}
    			break;
    Note that it would probably be better if oldx and oldy are part of the class. I've just made them static here for ease of an example. The issue of having them static like this is that the same values are shared across all the label controls.
    so these 'problem' is like the mouse be so precise, that we don't notice, but can 'move' and do the effect, right?

  13. #148
    Join Date
    Apr 2009
    Posts
    605

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

    why these code isn't working?
    Code:
    HBRUSH g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
    
            //Working with messages
            switch(msg)
            {
                case WM_CTLCOLORSTATIC:
                {
                    SetTextColor(GetDC(inst->hwnd), inst->clrTextColor);
                    SetBkColor(GetDC(inst->hwnd),inst->clrBackColor);
                    SetBkMode(GetDC(inst->hwnd), TRANSPARENT);
                    g_hbrBackground = CreateSolidBrush(inst->clrBackColor);
                    return (LONG)g_hbrBackground;
                }
    the backcolor is correct, but the textcolor isn't changed and the text is showed with a backcolor, the SetBkMode() isn't working too

  14. #149
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,293

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

    From MSDN article:"SetTextColor"
    Return value
    If the function succeeds, the return value is a color reference for the previous text color as a COLORREF value.
    If the function fails, the return value is CLR_INVALID.
    Did you check the return value of SetTextColor?
    Did you debug this code to be sure you are passing the correct inst->clrTextColor value?
    Victor Nijegorodov

  15. #150
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

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

    SetTextColor, SetBkColor etc take as parameter 1 a hdc which is available as wParam, so there is no need to use GetDC(). Just use

    Code:
    SetTextColor((HDC)wParam, inst->clrTextColor);
    etc...
    Also note that every time you process this message, you are creating a new brush without deleting the old brush. When a brush is no longer being used it should be deleted using DeleteObject(). See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    When this message is processed, is the value of inst->clrTextColor the value it is supposed to be? Have you checked with the debugger to make sure this value is right?

    For my test program, the code
    Code:
    case WM_CTLCOLORSTATIC:
                {
                    SetTextColor((HDC)wParam, RGB(200, 0, 0));
                    SetBkColor((HDC)wParam, RGB(0,200,0));
    		SetBkMode((HDC)wParam, TRANSPARENT);
    		inst->clrBackColor = (UINT)GetStockObject(GRAY_BRUSH);
                    return (LRESULT)inst->clrBackColor;
                }
    produces red text on a transparent background. If the SetBkMode statement is commented out, it produces red text on a green background.
    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 10 of 11 FirstFirst ... 7891011 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center