早上,
我现在准备好在这一点上抓住自己的眼睛.我正在使用.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(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();
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以指出我正确的方向或建议在哪里看.
非常感谢
我实际上为屏幕捕获做了一个应用,按照你描述的方式工作,使它可以拖动我使用鼠标事件.为了使它透明,我简单地制作了另一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3507 次 |
| 最近记录: |