Results 1 to 6 of 6

Thread: Bringing window to front and sending a message

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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:
    1. Public Class Form1
    2.     Const VK_F9 = 120
    3.     Const VK_SPACE As Int32 = &H20
    4.     Const SW_SHOW As Int32 = 5
    5.     Const WM_KEYDOWN As Integer = 256
    6.     Const WM_KEYUP As Integer = 257.0
    7.     <Runtime.InteropServices.DllImport("user32", EntryPoint:="ShowWindow")> _
    8.         Private Shared Function ShowWindow( _
    9.         ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
    10.     End Function
    11.     Private SW_SHOWNORMAL As Integer = &H1
    12.     Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    13.     Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
    14.  
    15.     Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    16.         Dim recordingAudioHwnd As Int32 = Process.GetProcessesByName("Live 7.0.1")(0).MainWindowHandle
    17.         Dim recordingVideoHwnd As Int32 = Process.GetProcessesByName("LiveCam")(0).MainWindowHandle
    18.         ShowWindow(recordingAudioHwnd, SW_SHOWNORMAL)
    19.         SetForegroundWindow(recordingAudioHwnd)
    20.         keybd_event(VK_F9, 0, 0, 0)
    21.         keybd_event(VK_F9, 0, WM_KEYUP, 0)
    22.         Application.DoEvents()
    23.         keybd_event(VK_SPACE, 0, 0, 0)
    24.         keybd_event(VK_SPACE, 0, WM_KEYUP, 0)
    25.         Me.Close()
    26.     End Sub
    27.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    28.  
    29.     End Sub
    30. 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?
    Last edited by |2eM!x; May 17th, 2008 at 06:57 PM.

  2. #2
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    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.

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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?

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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.

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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.

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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 )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width