473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

unhide window

Hi all,

how can I set an app's main window to visible from an other application?
My problem is, that I know only the handle of the other app's main
process, because the application's main window is set to hidden, so it
doesn't have a mainwindowHandle, so I can't send a message to it.

Any idea?

Thank's in advance,

Tamas Meszaros
Nov 17 '05 #1
8 11475
> because the application's main window is set to hidden, so it doesn't have a mainwindowHandle

The main window of an application cannot be "hidden" if it doesn't exist. If the app has a main window, that is not visible, then
it still has a handle.

To set the visibility of a window in another process using the handle of the window:

/// <summary>Sets the visibility of a window. This method allows one process to set the visiblity of a window owned by another
process.</summary>
/// <param name="cmd">SW_SHOW = 5, SW_HIDE = 0</param>
[System.Runtime.InteropServices.DllImport("user32.d ll", SetLastError=true)]
static extern bool ShowWindowAsync(IntPtr windowHandle, int cmd);
/// <summary>

/// Async version allows show window cross-process.

/// </summary>

[DllImport("user32.dll", SetLastError = true)]

private static extern bool ShowWindowAsync(IntPtr window, int cmd);
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Mészáros Tamás" <me*****@sch.bme.hu> wrote in message news:eB**************@TK2MSFTNGP15.phx.gbl...
Hi all,

how can I set an app's main window to visible from an other application? My problem is, that I know only the handle of the other
app's main process, because the application's main window is set to hidden, so it doesn't have a mainwindowHandle, so I can't send
a message to it.

Any idea?

Thank's in advance,

Tamas Meszaros

Nov 17 '05 #2
So, I try the following:

string proc=Process.GetCurrentProcess().ProcessName;
Process[] processes=Process.GetProcessesByName(proc);
if (processes.Length > 1)
{
Process p=Process.GetCurrentProcess();
int n=0;
if (processes[0].Id==p.Id)
{
n=1;
}
IntPtr hWnd=processes[n].MainWindowHandle;
...
}

But hWnd = 0, if the MainWindow is invisible, so I can't send a message
to it.

(My target is to show a previously launched instance of an application
if I try to start again. And this window can be hidden, not only minimized).

thanks

Tamas
Dave wrote:
because the application's main window is set to hidden, so it doesn't have a mainwindowHandle

The main window of an application cannot be "hidden" if it doesn't exist. If the app has a main window, that is not visible, then
it still has a handle.

To set the visibility of a window in another process using the handle of the window:

/// <summary>Sets the visibility of a window. This method allows one process to set the visiblity of a window owned by another
process.</summary>
/// <param name="cmd">SW_SHOW = 5, SW_HIDE = 0</param>
[System.Runtime.InteropServices.DllImport("user32.d ll", SetLastError=true)]
static extern bool ShowWindowAsync(IntPtr windowHandle, int cmd);
/// <summary>

/// Async version allows show window cross-process.

/// </summary>

[DllImport("user32.dll", SetLastError = true)]

private static extern bool ShowWindowAsync(IntPtr window, int cmd);

Nov 17 '05 #3
> But hWnd = 0, if the MainWindow is invisible, so I can't send a message to it.

No, hWnd is 0 if the MainWindow doesn't exist. It has not been created, or it was destroyed instead of being hidden.

The only way to show the window again is to recreate it.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Mészáros Tamás" <me*****@sch.bme.hu> wrote in message news:e2**************@TK2MSFTNGP09.phx.gbl...
So, I try the following:

string proc=Process.GetCurrentProcess().ProcessName;
Process[] processes=Process.GetProcessesByName(proc);
if (processes.Length > 1)
{
Process p=Process.GetCurrentProcess();
int n=0;
if (processes[0].Id==p.Id)
{
n=1;
}
IntPtr hWnd=processes[n].MainWindowHandle;
...
}

But hWnd = 0, if the MainWindow is invisible, so I can't send a message to it.

(My target is to show a previously launched instance of an application if I try to start again. And this window can be hidden, not
only minimized).

thanks

Tamas
Dave wrote:
because the application's main window is set to hidden, so it doesn't have a mainwindowHandle

The main window of an application cannot be "hidden" if it doesn't exist. If the app has a main window, that is not visible,
then it still has a handle.

To set the visibility of a window in another process using the handle of the window:

/// <summary>Sets the visibility of a window. This method allows one process to set the visiblity of a window owned by another
process.</summary>
/// <param name="cmd">SW_SHOW = 5, SW_HIDE = 0</param>
[System.Runtime.InteropServices.DllImport("user32.d ll", SetLastError=true)]
static extern bool ShowWindowAsync(IntPtr windowHandle, int cmd);
/// <summary>

