SelectArea Form height and width doesn't change in second monitor in different resolution in C#

1

The first page of code would create a draggable and moveable transparent form for the user to select the area to capture a screenshot here is select area. When I capture the primary screen, all is good. However, when I drag the form to a secondary screen, the capture area is not the same with the intended area. Expected image what I expect the screenshot to show on secondary monitor, and but the result is liked that. And I got a monitor orientation liked that the second monitor on top of primary one. And I have to consider all different configuration such as monitors side by side, etc. Thanks.

using System;
using System. Drawing;
using System.Windows.Forms;

namespace SelectArea
{
    public partial class SelectArea : Form
    {

        public SelectArea()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None; // no borders
            this.DoubleBuffered = true;
            this.Opacity = .5D;
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            Screen[] screens = Screen.AllScreens;


        }
        int thickness = 10;




        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle Top =new Rectangle(0, 0, this.ClientSize.Width, thickness);
            Rectangle Left = new Rectangle(0, 0, thickness, this.ClientSize.Height);
            Rectangle Bottom =new Rectangle(0, this.ClientSize.Height-thickness, this.ClientSize.Width, thickness);
            Rectangle Right = new Rectangle(this.ClientSize.Width-thickness, 0, thickness, this.ClientSize.Height);
            e.Graphics.FillRectangle(Brushes.DarkCyan, Top);
            e.Graphics.FillRectangle(Brushes.DarkCyan, Left);
            e.Graphics.FillRectangle(Brushes.DarkCyan, Right);
            e.Graphics.FillRectangle(Brushes.DarkCyan, Bottom);
        }

        private const int HT_CLIENT = 0x1;
        private const int HT_CAPTION = 0x2;
        private int monitor_check;
        protected override void WndProc(ref Message m)
        {
            Screen screen = Screen.FromControl(this);
            if (m.Msg == 0x84)
            {  // Trap WM_NCHITTEST
                //int x = (short)(m.LParam.ToInt32() & 0x0000FFFF);
                //int y = (short)((m.LParam.ToInt32() & 0xFFFF0000) >> 16);
                //Point pos = new Point(x, y);
                Point pos = new Point(m.LParam.ToInt32());
                pos = this.PointToClient(pos);

                if (pos.X < thickness)
                {
                    monitor_check = 0;
                    if (pos.Y < thickness)
                    {
                        m.Result = (IntPtr)13;  // TOPLEFT

                    }
                    else if (pos.Y > thickness && pos.Y < this.ClientSize.Height - thickness)
                    {
                        m.Result = (IntPtr)10; //LEFT


                    }
                    else if (pos.Y > this.ClientSize.Height - thickness && pos.Y < this.ClientSize.Height)
                    {
                        m.Result = (IntPtr)16;//BOTTOMLEFT

                    }
                    return;
                }
                else if (pos.X > thickness && pos.X < this.ClientSize.Width -  thickness)
                {
                    monitor_check = 0;
                    if (pos.Y < thickness)
                    {
                        m.Result = (IntPtr)12;//TOP
                    }
                    else if (pos.Y > this.ClientSize.Height - thickness & pos.Y < this.ClientSize.Height)
                    {
                        m.Result = (IntPtr)15;//BOTTOM
                    }
                    else if (pos.Y > thickness && pos.Y < this.ClientSize.Height - thickness)
                    {
                        m.Result = (IntPtr)2;//TITLEBAR 



                    }
                    return;
                }
                else if (pos.X > this.ClientSize.Width - thickness && pos.X < this.ClientSize.Width)
                {
                    monitor_check = 0;
                    if (pos.Y < thickness)
                    {
                        m.Result = (IntPtr)14;  // TOPRIGHT

                    }
                    else if (pos.Y > thickness && pos.Y < this.ClientSize.Height - thickness)
                    {
                        m.Result = (IntPtr)11; //RIGHT


                    }
                    else if (pos.Y > this.ClientSize.Height - thickness && pos.Y < this.ClientSize.Height)
                    {
                        m.Result = (IntPtr)17;//BOTTOMRIGHT

                    }
                    return;

                }


            }
            base.WndProc(ref m);
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            Save_Screenshot save = new Save_Screenshot(this.Location.X, this.Location.Y, this.Size.Width, this.Size.Height, this.Size);

        }


    }
}


public partial class Save_Screenshot : Form
    {
        Bitmap bmp;
        PictureBox pb = new PictureBox();

        public Save_Screenshot(Int32 x, Int32 y, Int32 w, Int32 h, Size s)
        {
            InitializeComponent();

            Rectangle rect = new Rectangle(x, y, w, h);
            bmp = new Bitmap(rect.Width, rect.Height,PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(bmp);
            g.CopyFromScreen(rect.Left, rect.Top, 0, 0, s, CopyPixelOperation.SourceCopy);
            Console.WriteLine(rect.Left.ToString(), rect.Top.ToString(), 0, 0, s);

            bmp = FilterImg(bmp);

            this.StartPosition = FormStartPosition.CenterScreen;
            this.Size = bmp.Size;

            pb.Dock = DockStyle.Fill;
            pb.Image = bmp;
            this.Controls.Add(pb);
            this.ShowDialog();


        }
}

When I do partially capture screen, it was supposed to be liked thatthe area of capture, but the result would move to the right and up like that enter image description here. I also added one line of code to check the Form body size '''Console.WriteLine("X:"+this.Location.X.ToString()+" Y:"+this.Location.Y.ToString()+" Width:"+(this.Width).ToString()+" Height:"+(this.Height).ToString()+" Size: "+this.Size.ToString());''' And it seems to work as normal.

The screen in the second monitor shrinks and locates at the left-bottom liked thatenter image description here And my second monitor is bigger than my primary one, the second one is 2400*1350 and my primary one is 1920*1080 , and I try to uncomment or comment on the DPIAWARE as well.

c#
winforms
screenshot
multiple-monitors
asked on Stack Overflow May 21, 2020 by jliu • edited May 22, 2020 by jliu

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0