Yeni bir pencere formları projesi oluşturun, bir metin kutusu ekleyin, çok satırlı hale getirin ve metin kutusunu Dock özelliğini doldurmak için doldurun, adlandırın ve aşağıdaki kodu yapıştırın (System.Runtime.InteropServices dosyasını eklemeniz gerekir)
WinEventDelegate dele = null;
public Form1()
{
InitializeComponent();
dele = new WinEventDelegate(WinEventProc);
IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
}
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
[DllImport("user32.dll")]
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
private const uint WINEVENT_OUTOFCONTEXT = 0;
private const uint EVENT_SYSTEM_FOREGROUND = 3;
[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;
IntPtr handle = IntPtr.Zero;
StringBuilder Buff = new StringBuilder(nChars);
handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
Log.Text += GetActiveWindowTitle() + "\r\n";
}
"Değişiklikler" tanımlayabilir misiniz? – Eric
http://support.microsoft.com/kb/318804 göre, .NET küresel kanca yapamaz ve küresel kanca Eğer bu gibi olayların nasıl izlediğimizi olduğundan, bu sadece diğer seçenekleri yoklama vardır benziyor veya C# 'dan erişebileceğiniz bir arabirim sağlayan bazı yönetilmeyen kodlar (örneğin C++) yazabilirsiniz. –
Jim Bence doğru cevaba sahipsiniz. – Joe