-
Aug 19th, 2005, 08:38 PM
#1
Thread Starter
Frenzied Member
ShowWindow can't bring to front[Resolved] :D
I already posted it in c# section. Since it's about API then I just post it here.
Code:
[DllImport("user32")]
public static extern int ShowWindow(int hwnd, int nCmdShow);
[DllImport("user32")]
public static extern int UpdateWindow(int hwnd);
Process[] p = Process.GetProcessesByName("WindowsApplication4");
int i=int.Parse(p[0].MainWindowHandle.ToString());
if(i != 0)
{
ShowWindow(i,9);
UpdateWindow(i);
}
I have that code that works fine in restoring a window(window in minimize mode) but when i didn't minimize the application just lost the focus it will not bring to front I tried all the combination of command show but still no luck.
Code:
hwnd
The handle of the window to change the show status of.
nCmdShow
Exactly one of the following flags specifying how to show the window:
SW_HIDE = 0
Hide the window.
SW_MAXIMIZE = 3
Maximize the window.
SW_MINIMIZE = 6
Minimize the window.
SW_RESTORE = 9
Restore the window (not maximized nor minimized).
SW_SHOW = 5
Show the window.
SW_SHOWMAXIMIZED = 3
Show the window maximized.
SW_SHOWMINIMIZED = 2
Show the window minimized.
SW_SHOWMINNOACTIVE = 7
Show the window minimized but do not activate it.
SW_SHOWNA = 8
Show the window in its current state but do not activate it.
SW_SHOWNOACTIVATE = 4
Show the window in its most recent size and position but do not activate it.
SW_SHOWNORMAL = 1
Show the window and activate it (as usual).
any other commands about bringing the apps to front when it is not in minimize mode?
Last edited by mar_zim; Aug 26th, 2005 at 09:39 PM.
-
Aug 19th, 2005, 11:56 PM
#2
Fanatic Member
Re: ShowWindow can't bring to front
Can't help much with the C code but the SetWindowPos API sounds like what you need
-
Aug 20th, 2005, 01:33 AM
#3
Re: ShowWindow can't bring to front
SetWindowPos(hWnd, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE);
-
Aug 20th, 2005, 02:46 AM
#4
Thread Starter
Frenzied Member
Re: ShowWindow can't bring to front
so what are the corresponding value of this variable?
HWND_TOP,SWP_NOMOVE | SWP_NOSIZE?
-
Aug 20th, 2005, 02:50 AM
#5
Re: ShowWindow can't bring to front
Don't you have them already defined?
Code:
#include "windows.h"
Then again, I don't know any C#.
The values are:
- HWND_TOP=0;
- SWP_NOMOVE=0x02;
- SWP_NOSIZE=0x01;
-
Aug 23rd, 2005, 01:21 AM
#6
Lively Member
Re: ShowWindow can't bring to front
You just write following code in form deactive event also. I have tried this, and it i working as you are demanding.
VB Code:
SetWindowPos hWnd, HWND_TOPMOST, _
0, _
0, _
0, _
0, _
SWP_NOMOVE Or SWP_NOSIZE
-
Aug 23rd, 2005, 03:04 AM
#7
-
Aug 23rd, 2005, 03:57 AM
#8
Thread Starter
Frenzied Member
Re: ShowWindow can't bring to front
manavo it will work when it's not in minimize mode.
do i have to call the 3 functions?
ShowWindow(hwnd,9);
SetForegroundWindow(hwnd);
UpdateWindow(hwnd);
It's so weird when I ran it many times the problem occur, it will not work as what I've expected.
here's the code:
pls.take easy on me.I'm new at this thingy.
Code:
[DllImport("user32")]
public static extern int ShowWindow(int hwnd, int nCmdShow);
[DllImport("user32")]
public static extern int UpdateWindow(int hwnd);
[DllImport("user32")]
public static extern int SetForegroundWindow(int hwnd);
[STAThread]
static void Main()
{
bool b;
Mutex m=new Mutex(true,typeof(Form1).FullName,out b);
if(b) Application.Run(new Form1());
else
{
Process[] p = Process.GetProcessesByName("WindowsApplication4");
int hwnd=int.Parse(p[0].MainWindowHandle.ToString());
if(hwnd != 0)
{
ShowWindow(hwnd,9);
SetForegroundWindow(hwnd);
UpdateWindow(hwnd);
}
}
}
-
Aug 23rd, 2005, 05:49 AM
#9
Re: ShowWindow can't bring to front
I don't know C#, but this in VB works :
VB Code:
Option Explicit
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Const SW_RESTORE As Long = 9
Private Sub Timer1_Timer()
If IsIconic(Me.hwnd) <> 0 Then
ShowWindow Me.hwnd, SW_RESTORE
Else
SetForegroundWindow Me.hwnd
End If
End Sub
Shouldn't be too hard to translate though
Has someone helped you? Then you can Rate their helpful post.
-
Aug 23rd, 2005, 09:15 AM
#10
Re: ShowWindow can't bring to front
In order for you to show the window, it would seem to me that you would first have to find the window.
Trying finding it with the FindWindow API. Once you have the window handle, pass that to the ShowWindow API.
-
Aug 23rd, 2005, 08:13 PM
#11
Thread Starter
Frenzied Member
Re: ShowWindow can't bring to front
now Resolved. I used 3 external function of API(showwindow,findwindow and setforegroundwindow)
Showwindow will use when the window is in minimize mode.
setforegroundwindow will use when the window is not in minimize mode.
int hwnd=FindWindow(null,"Form1");
SetForegroundWindow(hwnd);
ShowWindow(hwnd,9);
thanks guys for the assistance.
-
Aug 25th, 2005, 01:32 AM
#12
Thread Starter
Frenzied Member
Re: ShowWindow can't bring to front[Resolved]
doh! I found my code won't work fluently.
here's the code
Code:
[DllImport("user32")]
private static extern int FindWindow(string s,string ss);
[DllImport("user32")]
private static extern int SetForegroundWindow(int hwnd);
[DllImport("user32")]
private static extern int ShowWindow(int hwnd,int cmd);
public CreateInstance(Form form, string fullname)
{
bool flag;
Mutex m=new Mutex(true,fullname,out flag);
if(flag) Application.Run(form);
else
{
int hwnd=FindWindow(null,form.Text);
SetForegroundWindow(hwnd);
ShowWindow(hwnd,9);
}
}
here's the scenario: I have an mdiform that have lots of mdichildren.
It works fine when there are no mdichild open but when I start opening another an mdiform there the problem occur it will not restore the window/be on top of other applications.
What could be the problems???
-
Aug 25th, 2005, 04:09 AM
#13
Re: ShowWindow can't bring to front[Not Yet Resolved] :(
Restore what window? MDI child, or parent?
-
Aug 25th, 2005, 11:46 AM
#14
-
Aug 25th, 2005, 08:30 PM
#15
Thread Starter
Frenzied Member
Re: ShowWindow can't bring to front[Not Yet Resolved] :(
Originally Posted by penagate
Restore what window? MDI child, or parent?
the mdi parent.
It works fine when there are no mdi child open because the findwindow function find the title bar of the window.
i.e
first run the title bar (Main Application)
when i open an mdi child the title bar will change. The title of the mdi child append to the title bar of mdi parent because I set the windowstate of the mdi child maximize but when the user set the mdi child to its actual size then the title change again(it removes it's title to the title bar of the mdi parent).
title bar of mdiparent(child maximize mode)(Main Application-[First Form])
title bar of mdiparent(child actual size(not in maximize mode))(Main Application)
and oh manavo how would I check the window state?
-
Aug 25th, 2005, 08:36 PM
#16
-
Aug 25th, 2005, 08:38 PM
#17
-
Aug 25th, 2005, 08:58 PM
#18
Thread Starter
Frenzied Member
Re: ShowWindow can't bring to front[Not Yet Resolved] :(
thanks manavo. I try the IsIconic API.
-
Aug 26th, 2005, 09:37 PM
#19
Thread Starter
Frenzied Member
Re: ShowWindow can't bring to front[Not Yet Resolved] :(
thanks guys for the help. Now I get it right.
Code:
Process[] p=Process.GetProcessesByName(processname);
if(p.Length > 1)
{
IntPtr hwnd=p[0].MainWindowHandle;
if(IsIconic(hwnd)!=0)ShowWindow(hwnd,9);
else SetForegroundWindow(hwnd);
}
else Application.Run(form);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|