Archive for category Win32
I was forced to delve into the world of Win32 coding to solve an issue of calling the SaveFileDialog method. I have a long-running WPF application that I wanted to get the user’s attention when it was finally ready for some love (i.e. the Save File dialog box appeared). I was having an issue where the app would quietly sit there until the user decided to check it – not the kind of passive relationship I was looking for. I needed to spice it up.
Luckily, I found the solution to shy application.
First, add a few DllImport statements to delve into the user32.dll file:
<Runtime.InteropServices.DllImport("user32.dll")> _ Private Shared Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer End Function <Runtime.InteropServices.DllImport("user32.dll")> _ Private Shared Function SetForegroundWindow(ByVal hwnd As Integer) As Integer End Function <Runtime.InteropServices.DllImport("user32.dll")> _ Private Shared Function IsIconic(ByVal hWnd As Integer) As Integer End Function
The meat of the code is this For Each loop. The only modification you will have to make is to change the MainWindowTitle to match the result of the window you want to give focus to:
'Give focus to the dialog box when it is ready For Each p As Process In Process.GetProcesses If p.MainWindowTitle = "Results" Then If IsIconic(p.MainWindowHandle.ToInt32) <> 0 Then ShowWindow(p.MainWindowHandle.ToInt32, &H1) Else SetForegroundWindow(p.MainWindowHandle.ToInt32) End If End If Next