Results 1 to 19 of 19

Thread: ShowWindow can't bring to front[Resolved] :D

  1. #1

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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.

  2. #2
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: ShowWindow can't bring to front

    Can't help much with the C code but the SetWindowPos API sounds like what you need

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: ShowWindow can't bring to front

    SetWindowPos(hWnd, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE);

  4. #4

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: ShowWindow can't bring to front

    so what are the corresponding value of this variable?
    HWND_TOP,SWP_NOMOVE | SWP_NOSIZE?

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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;

  6. #6
    Lively Member
    Join Date
    May 2005
    Posts
    74

    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:
    1. SetWindowPos hWnd, HWND_TOPMOST, _
    2.                 0, _
    3.                 0, _
    4.                 0, _
    5.                 0, _
    6.                 SWP_NOMOVE Or SWP_NOSIZE

  7. #7
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: ShowWindow can't bring to front

    How about the SetForegroundWindow API? You can check if it's minimized or not with the IsIconic API and then you can call whichever API you need? (I don't know if SetForegroundWindow works with minimized windows)


    Has someone helped you? Then you can Rate their helpful post.

  8. #8

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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);
    		    	} 
    			}
    		}

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: ShowWindow can't bring to front

    I don't know C#, but this in VB works :

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    4. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    5. Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
    6.  
    7. Const SW_RESTORE As Long = 9
    8.  
    9. Private Sub Timer1_Timer()
    10.     If IsIconic(Me.hwnd) <> 0 Then
    11.         ShowWindow Me.hwnd, SW_RESTORE
    12.     Else
    13.        SetForegroundWindow Me.hwnd
    14.     End If
    15. End Sub

    Shouldn't be too hard to translate though


    Has someone helped you? Then you can Rate their helpful post.

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  11. #11

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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.

  12. #12

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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???

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: ShowWindow can't bring to front[Not Yet Resolved] :(

    Restore what window? MDI child, or parent?

  14. #14
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: ShowWindow can't bring to front[Not Yet Resolved] :(

    My way worked Maybe checking if it's minimized will help?


    Has someone helped you? Then you can Rate their helpful post.

  15. #15

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: ShowWindow can't bring to front[Not Yet Resolved] :(

    Quote 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?

  16. #16
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: ShowWindow can't bring to front[Not Yet Resolved] :(

    So finding the right handle is the problem?

    The IsIconic API


    Has someone helped you? Then you can Rate their helpful post.

  17. #17
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: ShowWindow can't bring to front[Not Yet Resolved] :(

    I know this is VB again and I'm not making your life much easier, but here is how you can find a window by the partial window caption

    http://www.vbforums.com/showthread.php?t=316924


    Has someone helped you? Then you can Rate their helpful post.

  18. #18

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: ShowWindow can't bring to front[Not Yet Resolved] :(

    thanks manavo. I try the IsIconic API.

  19. #19

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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
  •  



Click Here to Expand Forum to Full Width