问题
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.
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.
回答1:
You must use extended windows styles, this code show how to do it.
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.
To show your windows always on top, put a timer with a interval of 100ms and set the form property TopMost to true.
来源:https://stackoverflow.com/questions/23662983/click-through-able-form-that-stays-always-on-top