如何在WPF中在屏幕上绘制矩形边框?

时间:2020-05-03 12:42:37

标签: c# wpf winforms

我正在创建用于屏幕选择的WPF应用程序。我正在尝试在屏幕上绘制一个红色的矩形框。在主显示器上绘画时,我看不到任何问题,选择范围内。我看到辅助显示器出现问题,左侧和顶部都看不到。

这是我的代码:

      monitor_index is nothing but the index of the screen selection. 
       { System.Windows.Forms.Screen screen = System.Windows.Forms.Screen.AllScreens[Convert.ToInt32(monitor_index)];

                var StartPoint = new System.Drawing.Point(0, 0);


 var EndPoint = new System.Drawing.Point(screen.Bounds.Width, screen.Bounds.Height);
            selectedAreaControl = new Form
            {
                TopMost = true,
                ShowInTaskbar = false,
                BackColor = Color.LightGreen,
                TransparencyKey = Color.LightGreen,
                FormBorderStyle = FormBorderStyle.None,
                Name="redborder"

            };
            selectedAreaControl.Paint += new PaintEventHandler(OnSelectedAreaPaint);
            selectedAreaControl.StartPosition = FormStartPosition.Manual;
            selectedAreaControl.Location = screen.Bounds.Location;
            selectedAreaControl.Bounds = new Rectangle(selectedAreaControl.Location, new Size((EndPoint.X - StartPoint.X), (EndPoint.Y - StartPoint.Y)));
            selectedAreaControl.Left = selectedAreaControl.Location.X + StartPoint.X;
            selectedAreaControl.Top = selectedAreaControl.Location.Y + StartPoint.Y;
            selectedAreaControl.Show();
    }


        private void OnSelectedAreaPaint(object sender, PaintEventArgs e)
                {
                Rectangle rect = selectedAreaControl.ClientRectangle;
                Pen pen = new Pen(new SolidBrush(borderColor), borderWidth);
                e.Graphics.DrawRectangle(pen, rect);
            }

查看此内容:在这里,您可以看到最左边和最左边的边框不可见。 enter image description here

这是我在主屏幕上时的样子。 enter image description here

0 个答案:

没有答案