SetWindowPos doesn't seem to work

This is a discussion on SetWindowPos doesn't seem to work within the Windows Programming forums, part of the Platform Specific Boards category; Why doesn't SWP work? I'm trying to make sure that all the child windows for my custom control aren't over ...

  1. #1
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293

    SetWindowPos doesn't seem to work

    Why doesn't SWP work? I'm trying to make sure that all the child windows for my custom control aren't over the top of the caption by moving them to an y offset of a minimum of 26 if they aren't already greater than that.

    Code:
    BOOL CALLBACK EnumProc(HWND hwnd, LPARAM lParam)
    {
      RECT crc;
      RECT rc;
      GetWindowRect(GetParent(hwnd),&rc);
      GetWindowRect(hwnd,&crc);
      if(crc.top < rc.top+25)
      {
        SetWindowPos(hwnd,HWND_TOPMOST,crc.left,rc.top+26,0,0,SWP_SHOWWINDOW|SWP_NOSIZE);
        UpdateWindow(hwnd);
      }
    }
    It's just not doing anything. SetWindowPos is being called but the child window I'm testing with is still at 0,0 ..

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    Are you sure you are using the right HWND?

    this should be

    UpdateWindow(GetParent(hwnd));

    to update the window not the control.

    Try taking the SHOW off . I had problems with SetWindowPos() if I tried to make it do more than one thing at a time.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    I tried both of those things and neither worked.

    Also, I tried just SWP_HIDEWINDOW and the window did not hide.

    [edit:] Woah, crazy! For some reason everytime I run the application it moves the button more and more! Like, it's almost as if it's remembering the position and then incrementing!
    Code:
    RECT crc;
      RECT rc;
      GetWindowRect(GetParent(hwnd),&rc);
      GetWindowRect(hwnd,&crc);
      if(crc.top < rc.top+25)
      {
        MoveWindow(hwnd,crc.left,crc.top,crc.right-crc.left,rc.top+26,TRUE);
        UpdateWindow(GetParent(hwnd));
      }
    Last edited by Mithoric; 12-18-2003 at 01:43 AM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    GetWindowRect() returns screen coordinates and SetWindowPos() expects client coordinates.

    When using MoveWindow(), if the window being moved is a child window, then it expects client coordinates in terms of the parent.

    Use ScreenToClient() and ClientToScreen() for conversions to and from screen and client coordinates.

    gg

  5. #5
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    Ok, with this code it is supposed to minimize my window to just the caption. How would I do it?
    Code:
    RECT rc;
            RECT prc;
            GetWindowRect(GetParent(hwnd),&prc);
            GetWindowRect(hwnd,&rc);
            if(status != 3)
            {
                    status = 3;
                    oldHeight = rc.bottom-rc.top;
                    MoveWindow(hwnd,rc.left,rc.top,rc.right-rc.left,23,TRUE);
            }else{
                    status = 0;
                    MoveWindow(hwnd,rc.left,rc.top,rc.right-rc.left,oldHeight,TRUE);
            }
    I tried making it in client coords by putting rc.left and rc.top into a point structure but it was just a waste of time because it did worse by just always going to 0,0. I would usually do rc.left-prc.left where prc would be the parent rect to get the coords but for some reason it doesn't always work.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Originally posted by Mithoric
    I tried making it in client coords ... but ... it did worse by just always going to 0,0.
    Since you're using MoveWindow() on a child, you must convert to the parent's client coords.
    Post your effort so others can "debug" the issue.

    gg

  7. #7
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    Okely dokely, here is what I tried ( I think )
    Code:
    RECT rc;
            RECT prc;
            POINT p1,p2;
            GetWindowRect(GetParent(hwnd),&prc);
            GetWindowRect(hwnd,&rc);
            p1.x = rc.left;
            p1.y = rc.top;
            p2.x = prc.left;
            p2.y = prc.top;
            ScreenToClient(hwnd,&p1);
            ScreenToClient(GetParent(hwnd),&p2);
            if(status != 3)
            {
                    status = 3;
                    oldHeight = rc.bottom-rc.top;
                    MoveWindow(hwnd,p1.x,p1.y,rc.right-rc.left,23,TRUE);
            }else{
                    status = 0;
                    MoveWindow(hwnd,p1.x,p1.y,rc.right-rc.left,oldHeight,TRUE);
            }
            UpdateWindow(hwnd);
            return 0;

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    >>ScreenToClient(hwnd,&p1);

    This should be the parents HWND. You want both in the same 'client' area.

    >.MoveWindow(hwnd,p1.x,p1.y,rc.right-rc.left,oldHeight,TRUE);

    is oldHeight static or does it get a value somewhere outside the code posted? That is, does it always get a valid value before it is used.

    If this does not work, try getting the client rect for the parent, convert the top left point to screen coods.
    Get the window rect and work out an offset ie the amount from the topleft corner of the window to the topleft of the client area. This offset contains the menu, toolbar and titlebar. (NOTE: sometimes the toolbar is included in the client area)
    Last edited by novacain; 12-19-2003 at 12:06 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    Excellent, it works. I understand how the ScreenToClient() works now, converting the coords of the POINT to coords relative to the parent. . . I'm a little slow .

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    >>I'm a little slow

    That must be dangerous in Bathurst...........(Mt Panorama and all)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21