2

I have three monitors in the following configuration:

Monitor 1 Monitor 2 Monitor 3 [1280 x 1024] [1200 x 1900] [1280 x 1024]

I use a form to outline a selected monitor (code below), which works great when I initialize or change between Monitors 1 and 3, but results in two rectangles drawn when Monitor 2 is selected :/

I tried to modify the code in many-many different ways, but nothing seems to work. I thought someone might be able to help me understand why two rectangles are being drawn (1280 x 1024 and 1200 x 1900) and how to correct.

Thank you for your time, regards and happy New Year.

P.S. If possible, please keep explanation simple as I am still learning.

public partial class ScreenArea : Form
{
    private Pen _pen;
    private int screenSelect;

    public ScreenArea(int selectScreen = 0)
    {
        //xInitializeComponent();
        TopMost = true;
        ShowInTaskbar = true;
        FormBorderStyle = FormBorderStyle.None;
        BackColor = Color.LightGreen;
        TransparencyKey = Color.LightGreen;
        _pen = new Pen(Color.Aqua, 5);
        Paint += new PaintEventHandler(ScreenArea_Paint);
        ScreenSelect = screenSelect;
    }

    private void ScreenArea_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawRectangle(_pen, 0, 0, Width, Height);
    }

    public int ScreenSelect
    {
        get
        {
            return screenSelect;
        }
        set
        {
            Rectangle screenBounds;
            try
            {
                screenBounds = Screen.AllScreens[value].Bounds;
                screenSelect = value;
            }
            catch (Exception)
            {
                screenBounds = Screen.AllScreens[screenSelect].Bounds;
            }
            this.Left = screenBounds.X;
            this.Top = screenBounds.Y;
            this.Width = screenBounds.Width;
            this.Height = screenBounds.Height;
        }
    }
}
ChE Junkie
  • 326
  • 2
  • 9
  • 2
    Read the notes [here](https://stackoverflow.com/a/53026765/7444103) about the VirtualScreen (there's a section related to DpiAwareness, don't miss it). At the bottom, a piece of code to get the DC of each display, in case it's needed. Remember to `Invalidate()` the Form to repaint it. Btw, it's usually better to override `OnPaint` in these kind of implementations rather than subscribe to the Paint event. – Jimi Jan 01 '20 at 21:36
  • Perfect, thank you. Lots to play with :-) – ChE Junkie Jan 02 '20 at 00:57

0 Answers0