CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 4 of 11 FirstFirst 1234567 ... LastLast
Results 46 to 60 of 155
  1. #46
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

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

    i'm using the pointer: why the SetWindowText() isn't used? the inst->hndlabel can be the problem... but tell more please?
    (i need use some functions inside of the window procedure)
    In the debug.txt file output, do you see the msg 0x0200 ?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #47
    Join Date
    Apr 2009
    Posts
    1,355

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

    Quote Originally Posted by 2kaud View Post
    In the debug.txt file output, do you see the msg 0x0200 ?
    no

  3. #48
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

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

    So the WM_MOUSEMOVE case won't be executed!

    Try this

    Code:
    #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";
    
    const string lab[6] = {"qwerty", "asdfgh", "zxcvbn", "poiuyt", "lkjhgf", "mnbvcx"};
    
    class label
    {
    private:
        HWND hwnd;
        string text;
    
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    static ofstream os(debugname, ios_base::app);
    
    static labind = 0;
    
    WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), propname);
    
    label *inst = (label*)GetProp(GetParent(hwnd), classprop);
    
    	if (oldproc == NULL)
    		MessageBox(NULL, "null", "null", MB_OK);
    
    	os << "0x" << hex << setw(4) << setfill('0') << msg << endl;
    
        switch(msg) {
            case WM_CREATE:
    		MessageBox(NULL, inst->text.c_str(), "From class instance", MB_OK);
    
    		//This doesn't work as the window hasn't been created!
                   SetWindowText(hwnd, "hello world");
                   break;
    
    	case WM_NCHITTEST:
    		if (DefWindowProc(hwnd, msg, wParam, lParam) == HTCLIENT)
    			SendMessage(hwnd, WM_MOUSEMOVE, 0, lParam);
    
    		break;
    
    	case WM_MOUSEMOVE:
    		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:
    
    	~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);
    		SetProp(parent, classprop, (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,
                "CSTATIC",
                "hello",
                SS_LEFT|WS_CHILD|WS_VISIBLE,
                0, 0, 100, 100,
                parent,
                NULL,
                mod,
                NULL);
    
    		if (hwnd == NULL)
    			MessageBox(NULL, "error in create", "error", MB_OK);
        }
    
        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);
        }
    };
    
    /*  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 lpszArgument, 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,                   /* 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");
    
        /* 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)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
                break;
    
            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;
    }
    Last edited by 2kaud; December 29th, 2013 at 11:43 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #49
    Join Date
    Apr 2009
    Posts
    1,355

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

    Quote Originally Posted by 2kaud View Post
    So the WM_MOUSEMOVE case won't be executed!

    Try this

    Code:
    #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";
    
    const string lab[6] = {"qwerty", "asdfgh", "zxcvbn", "poiuyt", "lkjhgf", "mnbvcx"};
    
    class label
    {
    private:
        HWND hwnd;
        string text;
    
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    static ofstream os(debugname, ios_base::app);
    
    static labind = 0;
    
    WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), propname);
    
    label *inst = (label*)GetProp(GetParent(hwnd), classprop);
    
        if (oldproc == NULL)
            MessageBox(NULL, "null", "null", MB_OK);
    
        os << "0x" << hex << setw(4) << setfill('0') << msg << endl;
    
        switch(msg) {
            case WM_CREATE:
            MessageBox(NULL, inst->text.c_str(), "From class instance", MB_OK);
    
            //This doesn't work as the window hasn't been created!
                   SetWindowText(hwnd, "hello world");
                   break;
    
        case WM_NCHITTEST:
            if (DefWindowProc(hwnd, msg, wParam, lParam) == HTCLIENT)
                SendMessage(hwnd, WM_MOUSEMOVE, 0, lParam);
    
            break;
    
        case WM_MOUSEMOVE:
            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:
    
        ~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);
            SetProp(parent, classprop, (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,
                "CSTATIC",
                "hello",
                SS_LEFT|WS_CHILD|WS_VISIBLE,
                0, 0, 100, 100,
                parent,
                NULL,
                mod,
                NULL);
    
            if (hwnd == NULL)
                MessageBox(NULL, "error in create", "error", MB_OK);
        }
    
        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);
        }
    };
    
    /*  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 lpszArgument, 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,                   /* 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");
    
        /* 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)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
                break;
    
            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;
    }
    the mouse move and click seems to be the same.... i have tested with keydown too, but isn't activated,, so what is the problem?

  5. #50
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by Cambalinho View Post
    the mouse move and click seems to be the same.... i have tested with keydown too, but isn't activated,, so what is the problem?
    First, get rid of the MessageBox calls in your window procedure!

    Use OutputDebugString() to output messages to the debug monitor. The reason why you should not use MessageBox() is that MessageBox() itself can cause issues with window procedures.

    Regards,

    Paul McKenzie

  6. #51
    Join Date
    Apr 2009
    Posts
    1,355

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

    Quote Originally Posted by Paul McKenzie View Post
    First, get rid of the MessageBox calls in your window procedure!

    Use OutputDebugString() to output messages to the debug monitor. The reason why you should not use MessageBox() is that MessageBox() itself can cause issues with window procedures.

    Regards,

    Paul McKenzie
    thanks for the tip

  7. #52
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

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

    i have tested with keydown too, but isn't activated,, so what is the problem?
    keyboard input is processed in WindowProcedure() for the main window. There is no position for keyboard chars as there is for the mouse. If you want to process keyboard chars in the label control you will need to determine in WindowProcedure() the position of the mouse and if its over the label window then send the keyboard message to the label window. This is a lot of work for what you are trying to do.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #53
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by Cambalinho View Post
    the mouse move and click seems to be the same.... i have tested with keydown too, but isn't activated,, so what is the problem?
    The problem is that you are not reading the documentation carefully for these messages:

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    What do you see that you must do if you process this message? Are you returning 0, as specified by the documentation above?

    For all of those messages, please read all of the documentation provided by MSDN on how to properly process such messages. Just "doing your own thing" when you get a message is a sure-fire way of not getting a Windows program to work properly.

    Regards,

    Paul McKenzie

  9. #54
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

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

    Quote Originally Posted by Cambalinho View Post
    the mouse move and click seems to be the same.... i have tested with keydown too, but isn't activated,, so what is the problem?
    You are not dealing with input focus. Keyboard messages go to the window that has input focus. In this program the main window always has input focus so the keyboard messages all go to the main window. If you want keyboard messages to go to the static window (unusual as static windows don't normally process mouse/keyboard messages) then you need to set the input focus to the static window when required so that it receives the required messages (and set it back to the main window when needed as well).

    Programming Windows is non-trivial and you really need to understand how it all fits together. Have a read of the books I suggested in my posts #12 and #22.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #55
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

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

    Try this. Moving the mouse will change the text for the window over which it is positioned. Pressing a key will change the text of the window over which the mouse is positioned. Pressing and releasing the left mouse button will display a message in the window over which the mouse is positioned.

    Code:
    #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";
    
    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;
    
    WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), propname);
    
    label *inst = (label*)GetProp(GetParent(hwnd), classprop);
    
    	if (oldproc == NULL)
    		MessageBox(NULL, "null", "null", MB_OK);
    
    	if (msg != 0x84 && msg != 0x200 && msg != 0x0c)
    		os << "0x" << hex << setw(4) << setfill('0') << msg << endl;
    
        switch(msg) {
    		case WM_NCHITTEST:
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    
    		case WM_LBUTTONUP:
    			SetFocus(hwnd);
    			inst->SetText("Left mouse button down");
    			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;
    
    	~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);
    		SetProp(parent, classprop, (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,
                "CSTATIC",
                "hello",
                SS_LEFT|WS_CHILD|WS_VISIBLE,
                0, 0, 100, 100,
                parent,
                NULL,
                mod,
                NULL);
    
    		if (hwnd == NULL)
    			MessageBox(NULL, "error in create", "error", MB_OK);
        }
    
        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);
        }
    };
    
    /*  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 lpszArgument, 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,                   /* 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");
    
        /* 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;
    
        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_MOUSEMOVE:
    			SetFocus(hwnd);
    			SetWindowText(hwnd, lab[labind].c_str());
    			if (++labind > 5)
    				labind = 0;
    			break;
    
    
            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;
    }
    You need to understand the message based nature of windows programming and how it is supposed to work, what the various messages mean, how to handle them and what to return etc. If you don't understand this then you will struggle programming windows for anything other than the 'standard' way expected - which this isn't.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #56
    Join Date
    Apr 2009
    Posts
    1,355

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

    thanks for all
    let me ask: why you use the SetFocus(hwnd);?
    (i know that it's for change the window focus)
    Last edited by Cambalinho; December 30th, 2013 at 07:59 AM.

  12. #57
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

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

    Keyboard input messages go to the window that has the focus. By default, this is your main window. Moving the mouse doesn't change the focus if it goes from one window to the next. So if the mouse moves across windows (ie from the main window to the static control window), the focus needs to be set to the window that has the mouse cursor. If the focus isn't changed, the keyboard messages will go the other window.

    Have a read of the windows books.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #58
    Join Date
    Apr 2009
    Posts
    1,355

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

    Quote Originally Posted by 2kaud View Post
    Keyboard input messages go to the window that has the focus. By default, this is your main window. Moving the mouse doesn't change the focus if it goes from one window to the next. So if the mouse moves across windows (ie from the main window to the static control window), the focus needs to be set to the window that has the mouse cursor. If the focus isn't changed, the keyboard messages will go the other window.

    Have a read of the windows books.
    so for the mouse events, we don't need the SetFocus().
    thanks for all
    now i can finish the class with some properties and 'events'.
    thanks for all.. thanks
    thanks to others that help me too.. thanks to all

  14. #59
    Join Date
    Apr 2009
    Posts
    1,355

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

    see the code:
    Code:
    #include <windows.h>
    #include <string>
    #include <functional>
    #define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname
    
    using namespace std;
    
    const char *labelpropname = "Cambalinho";
    const char *labelclassprop = "classaddr";
    
    struct Position
    {
    	int X;
    	int Y;
    };
    
    struct Size
    {
    	int Width;
    	int Height;
    };
    
    class label
    {
    private:
        HWND hwnd;
    
        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)
                MessageBox(NULL, "null", "null", MB_OK);
    
    
            switch(msg)
            {
    
                case WM_MOVE:
                {
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    inst->Move(xPos,yPos);//why these line make my progra, give me an error for get support? :(
                }
                break;
                case WM_NCHITTEST:
                    return DefWindowProc(hwnd, msg, wParam, lParam);
    
                case WM_LBUTTONUP:
                    SetFocus(inst->hwnd);
                    inst->SetText("Left mouse button up");
    			break;
    
                case WM_MOUSEMOVE:
                    inst->SetText("mouse move");
    
    			break;
    
                case WM_KEYUP:
    
                    inst->SetText(to_string(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(Create,(int x, int y));
        event(Move,(int x, int y));
    	~label()
    	{
    		DestroyWindow(hwnd);
    		hwnd = 0;
    	}
    
        label(HWND parent)
        {
    
    	    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, 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,
                "CSTATIC",
                "hello",
                SS_LEFT|WS_CHILD|WS_VISIBLE,
                0, 0, 100, 100,
                parent,
                NULL,
                mod,
                NULL);
    
    		if (hwnd == NULL)
    			MessageBox(NULL, "error in create", "error", MB_OK);
        }
    
        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);
        }
    
        string GetText()
        {
            char *lbltext;
            GetWindowText(hwnd,lbltext,255);
            string strtext=lbltext;
            return strtext;
        }
    };
    why the line 45 make my program crash?
    what i'm doing wrong with lies 77 and 78 based on line 4?
    Name:  my new error.jpg
Views: 1002
Size:  99.8 KB

  15. #60
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by Cambalinho View Post
    why the line 45 make my program crash?
    what i'm doing wrong with lies 77 and 78 based on line 4?
    Hopefully you're not expecting someone to figure out where lines 4, 45, 77, and 78 are.

    Please tell us where these lines are in the code you posted.

    Regards,

    Paul McKenzie

Page 4 of 11 FirstFirst 1234567 ... 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
  •  





Click Here to Expand Forum to Full Width

Featured