簡體   English   中英

如何獲得不屬於我的應用程序的活動窗口?

[英]How to get active window that is not part of my application?

如何獲得用戶當前關注的窗口標題? 我正在制作一個與另一個Window一起運行的程序,如果用戶沒有將注意力集中在該窗口上,則我的程序沒有理由繼續更新。

那么,如何確定用戶關注的窗口呢?

我確實嘗試研究

[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();

但是我似乎只能在Window是我的應用程序的一部分的情況下使用它,而事實並非如此。

檢查此代碼:

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();


[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

private string GetActiveWindowTitle()
{
    const int nChars = 256;
    StringBuilder Buff = new StringBuilder(nChars);
    IntPtr handle = GetForegroundWindow();

    if (GetWindowText(handle, Buff, nChars) > 0)
    {
     return Buff.ToString();
    }
  return null;
}

使用GetForegroundWindow檢索聚焦窗口的句柄,並使用GetWindowText獲得窗口標題。

[ DllImport("user32.dll") ]
static extern int GetForegroundWindow();

[ DllImport("user32.dll") ]
static extern int GetWindowText(int hWnd, StringBuilder text, int count);   

static void Main() { 
     StringBuilder builder = new StringBuilder(255) ; 
     GetWindowText(GetForegroundWindow(), builder, 255) ; 

     Console.WriteLine(builder) ; 
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM