CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 5 of 11 FirstFirst ... 2345678 ... LastLast
Results 61 to 75 of 155
  1. #61
    Join Date
    Apr 2009
    Posts
    1,355

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

    Quote Originally Posted by Paul McKenzie View Post
    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
    sorry...
    4: #define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname
    (with includes files)

    45: inst->Move(xPos,yPos);//why these line make my program, give me an error for get support?
    (inside the window procedure)

    77: event(Create,(int x, int y));
    78: event(Move,(int x, int y));
    (in public variables)
    Last edited by Cambalinho; December 30th, 2013 at 03:04 PM.

  2. #62
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by Cambalinho View Post
    label *inst = (label*)GetProp(GetParent(hwnd), labelclassprop);
    You failed to check the return value of GetProp. If that property is not found, the return value is NULL. If that function returned a NULL, then inst is NULL.

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

    If "inst" is not NULL, then debug into the Move() function. Can you do that? If you can, where does the debugger take you? Does it actually go into a function, or does the debugger take you to some weird address that makes no sense? If it is the latter, this means that inst->Move points to garbage.

    Regards,

    Paul McKenzie

  3. #63
    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

    Where's the declaration/definition of move()?
    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. #64
    Join Date
    Apr 2009
    Posts
    1,355

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

    Quote Originally Posted by Paul McKenzie View Post
    You failed to check the return value of GetProp. If that property is not found, the return value is NULL. If that function returned a NULL, then inst is NULL.

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

    If "inst" is not NULL, then debug into the Move() function. Can you do that? If you can, where does the debugger take you? Does it actually go into a function, or does the debugger take you to some weird address that makes no sense? If it is the latter, this means that inst->Move points to garbage.

    Regards,

    Paul McKenzie
    but these works fine:
    Code:
    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;
    if i take that line off(inst->Move(xPos,yPos); ) the code works fine

  5. #65
    Join Date
    Apr 2009
    Posts
    1,355

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

    Quote Originally Posted by 2kaud View Post
    Where's the declaration/definition of move()?
    after the 'public:', inside of the class

  6. #66
    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

    event(Move,(int x, int y));
    ????????? As a definition of the class function Move()
    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)

  7. #67
    Join Date
    Apr 2009
    Posts
    1,355

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

    Quote Originally Posted by 2kaud View Post
    ????????? As a definition of the class function Move()
    sorry:
    #define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname

  8. #68
    Join Date
    Apr 2009
    Posts
    1,355

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

    sorry... was my mistake.
    how can i use my 'events' if i don't give them what to do... sorry.. was realy my mistake... sorry.
    (i did a test with a small code)
    heres the code correted:
    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 || inst == NULL)
                MessageBox(NULL, "null", "null", MB_OK);
    
    
            switch(msg)
            {
    
                case WM_CREATE:
                {
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
                    SetWindowText(GetParent(hwnd),"i'm alive my friend");
                    inst->Create(xPos,yPos);
                }
                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)
        {
            Move=[](int x, int y)
            {
                ;
            };
    
            Create=[](int x, int y)
            {
                ;
            };
    
            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;
        }
    };
    now i see that before create the control, i must change it's events and then change the parent(create the control) for works correctly
    thanks for all to all
    (if i didn't said)
    happy a new year to all

  9. #69
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by Cambalinho View Post
    but...
    The point being is that you should always check return codes. It doesn't matter if you are confident that it will work -- you always check return values if the function says what is returned on failure (or success).

    The other reason is that we are looking at your code -- there is no way we can tell if the call worked or not. An explicit check for the return value not only helps you, it helps others looking at your code.

    Regards,

    Paul McKenzie

  10. #70
    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

    Code:
     char *lbltext;
    GetWindowText(hwnd,lbltext,255);
    NO!! You have not created the buffer for GetWindowText - merely defined lbltext as a pointer to somewhere unknown! Use

    Code:
    char lbltext[256];
    Code:
    void SetText(string text)
        {
            char* chrText = (char*)text.c_str();
            SetWindowText(hwnd, chrText);
        }
    This would be better as
    Code:
    void SetText(const string& text)
        {
            SetWindowText(hwnd, text.c_str());
        }
    Last edited by 2kaud; December 30th, 2013 at 05:09 PM.
    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. #71
    Join Date
    Apr 2009
    Posts
    1,355

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

    Quote Originally Posted by Paul McKenzie View Post
    The point being is that you should always check return codes. It doesn't matter if you are confident that it will work -- you always check return values if the function says what is returned on failure (or success).

    The other reason is that we are looking at your code -- there is no way we can tell if the call worked or not. An explicit check for the return value not only helps you, it helps others looking at your code.

    Regards,

    Paul McKenzie
    thanks for all my friend.
    i will continue with these thread open, until complete these class. thanks for all

  12. #72
    Join Date
    Apr 2009
    Posts
    1,355

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

    Quote Originally Posted by 2kaud View Post
    Code:
     char *lbltext;
    GetWindowText(hwnd,lbltext,255);
    NO!! You have not created the buffer for GetWindowText - merely defined lbltext as a pointer to somewhere unknown! Use

    Code:
    char lbltext[256];
    thanks for correct me... thanks
    why i can't use the 2 constant's variables inside the class?
    const char *labelpropname = "Cambalinho";
    const char *labelclassprop = "classaddr";

  13. #73
    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
    why i can't use the 2 constant's variables inside the class?
    const char *labelpropname = "Cambalinho";
    const char *labelclassprop = "classaddr";
    Because only const static integral data members can be initialized inside a class or struct - basially char, bool and int (and the short/long variations). See http://msdn.microsoft.com/en-us/library/cc953fe1.aspx
    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)

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

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

    Quote Originally Posted by 2kaud View Post
    Because only const static integral data members can be initialized inside a class or struct - basially char, bool and int (and the short/long variations). See http://msdn.microsoft.com/en-us/library/cc953fe1.aspx
    thanks for all

  15. #75
    Join Date
    Apr 2009
    Posts
    1,355

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

    see these lines:

    Code:
    label label1(hwnd);
        label1.MouseDown=[](int Button, bool alt, bool shift,int x, int y)
        {
            if(alt==true  && Button==VK_LBUTTON) label1.SetText("mouse left down");//error message
        };
    what means these error:
    "'label1' is not captured|"
    ???

Page 5 of 11 FirstFirst ... 2345678 ... 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