gpt4 book ai didi

c# - Winforms:一个可拖动的透明矩形

转载 作者:行者123 更新时间:2023-11-30 14:00:59 26 4
gpt4 key购买 nike

早上好

此时我正准备挖出自己的眼睛。我正在使用 .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();

}
};
}
}

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

非常感谢

最佳答案

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

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;
}
}

关于c# - Winforms:一个可拖动的透明矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9374806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com