Hallo zusammen
Ich wollte zum testen für ein späteres Projekt versuchen, die ProcessId
von dem Fenster, über dem sich der Cursor befindet, zu ermitteln.
Das ganze habe ich in VS gemacht auf einem Computer mit Windows 7 X64.
Dazu habe ich dieses simple Programm geschrieben:
1 | using System;
|
2 | using System.Collections.Generic;
|
3 | using System.ComponentModel;
|
4 | using System.Data;
|
5 | using System.Drawing;
|
6 | using System.Linq;
|
7 | using System.Text;
|
8 | using System.Windows.Forms;
|
9 | using System.Runtime.InteropServices;
|
10 | using System.Diagnostics;
|
11 |
|
12 | namespace BeispielMausposProcessId
|
13 | {
|
14 | public partial class Form1 : Form
|
15 | {
|
16 | [DllImport("user32.dll")]
|
17 | public static extern IntPtr WindowFromPoint(Point lpPoint);
|
18 | [DllImport("kernel32.dll", SetLastError = true)]
|
19 | static extern int GetProcessId(IntPtr hWnd);
|
20 |
|
21 | IntPtr window;
|
22 | Process proc;
|
23 | public Form1()
|
24 | {
|
25 | InitializeComponent();
|
26 | }
|
27 |
|
28 | private void timer1_Tick(object sender, EventArgs e)
|
29 | {
|
30 | window = WindowFromPoint(new Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y));
|
31 | int procId = GetProcessId(window);
|
32 | proc = Process.GetProcessById(procId);
|
33 | this.Text += " " + Convert.ToString(proc.Id);
|
34 | }
|
35 | }
|
36 | }
|
Nun liefert GetProcessId aber immer nur den Wert 0. Das Handle von
WindowFromPoint liefert allerdings noch unterschiedliche Werte. Also
sollte das Problem schon mit GetProcessId zu tun haben.
In Hoffnung auf eine gute Lösung
Ein verzweifelter Hobby-Programmierer