472,371 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

Activate a window to forground using API

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
Nov 21 '05 #1
18 2608
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
----------------

"D Witherspoon" <dw**********@noway.org> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #2
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
Nov 21 '05 #3
"Ken Tucker [MVP]" <vb***@bellsouth.net> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
"Crouchie1998" <cr**********@spamcop.net> schrieb:
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...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
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
Nov 21 '05 #6
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
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...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7
> "Cor Ligthert" <no************@planet.nl> schrieb:
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...

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
Nov 21 '05 #8
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.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Cor Ligthert" <no************@planet.nl> schrieb:
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...

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

Nov 21 '05 #9
Not recommended as in technically or morally ?
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
"Ken Tucker [MVP]" <vb***@bellsouth.net> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #10
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
Nov 21 '05 #11
"D Witherspoon" <dw**********@noway.org> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #12
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
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.


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #13
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.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
"D Witherspoon" <dw**********@noway.org> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #14
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" <dw**********@noway.org> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
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.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
"D Witherspoon" <dw**********@noway.org> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Nov 21 '05 #15
"D Witherspoon" <dw**********@noway.org> schrieb:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #16
XP Pro SP2

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eS****************@TK2MSFTNGP09.phx.gbl...
"D Witherspoon" <dw**********@noway.org> schrieb:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #17
"D Witherspoon" <dw**********@noway.org> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #18
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..

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
"D Witherspoon" <dw**********@noway.org> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Milon | last post by:
Hi all, I am new to Python, and I just wrote a simple script to launch an application from the windows's start menu's run command. (see code as follow) There is an "OK" button on the new...
3
by: Michael Wu | last post by:
I wonder if anyone know how to activate a window with a given window handler. I used to do this with the win32 API static native boolean SetForegroundWindow(int hwnd); I wonder if there is...
18
by: D Witherspoon | last post by:
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...
1
by: Lex | last post by:
I want to write an app in C# that keeps track of when the foreground window in the OS changes. Is there an event that I can "susbscribe" to for this? Or am I stuck polling using the...
10
by: zhou | last post by:
in Visual Basic ,I use Appactivate(AcadApp.caption);
5
by: Ivan V via DotNetMonster.com | last post by:
Hi I would like to knwo if I could activate the up and down arrow in a window form which have the same function as using the tab button. Thanks for all! best rgds, Ivan Vong -- Message...
2
by: Dave Booker | last post by:
I have a Windows form application that I generally run minimized. When a critical event occurs it instantiates another "Alert" form which I want to grab the user focus. In the Alert constructor...
1
by: pearturtle123 | last post by:
Hi, I am new to Python and I am using PAMIE to manipulate IE browser. Currently, I have IE window 1 launched, after I clicked on a link on window 1, a second IE window 2 isopened. Then, I want...
0
IonutZ
by: IonutZ | last post by:
VB.NET 2008-FORM: Getting a window to forground while only knowing its partial name. Hi guys :) I have the name of an application, it starts with ACI and I want to set it to my forground in...
10
by: Gary | last post by:
I'm trying to find a way to determin which window Within-Another-Program currently has the focus. THE SITUATION: (Skip down to "My Question" if you don't want the background...) * This is...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.