4

I have a Windows Forms application that I use the following code to dock it to the lower-right side of the screen when it is launched:

protected override void OnLoad(EventArgs e)
{
    var s = Screen.FromPoint(this.Location);
    this.Location = new Point(s.WorkingArea.Right - this.Width,
                              s.WorkingArea.Height - this.Height);
    base.OnLoad(e);
}

When I am in Visual Studio debug mode, this works like a charm. When I build an installer and install the application (on the same dev box) and run it, the coordinates are slightly different and the form is not perfectly docked in the corner. So I stuck this in the OnLoad:

MessageBox.Show(this.Location.X + "x" + this.Location.Y);

In development mode it displays 1355x720 but when I install it displays 1365x730

Why the discrepency? Obviously I could just subtract 10 for my code to work but I would like an explanation for this behavior.

Update:

  1. This machine is Windows 8.1 Pro 64-bit with multiple monitors.
  2. It is the form size that is different (not Screen.WorkingArea). In development mode the form size shows 245x140 but when I install it shows 235x130
  3. I am also unable to give the form focus. There is a textbox on the form that receives focus onload. It has the blinking caret, but I cannot type until I physically click on the textbox. This again only happens when I install it; works fine when run from VS. I suspect this is related to the form position going off the screen edge.
  4. The form has TopMost set to True
Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
  • 3
    It's probably Aero, which lies about the external width and height of the form. Setting the border style to none should fix it, but then you don't have borders. :-) – LarsTech Apr 08 '16 at 15:05
  • You are correct that if I use FormBorderStyle=None the issue goes away. I have to think there is a better solution for this problem. Can I get @Hans to the rescue? – Josh Stodola Apr 08 '16 at 15:20
  • 1
    There is a post about snapping forms to a screen edge from Hans, don't know if this is of any use to you: http://stackoverflow.com/questions/589268/how-to-make-my-windows-form-app-snap-to-screen-edges – Equalsk Apr 08 '16 at 15:39
  • @Equalsk no, it is doing the same thing my code is doing, except instead of setting `Location` it sets `Left` and `Top`. I suspect that snap code would also be off by 10 pixels on this machine. – Josh Stodola Apr 08 '16 at 15:49
  • The different-after-install angle is very hard to explain. The only cue I see is "multiple monitors", Win8.1 supports per-monitor DPI. Virtualized unless you declare you app to be compatible with it in the manifest. Which is not easy to implement. But that can only throw things off when you do this on the second monitor, you would have mentioned that. – Hans Passant Apr 08 '16 at 16:23
  • @Hans Correct, this is happening on the first monitor. I just tried unplugging the 2nd monitor and the form still bleeds off the right edge exactly ten pixels. Very strange. I also set AutoScaleMode to None on the form and that didn't help. – Josh Stodola Apr 08 '16 at 16:38
  • 1
    There's a seemingly related question [on MSDN](https://social.msdn.microsoft.com/Forums/vstudio/en-US/a7f2f6cf-26fd-47de-afde-a7a2d975fdea/on-any-form-border-width-is-bigger-when-the-debugger-is-on?forum=vbgeneral). And also [a question](http://stackoverflow.com/questions/15136564/winforms-wrong-form-size) on here – Stuart Whitehouse Apr 08 '16 at 19:24
  • @StuartWhitehouse yes it certainly seems to be a bug! I am still getting nowhere. I ended up inserting a pitiful and shameful statement `if(Debugger.IsAttached)` – Josh Stodola Apr 27 '16 at 14:43

0 Answers0