CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] how use GetProp() and SetProp()?

  1. #1
    Join Date
    Apr 2009
    Posts
    1,298

    [RESOLVED] how use GetProp() and SetProp()?

    i'm trying use the GetProp() and SetProp() by my own, but i don't get results
    see these function:
    Code:
    void setParent(HWND parent=GetDesktopWindow())
            {
                if (hwnd==NULL)
                {
    
                    WNDCLASSEX FormClass;
                    char classname[20]="Form";
                    sprintf(classname,"%s",strCaption.c_str());
                    HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
    
                    FormClass.cbSize        = sizeof(WNDCLASSEX);
                    FormClass.style         = CS_DBLCLKS;
                    FormClass.lpfnWndProc   = WndProcForm;
                    FormClass.cbClsExtra    = 0;
                    FormClass.cbWndExtra    = 0;
                    FormClass.hInstance     = mod;
                    FormClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
                    FormClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
                    FormClass.hbrBackground = (HBRUSH)GetSysColor(COLOR_BACKGROUND);
                    FormClass.lpszMenuName  = NULL;
                    FormClass.lpszClassName = classname;
                    FormClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
                    // register the new window class
                    RegisterClassEx(&FormClass);
    
                    hwnd = CreateWindowEx(WS_EX_LAYERED,classname, strCaption.c_str(),WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN ,
                                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, parent, NULL, mod, this);
    
                    SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
                    if (hwnd == NULL)
                        MessageBox(NULL, "Can't create the control", "error", MB_OK);
                    SetProp(parent, "parent", (HANDLE)parent);//creates? and sets property
                }
                else
                {
                    SetProp(parent, "parent", (HANDLE)parent);//sets the property
                    SetParent(hwnd,parent);
                }
    
                ShowWindow(hwnd, SW_NORMAL);
                InvalidateRect(hwnd,NULL,TRUE);//i add these line for fix that
                UpdateWindow(hwnd);
                clrBackColor =GetDCBrushColor(GetDC(hwnd));
                clrTextColor = GetTextColor(GetDC(hwnd));
                if(GetLastError()!=0)
                    MessageBox(NULL,to_string(GetLastError()).c_str(),"error", MB_OK);
            }
    and heres i use it:
    Code:
    form InputBox("hello",a);
    the HWND of form a must go to "parent" property.
    and heres how i get the value:
    Code:
    CenterInsideParent(InputBox,(HWND)GetProp(InputBox, "parent"));
    i didn't test the return value, but i have sure that isn't the HWND of a.
    what i'm doing wrong?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,734

    Re: how use GetProp() and SetProp()?

    Quote Originally Posted by Cambalinho View Post
    i didn't test the return value, but i have sure that isn't the HWND of a.
    what i'm doing wrong?
    Please, next time ask your questions after you have tested the return values of all API functions you use.
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2009
    Posts
    1,298

    Re: how use GetProp() and SetProp()?

    Quote Originally Posted by VictorN View Post
    Please, next time ask your questions after you have tested the return values of all API functions you use.
    but i did these test:
    Code:
    CenterInsideParent(InputBox,a);
    and works fine.
    i test these and the message box is showed:
    Code:
    if(SetProp(parent, "parent", (HANDLE)parent)==TRUE)
                        MessageBox(NULL,"done","done", MB_OK);
    i tested these and the messagebox give me 0:
    Code:
    if(GetProp(InputBox, "parent")==NULL)
                    MessageBox(NULL,to_string(GetLastError()).c_str(),"done", MB_OK);
    so what isn't wright?

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

    Re: how use GetProp() and SetProp()?

    Code:
    SetProp(parent, "parent", (HANDLE)parent);//creates? and sets property
    This doesn't seem to make sense. You are setting the value of property "parent" of a window to itself???????

    Don't you mean
    Code:
    SetProp(hwnd, "parent", (HANDLE)parent);//creates? and sets property
    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++17 Compiler: Microsoft VS2019 (16.6.3)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,298

    Re: how use GetProp() and SetProp()?

    Quote Originally Posted by 2kaud View Post
    Code:
    SetProp(parent, "parent", (HANDLE)parent);//creates? and sets property
    This doesn't seem to make sense. You are setting the value of property "parent" of a window to itself???????

    Don't you mean
    Code:
    SetProp(hwnd, "parent", (HANDLE)parent);//creates? and sets property
    thanks for all

    sorry the question, but let me ask: how can i avoid these type of errors?(i don't mean only in programming... but in life too)

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,302

    Re: how use GetProp() and SetProp()?

    Quote Originally Posted by Cambalinho View Post
    thanks for all

    sorry the question, but let me ask: how can i avoid these type of errors?
    You can avoid these types of errors by doing what Victor suggested and check the return codes for the api's you call.

    And by checking the return codes, I mean really check the return codes.

    Consider your code snippet.
    Code:
    hwnd = CreateWindowEx(WS_EX_LAYERED,classname, strCaption.c_str(),WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN ,
                                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, parent, NULL, mod, this);
    
                    SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
                    if (hwnd == NULL)
                        MessageBox(NULL, "Can't create the control", "error", MB_OK);
                    SetProp(parent, "parent", (HANDLE)parent);//creates? and sets property
    In the snippet above, if CreateWindowEx fails, you still call SetLayeredWindowAttibutes(), then display an error and continue calling SetProp().

    Calling multiple functions after hitting an error isn't what I would consider a good error handling strategy.

  7. #7
    Join Date
    Apr 2009
    Posts
    1,298

    Re: how use GetProp() and SetProp()?

    Quote Originally Posted by Arjay View Post
    You can avoid these types of errors by doing what Victor suggested and check the return codes for the api's you call.

    And by checking the return codes, I mean really check the return codes.

    Consider your code snippet.
    Code:
    hwnd = CreateWindowEx(WS_EX_LAYERED,classname, strCaption.c_str(),WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN ,
                                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, parent, NULL, mod, this);
    
                    SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
                    if (hwnd == NULL)
                        MessageBox(NULL, "Can't create the control", "error", MB_OK);
                    SetProp(parent, "parent", (HANDLE)parent);//creates? and sets property
    In the snippet above, if CreateWindowEx fails, you still call SetLayeredWindowAttibutes(), then display an error and continue calling SetProp().

    Calling multiple functions after hitting an error isn't what I would consider a good error handling strategy.
    sorry, but question was about miss between parent and hwnd. like you have seen, i error because i use the parent instead the hwnd, by mistake.
    thanks for all

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




On-Demand Webinars (sponsored)