activate a window using a window handler in .NET

M

Michael Wu

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 native support for that within .NET, instead having to
call WIN32 API.

Regards

Michael
 
D

Dmitriy Lapshin [C# / .NET MVP]

Michael,

You can try the Form.Activate() method, but I am not sure this is the exact
equivalent to the SetForegroundWindow() API.
 
S

smith

Still Win32.

Are you looking to just bring a window up?
Does ShowWindowAsync work for what you are doing? This will bring it to
the top of the z-order and restore it if minimized.


Public Const SW_RESTORE = 9
Public Const HWND_TOP = 0
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1

Declare Function ShowWindowAsync Lib "user32" (ByVal hwnd As Integer, ByVal
nCmdShow As Integer) As Integer

Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Integer, ByVal
hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx
As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer

ShowWindowAsync(temphwnd, SW_RESTORE)
'bring it to the top of the z-order
SetWindowPos(temphwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
'unlock it where it is
SetWindowPos(temphwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
 
M

Michael Wu

//Win32APIs.ShowWindowAsync(whdl.ToInt32(), Win32APIs.SC_RESTORE);

doen'st work, but the following 2 lines did work. Any idea why?

Win32APIs.SetForegroundWindow(whdl.ToInt32());

Win32APIs.SendNotifyMessage(whdl.ToInt32(), Win32APIs.WM_SYSCOMMAND,
Win32APIs.SC_RESTORE, 0);
 
T

Tian Min Huang

Hi,

The SetForegroundWindow() function is used to put a Window into the
foreground and activate it. In .NET class library, as suggested by Dmitriy,
you can use Form.Activate() method. Please refer to the following MSDN
article:

Form.Activate Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformsformclassactivatetopic.asp

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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