它正在進入F_Paint事件,但不是F_MouseDown事件。我希望能夠在F表單上繪制矩形。也許因為F表格是透明的,所以不能在上面畫畫?但它永遠不會到達F_MouseDown我在事件中使用斷點的F_MouseDown事件。不知道為什么它沒有參加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; }
1 回答

喵喔喔
TA貢獻1735條經驗 獲得超5個贊
阻止繪制矩形和觸發鼠標事件的原因有很多。首先,您將設置Opacity
為0
; 這意味著您嘗試繪制的任何內容都永遠不可見。相反,您應該將 設置為TransparencyKey
中的顏色BackColor
:
f.TransparencyKey = f.BackColor;
然后,您嘗試使用_rect
從未初始化的對象繪制一個矩形;因此,您嘗試繪制的矩形將以 0 寬度和 0 高度的大小繪制,這意味著它不會被繪制,因此,在初始化期間,您應該為_rect
例如:
_rect = new Rectangle(Point.Empty, this.Size);
繪制的矩形現在應該是可見的;但是,事件不會觸發,因為您正在反轉表單所有權,因此,而不是
Owner = f;
用:
f.Owner = this;
- 1 回答
- 0 關注
- 148 瀏覽
添加回答
舉報
0/150
提交
取消