根据 James Thorpe 的评论,我这样解决了。
首先我添加了所需的dll 和const
//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 状态。