的WinForms:可拖动矩形透明 [英] Winforms: A draggable transparent rectangle
问题描述
上午,
我正要准备爪自己的眼睛出在这一点上。我使用.NET 3.5的Windows窗体建立一个基本的图像编辑器,我需要的是一个选择工具。此工具需要一个按钮被点击时,出现和将是一个固定的大小,它需要一个拖动并dropable矩形透明中心
I'm just about ready to claw my own eyes out at this point. I'm building a basic image editor using Windows forms on .NET 3.5, and what I need is a 'select tool'. This tool needs to appear when a button is clicked and will be a fixed size, it needs to be a drag and dropable rectangle with a transparent center.
这样做的目的是采取行动几乎就像在用户可以拖动并在图像的一部分掉落的矩形,并触及按钮快照无论是在该点的矩形内的画框。 (请注意:我不想橡皮筋矩形,它必须是一个固定的大小,在窗体透明的可拖动)
The purpose of this is to act almost like a 'Picture frame' in that the user can drag and drop the rectangle over a portion of the image and hit another button to snapshot whatever is inside the rectangle at that point. (Please note: I don't want a rubber band rectangle, it has to be a fixed size, draggable across the form and transparent).
我已经花了几天冲刷互联网和这个网站寻找可能的解决方案,其中没有一个是任何用处。我已经设法使控制拖动 - 但这带来的问题与透明度。下面是这使得控制可拖动的代码,但我不知道这是走在正确的道路。
I've spent a couple of days scouring the internet and this site looking for possible solutions, none of which have been any use. I have managed to make a control draggable - but this poses problems with transparency. Below is the code which makes a control draggable, but I'm not sure this is the right path to take.
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(e.X, e.Y);
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, e.X + container.Left - DragStart.X);
if (direction != Direction.Horizontal)
container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
control.Invalidate();
}
};
}
}
任何人都可以点我在正确的方向或进行建议为去哪里找。
Can anyone point me in the right direction or make a suggestion as to where to look.
非常感谢
推荐答案
我actualy取得了屏幕捕获的aplication的工作你描述的方式,使之可拖动我使用鼠标事件。为了使它透明我只是做与半透明的PNG图像作为背景图片的另一种形式的控制。
I actualy made an aplication for screen capturing that worked the way you describe, to make it draggable i use Mouse events. To make it transparent i simply made another Form control with semi-transparent png Image as background 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;
}
}
这篇关于的WinForms:可拖动矩形透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!