Hi there,
I'm writing an application with forms. my main window can be hidden (minimize to a tray icon) like...Microsoft Outlook for example.
this application can run only one instance (again like outlook) - meaning that on initialization, I check if there is another process running and if so I exit from the new one.
in case the main window is hidden and the user tries to open the application again (from the start menu or desktop icon...), I want to re-display the main window and bring it to foreground as if it was restored from the tray icon (who's keep saying outlook???)
my problem is that I don't know how to do so. I can send a window message in case the main window is minimized by using windows API but how can this be done if the window is hidden????
Thanx in advance,
Uv
5 11375
Hi,
Try - using System;
-
using System.Diagnostics;
-
using System.Runtime.InteropServices;
-
class AppMain
-
{
-
[DllImport("user32.dll")] private static extern
-
bool SetForegroundWindow(IntPtr hWnd);
-
[DllImport("user32.dll")] private static extern
-
bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
-
[DllImport("user32.dll")] private static extern
-
bool IsIconic(IntPtr hWnd);
-
-
private const int SW_HIDE = 0;
-
private const int SW_SHOWNORMAL = 1;
-
private const int SW_SHOWMINIMIZED = 2;
-
private const int SW_SHOWMAXIMIZED = 3;
-
private const int SW_SHOWNOACTIVATE = 4;
-
private const int SW_RESTORE = 9;
-
private const int SW_SHOWDEFAULT = 10;
-
-
static void Main()
-
{
-
// get the name of our process
-
string proc=Process.GetCurrentProcess().ProcessName;
-
// get the list of all processes by that name
-
Process[] processes=Process.GetProcessesByName(proc);
-
// if there is more than one process...
-
if (processes.Length > 1)
-
{
-
// Assume there is our process, which we will terminate,
-
// and the other process, which we want to bring to the
-
// foreground. This assumes there are only two processes
-
// in the processes array, and we need to find out which
-
// one is NOT us.
-
-
// get our process
-
Process p=Process.GetCurrentProcess();
-
int n=0; // assume the other process is at index 0
-
// if this process id is OUR process ID...
-
if (processes[0].Id==p.Id)
-
{
-
// then the other process is at index 1
-
n=1;
-
}
-
// get the window handle
-
IntPtr hWnd=processes[n].MainWindowHandle;
-
// if iconic, we need to restore the window
-
if (IsIconic(hWnd))
-
{
-
ShowWindowAsync(hWnd, SW_RESTORE);
-
}
-
// bring it to the foreground
-
SetForegroundWindow(hWnd);
-
// exit our process
-
return;
-
}
-
// ... continue with your application initialization here.
-
}
-
}
AniJos
Thanx anijos for your reply but I've already done that. if the window is minimized it works fine but in my case the window is not visible and the window's handle return from the call IntPtr hWnd=processes[n].MainWindowHandle
is zero.
Thanx any way,
any other thoughts?
Uv
Try Mutex.
You tried this also?
anijos, again, thanks for your help.
this code won't contribute to the solution I seek for because the "already running" process's window is not visible, hence no handle is found. I think the solution should be something like firing an event to the process in order to notify it should restore the non-visible window. do you know if such thing can be done?
How about this. Identify the already existing process using code similar to that in the first sample posted by Anijos. Then use some code like the following sample to loop all windows and identify those that share the process ID, restoring them if necessary. You may want to tweak it slightly, i.e. to check whether a window is actually a notify icon or not, etc. -
[DllImport("user32.dll", SetLastError = true)]
-
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
-
-
[DllImport("user32.dll", SetLastError=true)]
-
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
-
-
[DllImport("user32.dll")]
-
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
-
private const int SW_RESTORE = 9;
-
-
public void restoreWindow(uint procIDtoFind)
-
{
-
uint processID;
-
IntPtr hwnd = IntPtr.Zero;
-
// Loop through windows
-
do
-
{
-
processID = 0;
-
// Get HWND
-
hwnd = FindWindowEx(IntPtr.Zero, hwnd, null, null);
-
// Get process ID
-
GetWindowThreadProcessId(hwnd, out processID);
-
if (processID == procIDtoFind)
-
{
-
// restore the window
-
ShowWindow(hwnd, SW_RESTORE);
-
break;
-
}
-
}
-
while (!hwnd.Equals(IntPtr.Zero));
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mountain Bikn' Guy |
last post by:
I am having serious problems with the following IDE bug:
Could not write to output file 'x.dll' -- 'The process cannot access the
file because it is being used by another process. '
and
BUG:...
|
by: mikes |
last post by:
Hello,
I am building a c# web app that uses serialization to generate xml
files within a page load event. The page support both creation and
modification events. I do not have a problem with the...
|
by: Corey Scheich |
last post by:
I am writing a simple exe that will locate a company file for the user with
minimal input and open it in the appropriate application (Same as Explorer)
Problem is sometimes the user may want to...
|
by: George |
last post by:
Hi,
I am re-writing part of my application using C#. This application starts
another process which execute a "legacy" program. This legacy program writes
to a log file and before it ends, it...
|
by: Victor Rosenberg |
last post by:
Hey guys
How can I painlessly execute some heavy processing in another process,
so the main application may still be closed, but the processing will
continue until its done?
Also, how can...
|
by: Builder |
last post by:
Is is possible to send events through application domain boundaries
(i.e. from one application domain to another)?
|
by: eashokan |
last post by:
Hi..
I am able to get the process (as object) running in the sysetm through process id. I need to trigger the event that corresponds to the process (object) i got. Note that the Process object i...
|
by: tamirmalas |
last post by:
Hi,
iam new to this forum, actually this is my first post, and i have a question,
how to read the innerhtml of a web browser created in another process (internet explorer or just a program that...
|
by: Tim Golden |
last post by:
aditya shukla wrote:
Why on earth are you *specifying* c:\python25 as the
directory for this file? It's automatically created
in your user-specific temp directory which has specific
permissions...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |