CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2010
    Posts
    67

    How to get a control position?

    So I want to move a control from a dialog box to some position and then move it back to it's initial position but, how do I get it's initial position? I tried ScreenToClient, MapWindowPoints, etc with no sucess. The first movement I know how to do, its going to be at the dialog's .left and .top.

    Thanks.

  2. #2
    Join Date
    Dec 2004
    Location
    Ann Arbor, MI
    Posts
    281

    Re: How to get a control position?

    Call GetWindowRect() on your control to get its bounding rectangle in screen coordinates, then call ScreenToClient() on that rect from your dialog to translate into dialog coordinates.
    --EJMW

  3. #3
    Join Date
    Oct 2010
    Posts
    67

    Re: How to get a control position?

    I already tried that, this code makes the first movement right, but the last one that's supposed to take it back to the initial spot takes it to the top left corner of the dialog.
    Code:
      RECT rc, rc2;
      POINT p;
      GetClientRect(hPic, &rc);
      GetWindowRect(hPic, &rc2);
      int width = rc.right - rc.left, height = rc.bottom - rc.top;
      p.x=rc2.left;
      p.y=rc2.top;
      ScreenToClient(hPic, &p);
    
    if(something){
    MoveWindow(hPic, rc.left+4, rc.top+4, width, height, 1);
    }
    
    else if(something{
    MoveWindow(hPic, p.x, p.y, width, height, 1);
    }

  4. #4
    Join Date
    Dec 2004
    Location
    Ann Arbor, MI
    Posts
    281

    Re: How to get a control position?

    Code:
    ScreenToClient(hPic, &p);
    As I said, you need to call ScreenToClient from your dialog. You're calling it with the handle of your control.

    Re-reading my post, I realize that wasn't very clear...sorry about that.
    --EJMW

  5. #5
    Join Date
    Oct 2010
    Posts
    67

    Re: How to get a control position?

    Thank you, and I realized that it was my mistake, it was a function which the first movement would overwrite the position.

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