单击始终位于顶部的可通过表单 [英] Click through-able form that stays always on top

查看:58
本文介绍了单击始终位于顶部的可通过表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个始终在顶部的表单,该表单是透明的,可以单击通过,并且纯粹用于绘制永远不会失去焦点的信息,因此它始终显示在顶部.这个想法是能够在我的屏幕上绘制信息,这些信息将始终位于每个其他窗口的顶部,很可能只是简单的文本,但不会阻止我在仍然可见的情况下与我的其他程序进行交互.

I'm trying to create a form that's always on top that is transparent, click through-able, and is purely used to draw information on that never loses focus, so it is always displayed on top. The idea is to be able to draw information on my screen that will constantly be on top of every other window, most likely only simple text, but doesn't prevent me from interacting with the rest of my programs while still being visible.

我遇到的麻烦是让表单始终位于最前面,尽管其他程序正在关注.我曾尝试使用 TopMost 属性,但这似乎不起作用,并且尝试过让窗口重新聚焦在未聚焦上,但这似乎有点草率并且无论如何都不起作用.我在 Windows 8.1 上应该有关系.

The trouble I am having is to get the form to always be on top, despite other programs being focuses. I have tried using the TopMost property but that doesn't seem to work, and have played about with having the window re-focus on unfocus but that seemed a bit sloppy and didn't work anyhow. I am on Windows 8.1 should it matter.

非常感谢您的回复,谢谢.

Any replies greatly appreciated, thank you.

推荐答案

您必须使用扩展的窗口样式,此代码显示了如何操作.

You must use extended windows styles, this code show how to do it.

此表单将鼠标交互传递给它后面的表单,即使其他 Top Most 窗口处于活动状态,它也会显示为 TopMost.

This form pass the mouse interaction to the form behind it, and will show as TopMost even with other Top Most windows active.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    const int GWL_EXSTYLE = -20;
    const int WS_EX_TRANSPARENT = 0x20;

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    extern static int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    extern static int SetWindowLong(IntPtr hWnd, int nIndex, int nStyle);

    private void Form1_Load(object sender, EventArgs e)
    {
        var style = GetWindowLong(this.Handle, GWL_EXSTYLE);
        var newStyle = style | WS_EX_TRANSPARENT;

        SetWindowLong(this.Handle, GWL_EXSTYLE, newStyle);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.BringToFront();
    }
}

用户与表单交互将是一个小技巧,您必须提供有关您要执行的操作的更多详细信息,以便我们提供帮助.

It will be a little trick for the user to interact with the form, you must provide more details about what you're trying to do, so we can help.

要始终在顶部显示您的窗口,请设置一个间隔为 100 毫秒的计时器,并将表单属性 TopMost 设置为 true.

To show your windows always on top, put a timer with a interval of 100ms and set the form property TopMost to true.

这篇关于单击始终位于顶部的可通过表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