亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么不在新的透明窗體上用鼠標繪制矩形?

為什么不在新的透明窗體上用鼠標繪制矩形?

C#
素胚勾勒不出你 2021-10-24 17:01:17
它正在進入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個贊

阻止繪制矩形和觸發鼠標事件的原因有很多。首先,您將設置Opacity0; 這意味著您嘗試繪制的任何內容都永遠不可見。相反,您應該將 設置為TransparencyKey中的顏色BackColor

f.TransparencyKey = f.BackColor;

然后,您嘗試使用_rect從未初始化的對象繪制一個矩形;因此,您嘗試繪制的矩形將以 0 寬度和 0 高度的大小繪制,這意味著它不會被繪制,因此,在初始化期間,您應該為_rect例如:

_rect = new Rectangle(Point.Empty, this.Size);

繪制的矩形現在應該是可見的;但是,事件不會觸發,因為您正在反轉表單所有權,因此,而不是

Owner = f;

用:

f.Owner = this;


查看完整回答
反對 回復 2021-10-24
  • 1 回答
  • 0 關注
  • 148 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號