Hi,
ich habe hier etwas Code, das mir die Möglichkeit schenkt, festzustellen
"welches Programm" aktiv ist.
Leider erkennt der Code nicht, wenn sich der Titel ändert z.B.
Ich wechsle von Outlook in den Browser
dann gibt er zurück
Browser - Mikrocontroller.net...
jetzt wechsle ich IM Browser den Tab von Mikrocontroller auf eBay, das
Tool erkennt das aber nicht, weil kein Fensterwechsel statt fand...
muss ich jetzt jeden Mausklick überprüfen? Oder gibt es eine
Windowsfunktion welche Titel-Änderungen erkennt?
Vielen Dank
anbei mein Code:
1 | WinEventDelegate dele = null;
|
2 | delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
|
3 |
|
4 | [DllImport("user32.dll")]
|
5 | static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
|
6 |
|
7 | private const uint WINEVENT_OUTOFCONTEXT = 0;
|
8 | private const uint EVENT_SYSTEM_FOREGROUND = 3;
|
9 |
|
10 | [DllImport("user32.dll")]
|
11 | static extern IntPtr GetForegroundWindow();
|
12 |
|
13 | [DllImport("user32.dll")]
|
14 | static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
|
15 |
|
16 | [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
17 | [return: MarshalAs(UnmanagedType.Bool)]
|
18 | private static extern bool UnhookWindowsHookEx(IntPtr hhk);
|
19 |
|
20 |
|
21 | private string GetActiveWindowTitle()
|
22 | {
|
23 | const int nChars = 256;
|
24 | IntPtr handle = IntPtr.Zero;
|
25 | StringBuilder Buff = new StringBuilder(nChars);
|
26 | handle = GetForegroundWindow();
|
27 |
|
28 | if (GetWindowText(handle, Buff, nChars) > 0)
|
29 | {
|
30 | return Buff.ToString();
|
31 | }
|
32 | return null;
|
33 | }
|
34 |
|
35 | public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
|
36 | {
|
37 | Ausgabe = GetActiveWindowTitle();
|
38 | }
|