none
How to find absolute screen coordinates of "this"?

    Question

  • How can I find the absolute screen coordinates of my custom control from inside my control?

    So I basically need to find the screen coordinates of "this" (in c#).  I've tried:

    Point popupLocationFromScreen = this.PointToScreen(new Point(0, 0));

    No luck.  Ideas? 
    Friday, September 05, 2008 5:12 PM

Answers

  • OnApplyTemplate might be too early - I'm not sure if the elements in the template have been laid out at that point. Probably not. Tried LayoutUpdated?
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    • Marked as answer by Kofoed Tuesday, September 09, 2008 4:23 AM
    Monday, September 08, 2008 5:17 PM

All replies

  • Here is one method use interop and  native  API ClientToScreen.
    http://blogs.msdn.com/llobo/archive/2006/05/02/Code-for-getting-screen-relative-Position-in-WPF.aspx

    Hope this helps.

    Yiling, MVP(Visual C++)
    Saturday, September 06, 2008 3:05 AM
  • You say no luck, but your code appears as though it should work ok. You should get the location in WPF screen coordinates.

    Did you get a NoLuckException, or can you elaborate?
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    Sunday, September 07, 2008 9:34 PM
  • The only thing I can think of that might cause PointToScreen not to work would be if your control has not been laid out on screen yet.  Are you trying to do this while your control is being constructed?  PointToScreen will throw an exception at that point.  If you could move the logic that requires knowledge of the control's location into the control's Loaded event handler, PointToScreen should succeed at that point.


    Eric
    Monday, September 08, 2008 1:31 AM
  • Well, not a "noluckexception" ... in fact, no exception at all!  Basically it just returns a Point with both the x and y coordinates zero.  I'm attempting to calculate this inside the OnApplyTemplate method override in my control - reason being I want to be sure the template has been applied so I can perform the screen coordinates check and adjust some UI elements if necessary.

    Wrong place?
    Monday, September 08, 2008 3:31 PM
  • OnApplyTemplate might be too early - I'm not sure if the elements in the template have been laid out at that point. Probably not. Tried LayoutUpdated?
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    • Marked as answer by Kofoed Tuesday, September 09, 2008 4:23 AM
    Monday, September 08, 2008 5:17 PM
  • bingo ... I think LayoutUpdated is what I'm looking for, you're right - the OnApplyTemplate is too early. 
    Tuesday, September 09, 2008 4:23 AM