Hi there,
I'm writing an application with forms. my main window can be hidden (minimize to a tray icon) like...Microsof t 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 11447
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].MainWindowHand le
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: "Could Not Copy Temporary Files to the Output Directory" Error Message
When You Build a Solution That Contains Multiple Projects
I have tried all the solutions in Microsoft Knowledge Base Article - 313512.
|
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 creation
function. When I resubmit the page to mod the previously created xml
file I get "file xxxx is being used by another process." It appears
that all object have be correctly closed but the XmlTextWriter or the
FileStream objects have not...
|
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 open a few files. I was opening
them in a For....Next loop and it seems that if a file takes longer than a
few seconds to open it is dropping the other files. I presume that I need
to wait for the process that was sent to stop I think what I need...
|
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 writes a specific string to the log file.
My original program (MKS Toolkit shell program) which keeps running "grep"
checking the "exit string" on the "log files". There are no file sharing
problem.
|
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 progressing events be implemented in this scenario?
Thanks in advance
| |
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 get is not started by me or started in separate application.
In my application, i need to get the object and the trigger of the process events - like process.Exited.
code snippet,
AddHandler gService.toolProc.Exited, AddressOf...
|
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 uses the web browser)?
i saw some code on the net that uses the
SHDocVw.ShellExplorer
but it wasnt helpfull because it reads only the internet explorer documents (not the web browsers in created in another programs)
|
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 and is (obviously) specific to that user.
You may -- depending on your needs -- also not need the
suffix & prefix.
ie, unless you have some specific need in mind, just use:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |