Thread: Updating control position

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Updating control position

    If I have a button control on a window, say MyButton, and I want to change its position to the left by a fixed amount from its current position. In pseudo terms:
    Code:
    MyButton.x = MyButton.x + amount;
    Naturally I thought using a GetWindowRect()/SetWindowPos() combo would be the way, but in classic winApi fashion, Windows has gotten everything mixed up again: apparently SetWindowPos operates on client co-ordinates (upper left of parent window is origin) whereas GetWindowRect operates on screen co-ordinates.
    So what function(s) would I use to get MyButton's current x position and increase by a certain amount?

    I've even tried compensating for the difference between client and screen coordinates by subtracting the button's left value by its window's left value to try to obtain its relative client coordinates, but alas its not as easy as this... I mean c'mon... how hard is it to make a GetWindowPos function to complement SetWindowPos' use of client co-ordinates.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Thanks, I'll try that out.
    I wonder why the API makes this process so cumbersome though? Why not have something like a GetWindowPos(hwnd, UINT, rect) that when given the parent window handle, child window identifier and a rect, retrieves the child window's position in client co-ordinates...

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It is ridiculous, you would at least think this problem would be solved in other Microsoftilized languages like Visual Basic? But it's the same story... they make you do half the work your self, while they do the other half.

    But you could merely make your own, and use it in your projects. Get the parent window rect (sigh), then relate the client window rect to the parent window rect to the screen, yarr :@

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    void GetChildPos(HWND hwndParent, HWND hwndChild, RECT* prc)
    {
    	GetWindowRect(hwndChild, prc);
    
    	ScreenToClient(hwndParent, (POINT*) &prc->left)
    	ScreenToClient(hwndParent, (POINT*) &prc->right);
    }
    
    void GetDlgItemPos(HWND hwndDlg, INT controlID, RECT* prc)
    {
    	GetChildPos(hwndDlg, GetDlgItem(hwndDlg, controlID), prc);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  2. Rich Edit Control - Get And Set Caret Position?
    By Charles01 in forum Windows Programming
    Replies: 1
    Last Post: 12-05-2005, 10:26 AM
  3. Updating a list control
    By MPSoutine in forum Windows Programming
    Replies: 2
    Last Post: 12-05-2003, 02:03 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Updating Static Control Color Real-Time :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-29-2002, 03:09 PM