Winforms:可拖动的透明矩形

早上,

我现在准备好在这一点上抓住自己的眼睛。 我正在使用.NET 3.5上的Windows窗体构建一个基本的图像编辑器,我需要的是一个“选择工具”。 单击按钮时需要显示此工具,并且该工具将是固定大小,它需要是具有透明中心的拖放式矩形。

这样做的目的就是像“画框”一样,用户可以将矩形拖放到图像的一部分上,然后点击另一个按钮来快照该点矩形内的任何内容。 (请注意:我不需要橡皮筋矩形,它必须是固定尺寸,在整个表格上可拖动并且透明)。

我花了几天时间在互联网上搜索,这个网站正在寻找可能的解决方案,其中没有一个是有用的。 我已设法使控件可拖动 – 但这会带来透明度方面的问题。 下面是使控件可拖动的代码,但我不确定这是正确的路径。

class ControlMover { public enum Direction { Any, Horizontal, Vertical } public static void Init(Control control) { Init(control, Direction.Any); } public static void Init(Control control, Direction direction) { Init(control, control, direction); } public static void Init(Control control, Control container, Direction direction) { EditorForm.blnSelectArea = true; bool Dragging = false; Point DragStart = Point.Empty; control.MouseDown += delegate(object sender, MouseEventArgs e) { Dragging = true; DragStart = new Point(eX, eY); control.Capture = true; }; control.MouseUp += delegate(object sender, MouseEventArgs e) { Dragging = false; control.Capture = false; }; control.MouseMove += delegate(object sender, MouseEventArgs e) { if (Dragging) { if (direction != Direction.Vertical) container.Left = Math.Max(0, eX + container.Left - DragStart.X); if (direction != Direction.Horizontal) container.Top = Math.Max(0, eY + container.Top - DragStart.Y); control.Invalidate(); } }; } } 

任何人都可以指出我正确的方向或建议在哪里看。

非常感谢

我实际上为屏幕捕获做了一个应用程序,它按照你描述的方式工作,使它可以拖动我使用鼠标事件。 为了使它透明,我简单地制作另一个Form控件,使用半透明png Image作为背景图像。

 public partial class Photo : Form { public delegate void ScreenShotReadyDelegate(Bitmap g); public event ScreenShotReadyDelegate ScreenShotReady; bool Moving = false; Point oldLoc = new Point(); public Photo() { InitializeComponent(); this.FormBorderStyle = FormBorderStyle.None; this.DoubleBuffered = true; this.SetStyle(ControlStyles.ResizeRedraw, true); } private void Photo_MouseDoubleClick(object sender, MouseEventArgs e) { this.BackgroundImage = null; this.Invalidate(); Rectangle bounds = this.Bounds; using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) { using (Graphics g = Graphics.FromImage(bitmap)) { g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size); } ScreenShotReady(bitmap); } this.BackgroundImage = Properties.Resources.rect; this.Invalidate(); } private void Photo_MouseDown(object sender, MouseEventArgs e) { this.Moving = true; this.oldLoc = MousePosition; } private void Photo_MouseMove(object sender, MouseEventArgs e) { if (this.Moving) { Point vector = new Point(MousePosition.X - this.oldLoc.X, MousePosition.Y - this.oldLoc.Y); this.Location = new Point(this.Location.X + vector.X, this.Location.Y + vector.Y); this.oldLoc = MousePosition; } } private void Photo_MouseUp(object sender, MouseEventArgs e) { this.Moving = false; } } 

你应该能够在阅读这个儿童控制之上的绘画之后

我使用过ControlPaint.DrawReversibleFrame(_FrameRect, Color.Black, FrameStyle.Dashed); 我以前和它工作正常:)

你最好制作一个绘制图像的自定义控件,并将矩形覆盖在顶部。 然后,您可以处理鼠标事件以“移动”矩形。

一些伪代码让你开始:

 class cropcontrol onpaint paint image paint rectangle using rect location onmousemove if mouse down update rect location