修改鼠标十字准线代码以使其位于窗体顶部?

发布于 2024-09-15 23:28:15 字数 830 浏览 3 评论 0原文

在空白 winform 代码上可以添加 以显示在鼠标指针处相交的线(十字准线)。问题是表单上的控件(即列表视图、splitcontainer、按钮)不会显示(或部分隐藏)这些行。

我将如何修改下面的代码以显示表单上存在的所有控件的顶部(置于前面...)?

int lastX = 0;
int lastY = 0;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Region r = new Region();
r.Union(new Rectangle(0, lastY, this.Width, 1));
r.Union(new Rectangle(lastX, 0, 1, this.Height));
this.Invalidate(r);
this.Update();
Graphics g = Graphics.FromHwnd(this.Handle);

g.DrawLine(Pens.Chocolate, 0, e.Y, this.Width, e.Y);
g.DrawLine(Pens.Chocolate, e.X, 0, e.X, this.Height);
lastX = e.X;
lastY = e.Y;
}

private void Form1_MouseLeave(object sender, EventArgs e)
{
this.Invalidate();
}

On a blank winform code can be added to show lines that intersect (crosshairs) at the mouse pointer. The problem is that the lines don't show (or are partially hidden) by controls on the form (ie listview, splitcontainer, buttons).

How would I modify the code below to show on-top (bring to front...) of all the controls present on the form?

int lastX = 0;
int lastY = 0;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Region r = new Region();
r.Union(new Rectangle(0, lastY, this.Width, 1));
r.Union(new Rectangle(lastX, 0, 1, this.Height));
this.Invalidate(r);
this.Update();
Graphics g = Graphics.FromHwnd(this.Handle);

g.DrawLine(Pens.Chocolate, 0, e.Y, this.Width, e.Y);
g.DrawLine(Pens.Chocolate, e.X, 0, e.X, this.Height);
lastX = e.X;
lastY = e.Y;
}

private void Form1_MouseLeave(object sender, EventArgs e)
{
this.Invalidate();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱情眠于流年 2024-09-22 23:28:15

您需要一个位于所有其他控件之上的透明窗口。获得一个的唯一方法是将表单与另一个表单重叠,并通过其 TranparencyKey 属性使其透明。您可以在我的答案中找到示例代码 此帖子

You need a transparent window that's on top of all the other controls. The only way to get one is by overlapping the form with another form, made transparent with its TranparencyKey property. You'll find sample code for this in my answer in this thread.

向日葵 2024-09-22 23:28:15

请先尝试将表单上的控件(即 listview、splitcontainer、按钮)发送到 back(Control.SendToBack())。将其放在 FormLoad 事件中。我曾使用 Windows MDI 应用程序尝试过同样的噩梦。
希望有帮助,

Please just try first sending to back(Control.SendToBack()) the controls on the form (ie listview, splitcontainer, buttons). Put this at the FormLoad event. I have experimented the same nightmare with a Windows MDI application.
Hope that helps,

檐上三寸雪 2024-09-22 23:28:15

枚举所需的控件并对其调用 .BringToFront(); 函数。

listBox1.BringToFront();

Enumerate through the desired controls and call the .BringToFront(); function on them.

listBox1.BringToFront();
苍风燃霜 2024-09-22 23:28:15

根据文档,区域对象应该位于世界坐标中,您正在传递客户端坐标。使用 Control.PointToScreen 将矩形的左上角坐标映射到世界空间。

我还想将绘图推迟到 OnPaint 方法。

According to the documentation, the region object should be in world co-ordinates, you're passing in client co-ordinates. Use Control.PointToScreen to map the rectangles' top left coordinate to world space.

I'd also be tempted to defer the drawing to the OnPaint method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文