Popup program focus issue

J

James Pyrich

Greetings:

I am using .NET to pop up a graphical window for a legacy application
(remember the IBM System/23?). In order to achieve good performance, I
created a GUI Broker Server and a Client Stub. The Client just uses the
Win32 API and is very fast. The Server runs in the background, using .NET
(thus eliminating the startup time for an individual application).

The performance is very good, no problems there. However, I am having
an issue with focusing as the window spawned by the Server does not come to
the front.

I *would* feel stupid for asking this question, but I haven't done a lot
of Windows programming before, .NET or otherwise.

So far, I've tried executing Me.Focus in various Form events, but that
doesn't appear to do anything. My next guess is to call focus somehow for
the Server, but that never has a form showing.

Basically, my question is, how do I force an application to pop up to
the front of all other windows?

Thanks,

James
 
O

One Handed Man \( OHM - Terry Burns \)

try

me.activate()

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
O

One Handed Man \( OHM - Terry Burns \)

O, then can you maximise or restore after the activate

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
J

James Pyrich

Nope, still having trouble with it.

Is there a VB method which tells the Windows OS to push that window to the
front and give it focus? Perhaps a Windows API call?

James
 
C

Cor Ligthert

James,

You want to make this iritating program?

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim tim As New Windows.Forms.Timer
AddHandler tim.Tick, AddressOf tim_Tick
tim.Interval = 1000
tim.Enabled = True
End Sub
Private Sub tim_Tick(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.Activate()
Me.CenterToScreen()
Me.WindowState = FormWindowState.Normal
End Sub
///

I hope you do not want it?

:)

Cor
 
J

James Pyrich

Yeah, that's ugly... and I tried it... and it doesn't work!

It just flashes in the taskbar--it doesn't actually bring the window to
focus.

The window is on top of all the other windows, but it's got to be able to
accept keyboard input (and on Windows NT4, it *is* behind the other
windows).

Is this because, say, it sort of has focus, but it doesn't somehow? Maybe I
can have Windows think I clicked on it with the mouse somehow?

James
 
O

One Handed Man \( OHM - Terry Burns \)

Maybe I am missing something here. Please give me the code you are running
on the server to launch the form

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
J

James Pyrich

I finally came up with the answer, and it involved a little Windows API
magic.

I found it here: http://www.thecodeproject.com/vb/net/SpokenWord_1_0.asp

He had some different requirements, but I was able to extract what I needed
from his code.

My final version is here:

Class Utility:
///
Private Declare Function GetCurrentThreadId Lib "kernel32" () As IntPtr

Private Declare Function AttachThreadInput Lib "user32" _
(ByVal idAttach As IntPtr, _
ByVal idAttachTo As IntPtr, _
ByVal fAttach As Boolean) As Boolean

Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal lpdwProcessId As IntPtr) As IntPtr

Private Declare Function SetForegroundWindow Lib "user32" (ByVal ByValhWnd
As IntPtr) As Boolean

Private Declare Function GetForegroundWindow Lib "user32" () As IntPtr

Public Shared Sub DoForceFocus(ByVal t As Timer, ByVal f As Form)
Dim foregroundThread As IntPtr =
GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero)
Dim currentThread As IntPtr = GetCurrentThreadId()
If Not foregroundThread.Equals(currentThread) Then
AttachThreadInput(foregroundThread, currentThread, True)
Else
' we have focus. disable timer
t.Enabled = False
End If

SetForegroundWindow(f.Handle)

If Not foregroundThread.Equals(currentThread) Then
AttachThreadInput(foregroundThread, currentThread, False)
End If

End Sub

///

In my form class:
///
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles TimerFocus.Tick
Me.Activate()
Utility.DoForceFocus(TimerFocus, Me)
End Sub

///

This accomplishes what I needed. (Sorry for not replying right away.)

James Pyrich
 
O

One Handed Man \( OHM - Terry Burns \)

Good to hear you solved it, sorry I was not able to expidite a solution for
you.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top