[02/03] Problem using Process.MainWindowHandle on application in systray.
Firstly, thank you for your taking your time to look at this issue. Secondly, here is the situation. I have an external application (an OpenGL window) that I force to run in the system tray. I need to run several instances of this OpenGL application, and send keystrokes to the application windows while they are running in the tray. The reason I force these applications to the tray is because Windows moves most of the applications memory usage to the pagefile, which frees up system RAM for other processes.
I can accomplish this if a single instance is running by using:
VB Code:
Dim ThWnd As Integer = FindWindow(vbNullString, "OpenGL Window")
PostMessage(ThWnd, WM_KEYDOWN, PM_KEY_RETURN, 0)
The problems start when I need to run multiple instances of the OpenGL window. FindWindow(vbNullString, "OpenGL Window") only returns the handle for a single window. To get around this I use:
VB Code:
Dim proc As Process = Process.GetCurrentProcess
For Each proc In Process.GetProcesses
If proc.ProcessName = "openGLprocess" Then
Dim ThWnd As Integer = proc.MainWindowHandle.ToInt32()
PostMessage(ThWnd, WM_KEYDOWN, PM_KEY_RETURN, 0)
End If
Next
This works fine if all the OpenGL windows are not forced to the system tray. However, when an OpenGL window is forced to the tray proc.MainWindowHandle = 0 and proc.MainWindowTitle = "". But FindWindow(vbNullString, "OpenGL Window") can still return an individual MainWindowHandle at random, and return a window handle that will properly send the keystrokes to a trayed OpenGL application.
How can I get Process.MainWindowHandle to return the window handle of a process that has been forced to the tray? If this is not possible, how could you achieve the same effect using Findwindow.