/// Async version allows show window cross-process.

/// </summary>

[DllImport("user32.dll", SetLastError = true)]

private static extern bool ShowWindowAsync(IntPtr window, int cmd);


Nov 17 '05 #4
It's sure, it exists, I've only called a Hide() method on it, and hWnd
is 0 (I've tried it).

Can I catch messages somehow, which are sent to the process handle?
(wndproc handles messages sent to the window handle only)

thanks

Tamas
Dave wrote:
But hWnd = 0, if the MainWindow is invisible, so I can't send a message to it.

No, hWnd is 0 if the MainWindow doesn't exist. It has not been created, or it was destroyed instead of being hidden.

The only way to show the window again is to recreate it.

Nov 17 '05 #5
I just looked at your code to access the process that you posted earlier. It seems your attempting to acquire the current process:
Process p=Process.GetCurrentProcess();
Why not just use Process.GetCurrentProcess().MainWindowHandle?
I don't understand how that is useful. Isn't your code running in the main form?
It's sure, it exists, I've only called a Hide() method on it, and hWnd is 0 (I've tried it).
What did you try? A handle of 0 (zero) means that no window exists. (period).

Can I catch messages somehow, which are sent to the process handle?
(wndproc handles messages sent to the window handle only)
I believe you have to use Hooks for that... Check out MSDN for the Windows API functions pertaining to Messaging and Hooks. You'll
have to use interop.

You can use Spy++ to monitor messages sent to a window (tool that ships with C++).
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Mészáros Tamás" <me*****@sch.bme.hu> wrote in message news:Od*************@TK2MSFTNGP10.phx.gbl... It's sure, it exists, I've only called a Hide() method on it, and hWnd is 0 (I've tried it).

Can I catch messages somehow, which are sent to the process handle?
(wndproc handles messages sent to the window handle only)

thanks

Tamas
Dave wrote:
But hWnd = 0, if the MainWindow is invisible, so I can't send a message to it.

No, hWnd is 0 if the MainWindow doesn't exist. It has not been created, or it was destroyed instead of being hidden.

The only way to show the window again is to recreate it.

Nov 17 '05 #6
> I just looked at your code to access the process that you posted
earlier. It seems your attempting to acquire the current process:
Process p=Process.GetCurrentProcess();

Why not just use Process.GetCurrentProcess().MainWindowHandle?
I don't understand how that is useful. Isn't your code running in

the main form?

I use GetCurrentProcess( ).Processname to determine the name of the
process. Then I look for other running processes whit the same name. If
any other process exists whit the same name, then I would like to set
that other window visible, and close this one.

So I don't want to allow to run two instances from the same application.
When I try to execute the second one, I would like to set the first
one visible, and exit the second one.
It's sure, it exists, I've only called a Hide() method on it, and

hWnd is 0 (I've tried it).What did you try? A handle of 0 (zero) means that no window exists.

(period).

I started the application, then started an other instance of it, and the
second one could realize the windowhandle of the first one. Then I hid
the first app's main window (with Form.Hide( )), looked for the window
handle of it, but it was 0. I've tried "third party" applications as
well, and they found the handle to bo 0 as well. So if a window is
hidden, it seems not to have a windowhandle.

Tamas

Nov 17 '05 #7
If your want to ensure that only a single instance of your application may be executed at any given time, then use a Mutex object,
which can be shared across-process boundaries as a singleton:

private static Mutex SingleInstanceMutex;

/// <summary>Disposes the mutex object that ensures only a single instance of this application will execute at any given
time.</summary>
private static void SingleInstanceProcess_Exited(object sender, EventArgs e)
{
if (SingleInstanceMutex != null)
SingleInstanceMutex.Close();
}

public static void Main()
{
Process process = Process.GetCurrentProcess();
bool createdNew;
SingleInstanceMutex = new Mutex(true, process.ProcessName, out createdNew);

if (createdNew)
process.Exited+= new EventHandler(SingleInstanceProcess_Exited);
else
// Allow only a single instance of the app to execute
process.Kill();
}
So if a window is hidden, it seems not to have a windowhandle.


Incorrect. I attempted to reproduce the behavior your experiencing and I've done so with success. Take this code for example:

CheckHiddenHandleForm form = new CheckHiddenHandleForm();

Console.WriteLine("Show Form...");

form.Show();
System.Windows.Forms.Application.DoEvents();

Console.WriteLine("\tMainWindowHandle: {0}", Process.GetCurrentProcess().MainWindowHandle);
Console.WriteLine("\tForm.Handle: {0}", form.Handle);

Console.WriteLine("Hide Form...");

form.Hide();
System.Windows.Forms.Application.DoEvents();

Console.WriteLine("\tMainWindowHandle: {0}", Process.GetCurrentProcess().MainWindowHandle);
Console.WriteLine("\tForm.Handle: {0}", form.Handle);

Console.WriteLine("Close Form...");

form.Close();
System.Windows.Forms.Application.DoEvents();

Console.WriteLine("\tMainWindowHandle: {0}", Process.GetCurrentProcess().MainWindowHandle);
Outputs the following results to the Console window:
Show Form...
MainWindowHandle: 5308896 -- MainWindowHandle
Form.Handle: 5308896 -- Notice that the Form.Handle is the MainWindowHandle
Hide Form...
MainWindowHandle: 0 -- there is no longer a Main window for the process since the Form has been hidden
Form.Handle: 5308896 -- *** The window still has a handle ***
Close Form...
MainWindowHandle: 0 -- The window is now disposed (Form.Handle is now inaccessible and assumed
IntPtr.Zero)
Press any key to continue
As you can see, the window still has a handle, however, once it is hidden it is no longer the "MainWindow" of the process. You may
have to use Windows API calls to obtain the handle to the window, or just ignore it because it's hidden. That's what you're
attempting to accomplish anyway, correct?

I suggest looping through all of the processes with the same name if you do not want to ensure a single instance of the app using
the Mutex in my code example above. If the process Id is not of the current process, AND the MainWindowHandle is not IntPtr.Zero
then use my Interop code I supplied to you in a previous post to show the window.

GL

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Mészáros Tamás" <me*****@sch.bme.hu> wrote in message news:%2***************@TK2MSFTNGP10.phx.gbl...
I just looked at your code to access the process that you posted

earlier. It seems your attempting to acquire the current process:
Process p=Process.GetCurrentProcess();

Why not just use Process.GetCurrentProcess().MainWindowHandle?
I don't understand how that is useful. Isn't your code running in

the main form?

I use GetCurrentProcess( ).Processname to determine the name of the process. Then I look for other running processes whit the same
name. If any other process exists whit the same name, then I would like to set that other window visible, and close this one.

So I don't want to allow to run two instances from the same application. When I try to execute the second one, I would like to set
the first one visible, and exit the second one.
It's sure, it exists, I've only called a Hide() method on it, and

hWnd is 0 (I've tried it).
What did you try? A handle of 0 (zero) means that no window exists.

(period).

I started the application, then started an other instance of it, and the second one could realize the windowhandle of the first
one. Then I hid the first app's main window (with Form.Hide( )), looked for the window handle of it, but it was 0. I've tried
"third party" applications as well, and they found the handle to bo 0 as well. So if a window is hidden, it seems not to have a
windowhandle.

Tamas

Nov 17 '05 #8
As you can see, the window still has a handle, however, once it is hidden it is no longer the "MainWindow" of the process.


That's what I didn't know till now. But everything is clear now!

Thank's a lot for your help

Tamas
Nov 17 '05 #9

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

Similar topics

2
by: Peter K | last post by:
How do I Hide/Unhide the Main Database Window in VB?
4
by: caimito | last post by:
Hello to the group. I have been unsuccessful at unhiding the database window via code. The most I have gotten is to open the unhide dialogue from a command button on a form, but then I still have...
7
by: MLH | last post by:
Is there any case you can imagine that A97 would NOT display the Unhide option under the Window menu after running this line... Me.Visible = False Yes, its happening to me. The Unhide choice...
5
by: MLH | last post by:
I used this line in A2.0 to unhide the database window: DoCmd.DoMenuItem A_FORMBAR, 4, 4, 0, A_MENU_VER20 Although it will work in A97, it will also unhide anything else that's hidden on each...
4
by: g3000 | last post by:
I have seen examples of hiding and showing a div What I want to do is have a table with six columns. The sixth column is a check box to Unhide a div that is a form. I want it to show on top of...
5
by: victorcamp | last post by:
Applications I work with routinely hide the database window on Startup, which, once open, return this value: CurrentDb.Properties! = False During maintenance, I use a special keystroke to close...
3
by: toodi4 | last post by:
I'm using a javascript that hides and unhides text based on a button click. It works great across static fields on a form. The problem I have is that I'm trying to hide and unhide various fields...
3
by: =?Utf-8?B?aWxy?= | last post by:
Hi All I am developing an vb 2005 winforms application that connects to a database and displays a datagridview of the data. A user can select and then edit, or create a new entry by opening a...
1
by: Simon | last post by:
Dear reader, In a datasheet form you have the possibility to hide or unhide columns. But in case the datasheet form is a sub-form this functionality is not longer available.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.