bonjour
dans une application tierce j'aimerais retrouver la titre de la fenetre principale d'une application a partir de son processId
en fait au départ je voulais réactiver la fenêtre sauf que si l'on fait cela
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 protected override void OnStateChanged(EventArgs e)
        {
            base.OnStateChanged(e);
            if (this.WindowState == WindowState.Minimized)
                this.ShowInTaskbar = false;
            else if (this.WindowState == WindowState.Maximized)
                this.ShowInTaskbar = true;
        }
dans la fenêtre
alors le Process.MainWindowHandle est a zero

les deux solutions sont les suivantes
- trouver le hwnd de la fenêtre par un win32 FindWindow mais ce cas il faut le titre et j'aimerais généraliser
- de parser les fenêtres avec un FindWindowEx du systeme mais dans ce cas pour wpf j'ai des tas de thread (correspondant au processID) et de surcrois je peux pas identifier surement la fenêtre ni par sa class qui commence par hWndWrapper ni par son texte car j'en ai 3 qui correspondent

voici la classe static que j'utilise pour faire cela
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
 public class UnMinimizeClass
    {
        [DllImport("user32.dll")]
        static extern bool AllowSetForegroundWindow(int dwProcessId);
 
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
 
        [DllImport("user32.dll")]
        public static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);
 
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
 
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, long uFlags);
 
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
 
        [DllImport("user32.dll", SetLastError = true)]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
 
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
 
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetWindowTextLength(IntPtr hWnd);
 
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
 
        const int SW_RESTORE = 9;
 
        static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
        static readonly IntPtr HWND_TOP = new IntPtr(0);
        static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
 
        public static IntPtr GetWindowHandle(uint processId)
        {
            string s = "";
            IntPtr hCurWnd = IntPtr.Zero;
            do
            {
                hCurWnd = FindWindowEx(IntPtr.Zero, hCurWnd, null, null);
                uint dwProcessID = 0;
                GetWindowThreadProcessId(hCurWnd, out dwProcessID);
                string x = "- pointeur = " + hCurWnd + "\n - titre = \"" + GetText(hCurWnd) + "\" \n - className = " + ClassName(hCurWnd) + "\n";
                bool? result = ClassName(hCurWnd)?.StartsWith("HwndWrapper");
                if (result == true && processId == dwProcessID && !string.IsNullOrEmpty(GetText(hCurWnd)))
                {
                    s += x;
                }
            }
            while (hCurWnd != IntPtr.Zero);
            MessageBox.Show(">>> \n" + s);
            return hCurWnd;
        }
 
        public static string ClassName(IntPtr hWnd)
        {
            StringBuilder ClassName = new StringBuilder(256);
            int nRet = GetClassName(hWnd, ClassName, ClassName.Capacity);
            return nRet != 0 ? ClassName.ToString() : null;
        }
 
        public static string GetText(IntPtr hWnd)
        {
            // Allocate correct string length first
            int length = GetWindowTextLength(hWnd);
            StringBuilder sb = new StringBuilder(length + 1);
            GetWindowText(hWnd, sb, sb.Capacity);
            return sb.ToString();
        }
 
 
        public static void UnMinimize(Process process)
        {
            IntPtr handle = process.MainWindowHandle;
            if (process.MainWindowHandle.ToInt32() == 0)
            {
                IntPtr hx = FindWindow(null, "XXX");
                handle = GetWindowHandle((uint)process.Id);
                MessageBox.Show("pointeur a zero = " + handle + " window handle = " + hx);
            }
            UnMinimize(handle, process.Id);
        }
        public static void UnMinimize(IntPtr handle, int processId)
        {
            WINDOWPLACEMENT WinPlacement = new WINDOWPLACEMENT();
            GetWindowPlacement(handle, out WinPlacement);
            //MessageBox.Show("window placement flag = " + GetWindowStat(handle));
            int stat = GetWindowStat(handle);
 
            if (stat == SW_MAXIMIZE)
            {
                SetForegroundWindow(handle);
                SetWindowPos(handle, (IntPtr)HWND_TOP, 0, 0, 0, 0, SWP.NOMOVE | SWP.NOSIZE | SWP.SHOWWINDOW);
            }
            else if (stat == SW_HIDE)
            {
                ShowWindow(handle, SW_SHOW);
                SetForegroundWindow(handle);
                SetWindowPos(handle, (IntPtr)HWND_TOP, 0, 0, 0, 0, SWP.NOMOVE | SWP.NOSIZE | SWP.SHOWWINDOW);
            }
            else
            {
                ShowWindow(handle, (int)SW_RESTORE);
                SetForegroundWindow(handle);
                SetWindowPos(handle, (IntPtr)HWND_TOP, 0, 0, 0, 0, SWP.NOMOVE | SWP.NOSIZE | SWP.SHOWWINDOW);
            }
        }
 
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool IsWindowVisible(IntPtr hWnd);
 
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool IsIconic(IntPtr hWnd);
 
        [DllImport("user32.dll")]
        static extern bool IsZoomed(IntPtr hWnd);
 
        public const int SW_HIDE = 0;
        public const int SW_MINIMIZE = 6;
        public const int SW_MAXIMIZE = 3;
        public const int SW_SHOW = 5;
 
        public static int GetWindowStat(IntPtr hWnd)
        {
            if (!IsWindowVisible(hWnd))
                return SW_HIDE;
            else if (IsIconic(hWnd))
                return SW_MINIMIZE;
            else if (IsZoomed(hWnd))
                return SW_MAXIMIZE;
            else
            {
                return SW_SHOW;
            }
        }
    }
 
    public static class SWP
    {
        public static readonly int
        NOSIZE = 0x0001,
        NOMOVE = 0x0002,
        NOZORDER = 0x0004,
        NOREDRAW = 0x0008,
        NOACTIVATE = 0x0010,
        DRAWFRAME = 0x0020,
        FRAMECHANGED = 0x0020,
        SHOWWINDOW = 0x0040,
        HIDEWINDOW = 0x0080,
        NOCOPYBITS = 0x0100,
        NOOWNERZORDER = 0x0200,
        NOREPOSITION = 0x0200,
        NOSENDCHANGING = 0x0400,
        DEFERERASE = 0x2000,
        ASYNCWINDOWPOS = 0x4000;
    }
et plus particulièrement la fonction GetWindowHandle