Msgbox doesnt get focus

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vdraceil
    New Member
    • Jul 2007
    • 236

    Msgbox doesnt get focus

    Hi everyone,
    I'm using VB6.I've lately been into working with APIs.I'm working on a program that makes the cursor rotate spirally and as it does so,any window opened will be minimized.The only way to stop it is by pressing F11+F12.

    Form's name=cursfrm
    It has only 2 timer controls-curstmr and mintmr,both with interval=1

    My code...

    Code:
    Option Explicit
    
    Private Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer
    Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long
    Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    
    Private Const VK_F11 As Long = &H7A
    Private Const VK_F12 As Long = &H7B
    Private Const VK_LWIN As Long = &H5B
    Private Const VK_M As Long = &H4D
    
    Private Const KEYEVENTF_EXTENDEDKEY As Long = &H1
    Private Const KEYEVENTF_KEYUP As Long = &H2
    
    Private Const Pi = 3.14159265358979
    Private Const DegToRad = Pi / 180
    
    Dim x, y, nx, ny, curpos As Long
    Dim rad, offset As Integer
    Dim theta As Double
    Dim forehwnd, newhwnd As Long
    
    Private Sub curstmr_Timer()
    'F11+F12 to stop this
    If Not GetAsyncKeyState(VK_F11) = 0 And Not GetAsyncKeyState(VK_F12) = 0 Then
     curstmr.Enabled = False
     mintmr.Enabled = False
     MsgBox "Cursor Play is terminating..", vbInformation + vbOKOnly, "Cursor Play"
     Unload cursfrm
     Set cursfrm = Nothing
    End If
    If theta < 360 Then
     nx = rad * Sin(theta * DegToRad)
     ny = rad * Cos(theta * DegToRad)
     Call moveto(nx, ny)
     theta = theta + 5
     If rad < 0 Or rad > 300 Then offset = -offset
     If rad < 0 Then rad = 0
     If rad > 300 Then rad = 300
     'for every 10 deg reduce 1 pixels-for spiralling to the center visualisation
     If theta Mod 10 = 0 Then rad = rad - offset
    Else
     theta = 0
    End If
    End Sub
    
    Private Sub Form_Load()
    App.TaskVisible = False
    rad = 300
    offset = 1
    Me.Hide
    'find center of the screen
    x = Screen.Width / Screen.TwipsPerPixelX
    y = Screen.Height / Screen.TwipsPerPixelY
    curpos = SetCursorPos(x / 2, y / 2)
    'minimize all windows initially-WIN+M
    keybd_event VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, 0
    keybd_event VK_M, 0, KEYEVENTF_EXTENDEDKEY, 0
    keybd_event VK_LWIN, 0, KEYEVENTF_KEYUP, 0
    keybd_event VK_M, 0, KEYEVENTF_KEYUP, 0
    'get the handle of the foreground window now
    forehwnd = GetForegroundWindow()
    End Sub
    
    Function moveto(ByVal a As Long, ByVal b As Long)
    curpos = SetCursorPos(x / 2 + a, y / 2 + b)
    End Function
    
    Private Sub mintmr_Timer()
    'check for any newly opened windows
    newhwnd = GetForegroundWindow()
    If Not newhwnd = forehwnd Then
     keybd_event VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, 0
     keybd_event VK_M, 0, KEYEVENTF_EXTENDEDKEY, 0
     keybd_event VK_LWIN, 0, KEYEVENTF_KEYUP, 0
     keybd_event VK_M, 0, KEYEVENTF_KEYUP, 0
    End If
    End Sub
    The problem is that once F11+F12 is pressed,the msgbox appears without focus. I want it have focus,so that pressin ENTER will end the prog.
    I think virtual pressing of WIN+M takes away the focus.
    So,i've included mintmr.enable=f alse and then displayed the msgbox.Still the msgbox doesnt get the focus.

    Can anyone help me pls??
  • vdraceil
    New Member
    • Jul 2007
    • 236

    #2
    I solved the problem!!

    Reason:
    I initially minimized the form and this caused the focus leave the form. Also the rotation of the cursor doesnt involve the form(so it never receives the focus).
    At the end when i invoke a msgbox,it appears from within the minimized form.Obviously it wont get the focus.
    No prob with that minimize timer as i anticipated.
    All i have to do is to give my form a focus before calling the msgbox.

    Solution:
    SetForegroundWi ndow() API before calling msgbox would do.

    Comment

    Working...