Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/45.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么';What’用鼠标在新的透明窗体上画一个矩形不是吗?_C#_Winforms - Fatal编程技术网

C# 为什么';What’用鼠标在新的透明窗体上画一个矩形不是吗?

C# 为什么';What’用鼠标在新的透明窗体上画一个矩形不是吗?,c#,winforms,C#,Winforms,它将进入F_Paint事件,但不会进入F_MouseDown事件。 我希望能够在F窗体上绘制矩形 也许因为F表单是透明的,所以它不能在上面绘制?但是它永远不会到达F_MouseDown事件,我在F_MouseDown事件中使用了一个断点 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.

它将进入
F_Paint
事件,但不会进入
F_MouseDown
事件。 我希望能够在
F
窗体上绘制矩形

也许因为
F
表单是透明的,所以它不能在上面绘制?但是它永远不会到达
F_MouseDown
事件,我在
F_MouseDown
事件中使用了一个断点

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tester
{
    public partial class Form1 : Form
    {
        Ffmpeg ffmpeg = new Ffmpeg();

        private bool _canDraw;
        private int _startX, _startY;
        private Rectangle _rect;

        public Form1()
        {
            InitializeComponent();

            BackColor = Color.Blue;
            TransparencyKey = BackColor;
            Opacity = 1;
            var f = new HelperForm { Opacity = 0, ShowInTaskbar = false, FormBorderStyle = FormBorderStyle.None };
            f.MouseDown += F_MouseDown;
            f.MouseMove += F_MouseMove;
            f.MouseUp += F_MouseUp;
            f.Paint += F_Paint;
            f.Show();
            Visible = false;
            Owner = f;
            Visible = true;
            Move += (o, a) => f.Bounds = Bounds;
            Resize += (o, a) => f.Bounds = Bounds;
            f.Bounds = Bounds;
            ffmpeg.Start(@"d:\ffmpegx86\test.mp4", 24);
        }

        private void F_Paint(object sender, PaintEventArgs e)
        {
            //Create a new 'pen' to draw our rectangle with, give it the color red and a width of 2
            using (Pen pen = new Pen(Color.Red, 2))
            {
                //Draw the rectangle on our form with the pen
                e.Graphics.DrawRectangle(pen, _rect);
            }
        }

        private void F_MouseUp(object sender, MouseEventArgs e)
        {
            //The system is no longer allowed to draw rectangles
            _canDraw = false;
        }

        private void F_MouseMove(object sender, MouseEventArgs e)
        {
            //If we are not allowed to draw, simply return and disregard the rest of the code
            if (!_canDraw) return;

            //The x-value of our rectangle should be the minimum between the start x-value and the current x-position
            int x = Math.Min(_startX, e.X);
            //The y-value of our rectangle should also be the minimum between the start y-value and current y-value
            int y = Math.Min(_startY, e.Y);

            //The width of our rectangle should be the maximum between the start x-position and current x-position minus
            //the minimum of start x-position and current x-position
            int width = Math.Max(_startX, e.X) - Math.Min(_startX, e.X);

            //For the hight value, it's basically the same thing as above, but now with the y-values:
            int height = Math.Max(_startY, e.Y) - Math.Min(_startY, e.Y);
            _rect = new Rectangle(x, y, width, height);
            //Refresh the form and draw the rectangle
            Refresh();
        }

        private void F_MouseDown(object sender, MouseEventArgs e)
        {
            //The system is now allowed to draw rectangles
            _canDraw = true;
            //Initialize and keep track of the start position
            _startX = e.X;
            _startY = e.Y;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ffmpeg.Close();
        }
    }

    class HelperForm : Form
    {
        protected override CreateParams CreateParams
        {
            get
            {
                const int WS_EX_TOOLWINDOW = 0x80;
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= WS_EX_TOOLWINDOW;
                return cp;
            }
        }
    }
}
不确定它为什么没有进入
MouseDown
事件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tester
{
    public partial class Form1 : Form
    {
        Ffmpeg ffmpeg = new Ffmpeg();

        private bool _canDraw;
        private int _startX, _startY;
        private Rectangle _rect;

        public Form1()
        {
            InitializeComponent();

            BackColor = Color.Blue;
            TransparencyKey = BackColor;
            Opacity = 1;
            var f = new HelperForm { Opacity = 0, ShowInTaskbar = false, FormBorderStyle = FormBorderStyle.None };
            f.MouseDown += F_MouseDown;
            f.MouseMove += F_MouseMove;
            f.MouseUp += F_MouseUp;
            f.Paint += F_Paint;
            f.Show();
            Visible = false;
            Owner = f;
            Visible = true;
            Move += (o, a) => f.Bounds = Bounds;
            Resize += (o, a) => f.Bounds = Bounds;
            f.Bounds = Bounds;
            ffmpeg.Start(@"d:\ffmpegx86\test.mp4", 24);
        }

        private void F_Paint(object sender, PaintEventArgs e)
        {
            //Create a new 'pen' to draw our rectangle with, give it the color red and a width of 2
            using (Pen pen = new Pen(Color.Red, 2))
            {
                //Draw the rectangle on our form with the pen
                e.Graphics.DrawRectangle(pen, _rect);
            }
        }

        private void F_MouseUp(object sender, MouseEventArgs e)
        {
            //The system is no longer allowed to draw rectangles
            _canDraw = false;
        }

        private void F_MouseMove(object sender, MouseEventArgs e)
        {
            //If we are not allowed to draw, simply return and disregard the rest of the code
            if (!_canDraw) return;

            //The x-value of our rectangle should be the minimum between the start x-value and the current x-position
            int x = Math.Min(_startX, e.X);
            //The y-value of our rectangle should also be the minimum between the start y-value and current y-value
            int y = Math.Min(_startY, e.Y);

            //The width of our rectangle should be the maximum between the start x-position and current x-position minus
            //the minimum of start x-position and current x-position
            int width = Math.Max(_startX, e.X) - Math.Min(_startX, e.X);

            //For the hight value, it's basically the same thing as above, but now with the y-values:
            int height = Math.Max(_startY, e.Y) - Math.Min(_startY, e.Y);
            _rect = new Rectangle(x, y, width, height);
            //Refresh the form and draw the rectangle
            Refresh();
        }

        private void F_MouseDown(object sender, MouseEventArgs e)
        {
            //The system is now allowed to draw rectangles
            _canDraw = true;
            //Initialize and keep track of the start position
            _startX = e.X;
            _startY = e.Y;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ffmpeg.Close();
        }
    }

    class HelperForm : Form
    {
        protected override CreateParams CreateParams
        {
            get
            {
                const int WS_EX_TOOLWINDOW = 0x80;
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= WS_EX_TOOLWINDOW;
                return cp;
            }
        }
    }
}

有几个原因阻止绘制矩形并触发鼠标事件。首先,将
不透明度设置为
0
;这意味着你试图画的东西永远都看不见。相反,您应该将
透明键
设置为
背景色中的颜色

f.TransparencyKey = f.BackColor;
然后,您尝试使用从未初始化过的对象
\rect
绘制一个矩形;因此,您试图绘制的矩形将以0宽0高的大小绘制,这意味着不会绘制该矩形,因此,在初始化过程中,您应该为
\u rect
提供默认值,例如:

_rect = new Rectangle(Point.Empty, this.Size);
绘制的矩形现在应该是可见的;但是,事件不会触发,因为您正在反转表单所有权,因此

Owner = f;
使用:


因为您正在将
不透明度设置为
0