locked
Absolute screen coordinates of WPF user control RRS feed

  • Question

  •  

    Hi,

     

    How can I get the absolute screen coordinates of the top left corner of a WPF user control please?

     

    The hierarchy is:

    Windows Forms Applciation > Main Form > Element Host > WPF User Control by the way.

     

    Thank you very much,

    Patrick

     

    Friday, May 9, 2008 6:38 AM

Answers

  • Hi,

     

    thanks you all for your input. I got it working now . In case anybody else needs it:

     

    Code Snippet

    // Get absolute location on screen of upper left corner of button

    Point locationFromScreen = this.button1.PointToScreen(new Point(0, 0));

    // Transform screen point to WPF device independent point

    PresentationSource source = PresentationSource.FromVisual(this);

    System.Windows.Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(locationFromScreen);

    // Set coordinates

    Window wpfWindow = new Window2();

    wpfWindow.Top = targetPoints.Y;

    wpfWindow.Left = targetPoints.X;

    wpfWindow.Show();

     

     

    All the best,

    Patrick

    Monday, May 12, 2008 1:38 AM
  • Hi Pat,

     

    even though coordinates are not so obvious they do exsit.

     

    Code Snippet

    Point locationFromWindow = Button1.TranslatePoint(new Point(0, 0), this);

    Point locationFromScreen = Button1.PointToScreen (locationFromWindow);

     

     

    Benjamin

     

    Friday, May 9, 2008 11:47 AM

All replies

  • Hi Pat,

     

    even though coordinates are not so obvious they do exsit.

     

    Code Snippet

    Point locationFromWindow = Button1.TranslatePoint(new Point(0, 0), this);

    Point locationFromScreen = Button1.PointToScreen (locationFromWindow);

     

     

    Benjamin

     

    Friday, May 9, 2008 11:47 AM
  • Ok thanks that worked. Now to the actual task I wanted to accomplish. Open a new WPF window whoes top left corner is exactly over the top left corner of my current wpf control.

    So how would I open the WPF Window at this location please:

    If I just set:
    wpfWindow.Top = locationFromScreen.Y;
    wpfWindow.Left =
    locationFromScreen.X;
    wpfWIndow.Show();

    It doesn't open at the right position.

    If I do this it doesn't open at the right position:
    PresentationSource source = PresentationSource.FromVisual(this);
    System.Windows.Point targetPoints = source.CompositionTarget.TransformToDevice.Transform(
    locationFromScreen);
    wpfWindow.Top = targetPoints.Y;
    wpfWindow.Left =
    targetPoints.X;
    wpfWIndow.Show();


    So what do I do please?

    Thanks,
    Patrick
    Friday, May 9, 2008 6:52 PM
  • Try it this way:

     

     

    Code Snippet

    // get's the screen- coordinates of the upper left corner of the button

    Point locationFromScreen = Button1.PointToScreen(new Point(0, 0));

     

    // set the coordinates for the new window

    wpfWindow.Top = locationFromScreen.Y;

    wpfWindow.Left = locationFromScreen.X;

     

     

    Friday, May 9, 2008 11:23 PM
  • Hi,

     

    thanks you all for your input. I got it working now . In case anybody else needs it:

     

    Code Snippet

    // Get absolute location on screen of upper left corner of button

    Point locationFromScreen = this.button1.PointToScreen(new Point(0, 0));

    // Transform screen point to WPF device independent point

    PresentationSource source = PresentationSource.FromVisual(this);

    System.Windows.Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(locationFromScreen);

    // Set coordinates

    Window wpfWindow = new Window2();

    wpfWindow.Top = targetPoints.Y;

    wpfWindow.Left = targetPoints.X;

    wpfWindow.Show();

     

     

    All the best,

    Patrick

    Monday, May 12, 2008 1:38 AM
  • Hi Patric,

    Thanks for you help. It really help me.

    Nivas D
    Thursday, December 17, 2009 7:17 AM
  • Nice, Thank you. helped me in other requirement.
    Wednesday, February 1, 2012 10:20 PM
  • Hello Patrick,

        Thanks for sharing the final solution. Appreciate it!

    From,

    Patrick

    Thursday, July 30, 2015 12:48 PM