56

Let's say I have a Control and its location is relative to its parent. If its embedded many times and is the great great great grandchild of the main form, how can I determine what its location is on my entire screen, not just its location in the immediate parent? This is for the purpose of printing a particular Control via a screenshot (since for some controls the DrawToBitmap doesn't work properly).

BSMP
  • 4,596
  • 8
  • 33
  • 44
sooprise
  • 22,657
  • 67
  • 188
  • 276

1 Answers1

122

You're looking for the PointToScreen method:

Point location = someControl.PointToScreen(Point.Empty);
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 4
    Note that PointToScreen takes _client area_ coordinates, so using this code on a Form for instance would not give the correct result. – rymdsmurf Feb 18 '15 at 10:49
  • 4
    @rymdsmurf not sure if I understand you correctly, but if you use it on a `Form` (like `myForm.PointToScreen`) and you have the right coordinates (from `control.Location`) it will give you the right coordinates on the screen. – Artholl May 04 '15 at 08:06
  • 3
    As a note: make sure your screens are scaled to 100% to get correct values. Mine was 125% and I was getting wrong values. – Bitterblue May 30 '18 at 07:26
  • 1
    How to take scaling into consideration? – pookie Oct 19 '18 at 12:18
  • 3
    @pookie you can get a Graphics object (Dim g As Graphics = CreateGraphics()) and use (g.DpiX / 96) to get the scaling factor. It ill be 1 for 100% and 1.5 for 150%. – Richard Moore Nov 07 '18 at 16:34