Activate a window to forground using API

D

D Witherspoon

I developed this module, examples on the internet seem to be much longer..
Can someone explain to me why my solution doesn't work?



Module modAPI

Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As
Long



Public Sub ActivateAppWindow()

SetForegroundWindow(fMain.Handle.ToInt32)

End Sub

End Module
 
K

Ken Tucker [MVP]

Hi,

API declare is wrong. Types have changed since vb6. Use integer
instead of long. In this case i would use intptr.

Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As intptr) As
integer

Public Sub ActivateAppWindow()

SetForegroundWindow(fMain.Handle)

End Sub


Ken
----------------

I developed this module, examples on the internet seem to be much longer..
Can someone explain to me why my solution doesn't work?



Module modAPI

Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As
Long



Public Sub ActivateAppWindow()

SetForegroundWindow(fMain.Handle.ToInt32)

End Sub

End Module
 
C

Crouchie1998

Module Module1
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As
Integer) As Integer

Public Sub SetForeground(ByVal iHandle As Integer)
SetForegroundWindow(iHandle)
End Sub
End Module

Usage:
-------

SetForeground (frm.Handle.Int32)

'frm' is the Form to set as the foreground window

Example:
---------

Declaration:
------------

Dim frm As New Form2

Behind Button1 (for example):
------------------------------

frm.Show()

Behind Button2 (for example):
------------------------------

SetForeground (frm.Handle.Int32)

Crouchie1998
BA (HONS) MCP MCSE
 
H

Herfried K. Wagner [MVP]

Ken Tucker said:
API declare is wrong. Types have changed since vb6. Use integer
instead of long. In this case i would use intptr.

Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As intptr)
As
integer

Public Sub ActivateAppWindow()

SetForegroundWindow(fMain.Handle)

Note that this will only work as long as your application is the active
application. Otherwise 'AttachThreadInput' is needed too. In other words,
it's not as easy (and not recommended) to "steal" the focus from another
application.
 
H

Herfried K. Wagner [MVP]

Crouchie1998 said:
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As
Integer) As Integer

I prefer this declaration/usage, although your code should work:

\\\
Public Declare Function SetForegroundWindow Lib "user32.dll" ( _
ByVal hWnd As IntPtr _
) As Boolean
..
..
..
Dim Success As Boolean = SetForegroundWindow(frm.Handle)
///

Just my 2 Euro cents...
 
C

Cor Ligthert

D,

These questions make me forever curious.

Although as Herfried already said have I the same opinion that you should
avoid this kind of instruction.

Why not just
me.topmost = true

Cor
 
H

Herfried K. Wagner [MVP]

Cor,

Cor Ligthert said:
Although as Herfried already said have I the same opinion that you should
avoid this kind of instruction.

Why not just
me.topmost = true

I doubt that a topmost window is a viable solution in this situation...
 
C

Cor Ligthert

Cor Ligthert said:
I doubt that a topmost window is a viable solution in this situation...


After that I had sent this message I thought,
me.topmost = true
me.topmost = false

Maybe does that the job that is asked, however I am more interested in that
statusbar problem, but the information from the OP is to few and I think
that this is a on the border solution.

Cor
 
D

D Witherspoon

Me.TopMost will make a form the topmost in the application but not over
other applications.

When you refer to the status bar problem are you talking about my problem
with the app not showing up in the taskbar.

It's tricky and yes, is the only reason I have to resort to trying to bring
the app window into the foreground. Unfortunatly no luck yet with the
status bar thing.
 
D

D Witherspoon

Not recommended as in technically or morally ?


Herfried K. Wagner said:
Note that this will only work as long as your application is the active
application. Otherwise 'AttachThreadInput' is needed too. In other
words, it's not as easy (and not recommended) to "steal" the focus from
another application.
 
C

Cor Ligthert

D,
Me.TopMost will make a form the topmost in the application but not over
other applications.
Than I have probably another system. When I use topmost = true I get the
form only from my screen when I minimalize it.

Cor
 
H

Herfried K. Wagner [MVP]

D Witherspoon said:
Not recommended as in technically or morally ?

Morally -- I doubt that the application will pass the Windows Logo test when
it's stealing the focus.
 
H

Herfried K. Wagner [MVP]

Cor,

Cor Ligthert said:
Than I have probably another system. When I use topmost = true I get the
form only from my screen when I minimalize it.

ACK. MSDN:

| A topmost form is a form that overlaps all the other (non-topmost)
| forms even if it is not the active or foreground form. Topmost forms
| are always displayed at the highest point in the Z-order of the
| windows on the desktop.
 
D

D Witherspoon

Thats a good point.

But until I can find out why it deosn't show up in the task (when it first
opens) bar I will have to do this.
 
D

D Witherspoon

It works now without even requiring 'AttachThreadInput'. I changed the
declare to return an Interger instead of a Long. Seemed to fix it and
return 1 as the return code instead of a crazy long number.

Even if it isn't the active application it still works, 'AttachThreadInput'
doesn't seem to be required.


D Witherspoon said:
Thats a good point.

But until I can find out why it deosn't show up in the task (when it first
opens) bar I will have to do this.
 
H

Herfried K. Wagner [MVP]

D Witherspoon said:
It works now without even requiring 'AttachThreadInput'. I changed the
declare to return an Interger instead of a Long. Seemed to fix it and
return 1 as the return code instead of a crazy long number.

Even if it isn't the active application it still works,
'AttachThreadInput' doesn't seem to be required.

What Windows version are you using?
 
H

Herfried K. Wagner [MVP]

D Witherspoon said:
XP Pro SP2

Mhm... Normally your application's taskbar button should flash when calling
'SetForegroundWindow' when the application is not the active application.

MSDN on 'SetForegroundWindow':

| With this change, an application cannot force a window to
| the foreground while the user is working with another window.
 
D

D Witherspoon

That's interesting.

I also tried FlashWindow API as well to see if I could just flash the
taskbar instead, but still because on startup there is no item for the
window showing up in the taskbar this doesn't work. When calling
SetForegroundWindow the window will pop to the front when the window is done
loading and it will show up on the taskbar. It's unfortunate that we have
to do it this way, but it's an application that runs within our office
enviroment, so I've made a decision that it's fine.

I would love to find out why it doesn't show up in the taskbar. Could it be
partly due to the 3rd party infragistics controls we're using, or that it's
MDI, or that it's being launched as a no ntouch deployment app frmo IE? I
have no idea. Uunfortunatly in IT, you never have enough time to find out
why these bugs are they. Either fix it or find a workaround... and move
on... unfortunatly..
 

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