Bringing window to front and sending a message
Gah. Im depressed that I cant do this, I used to be good at programming! I'm in Vb.Net 2008 express, and I have a window that I need to send the message F9 to. So, I tried using sendmessage, but it isnt working. So I figured I would use keybd_event after bringing the window to the front. Problem is, I cant bring the window to the front!!! Im tearing my hair out. Okay. Heres my code (along with a whole bunch of other crud I threw in there to hopefully get it to work)
VB Code:
Public Class Form1
Const VK_F9 = 120
Const VK_SPACE As Int32 = &H20
Const SW_SHOW As Int32 = 5
Const WM_KEYDOWN As Integer = 256
Const WM_KEYUP As Integer = 257.0
<Runtime.InteropServices.DllImport("user32", EntryPoint:="ShowWindow")> _
Private Shared Function ShowWindow( _
ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
End Function
Private SW_SHOWNORMAL As Integer = &H1
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
Dim recordingAudioHwnd As Int32 = Process.GetProcessesByName("Live 7.0.1")(0).MainWindowHandle
Dim recordingVideoHwnd As Int32 = Process.GetProcessesByName("LiveCam")(0).MainWindowHandle
ShowWindow(recordingAudioHwnd, SW_SHOWNORMAL)
SetForegroundWindow(recordingAudioHwnd)
keybd_event(VK_F9, 0, 0, 0)
keybd_event(VK_F9, 0, WM_KEYUP, 0)
Application.DoEvents()
keybd_event(VK_SPACE, 0, 0, 0)
keybd_event(VK_SPACE, 0, WM_KEYUP, 0)
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
NOTHING works. Thanks for your help.
Edit**
myAudio(0) is returning a hwnd for those who are wondering :)
edit#2-
I had to fix the declaration for ShowWindow. Now that somewhat works. Problem is, it isnt sending the vk_space ever, any ideas?
Re: Bringing window to front and sending a message
j2mix, what about http://msdn.microsoft.com/en-us/libr...45(VS.85).aspx
the api's you have used look like works only with in the application own windows.
Re: Bringing window to front and sending a message
Hey man thanks. Unfortunately for me, this reacts the same way. The program is brought to the top, it sends F9 (and it takes it) and it denies the keybd event of spacebar most of the time. Sometimes it works though. Any ideas?
Re: Bringing window to front and sending a message
Just a shoot in the dark here but.
Suppose that the window you bring to foreground is not initialized yet (although you have (Form1_Activated and doevents). I had an issue with starting an application inside the form and the problem was that it could not be initialized soon enought.
When you set your form to foreground, try pausing all the code for 5seconds and then continue (better yet, pause it before the first keybd_event) . As i said it's a shoot in the dark but your code seems ok.
Re: Bringing window to front and sending a message
Dang I thought you had it because it works the first time. Ill rep you anyways, but I still have not got this to work 100%. Just sometimes the space bar gets received. I wish i still had spy++ because it works so much better than WinID.
Re: Bringing window to front and sending a message
As i said, it's probably as shoot in the dark. You're probably better at APIng than me so if you figure this out , would be glad to post the solution, it may help to solve my app in a form problem too, without having to pause.
(Thanks for the rep btw :wave: )