【问题标题】:How do I get the "topmost" status of every window in c#如何在 c# 中获取每个窗口的“最顶层”状态
【发布时间】:2016-05-20 20:08:47
【问题描述】:

实际上,我自己编写了一个程序,将特定窗口的topmost 状态设置为truefalse

我通过导入 user32.dll

实现了这一点
[DllImport("user32.dll")]

private static extern bool SetWindowPos(...);

现在我想知道是否可以获取窗口的状态并查看是否设置了 topmost 并将其添加到我在 Listview 中的项目中。

当然,我可以在运行时执行此操作,具体取决于我执行的操作,但我的程序不会一直打开,我不想在某些地方保存数据。

我想在listview 中看到是否在之前设置了topmost,然后在按钮点击上设置它true/false。取决于它的状态...

那么有没有像GetWindowPos 这样的东西来获取特定窗口的topmost 状态?

【问题讨论】:

  • Related,只需翻译成所需的DllImport调用等即可。
  • @JamesThorpe 我试图把它翻译成 c# 对我来说实际上有点困难,因为我是 c# 的新手^^
  • Nvm,我成功了。谢谢你:)
  • 太棒了!随意发布它作为答案:)

标签: c# user32


【解决方案1】:

根据 James Thorpe 的评论,我这样解决了。

首先我添加了所需的dllconst

//Import DLL
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

//Add the needed const
const int GWL_EXSTYLE      = (-20);
const UInt32 WS_EX_TOPMOST = 0x0008;

此时我意识到当我想使用它的几个功能时,我总是必须再次添加 DLL。例如

[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

我以为我可以这样写

[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

注意:你不能这样做:D

在此之后,我去我的函数刷新我的ListView 并像这样编辑它

//Getting the processes, running through a foreach
//...
//Inside the foreach
int dwExStyle    = GetWindowLong(p.MainWindowHandle, GWL_EXSTYLE);

string isTopMost = "No";

if ((dwExStyle & WS_EX_TOPMOST) != 0)
{
    isTopMost = "Yes";
}

ListViewItem wlv = new ListViewItem(isTopMost, 1);
wlv.SubItems.Add(p.Id.ToString());
wlv.SubItems.Add(p.ProcessName);
wlv.SubItems.Add(p.MainWindowTitle);
windowListView.Items.Add(wlv);

//Some sortingthings for the listview
//end of foreach processes

这样我可以显示窗口是否有topmost 状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    相关资源
    最近更新 更多