J
Jeff0803
Guest
I made a sample program which is minimized to the System Tray.(MyProgB)
Form1 has one button(bttnMinimize) which minimize program to the System Tray.
Here is the sample code.
//MyProgB
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Resize(object sender, EventArgs e)
{
notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
notifyIcon1.BalloonTipText = "You have successfully minimized your form.";
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
private void bttnMinimize_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
}
Problem occurs when MyProgB is activated from another program(MyProgA).
If bring front MyProgB from other program(MyProgA), then, MyProgB is not minimized.
If click bttnMyProgB in the MyProgA, then bring myPtogB to the front.
Here is MyProgA.
//MyProgA
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr handle);
const int SW_RESTORE = 9;
public Form1()
{
InitializeComponent();
}
private void bttnMyProgB_Click(object sender, EventArgs e)
{
IntPtr handle = (IntPtr)FindWindow(null, "MyProgB");
if (IsIconic(handle))
{
ShowWindow(handle, SW_RESTORE);
}
SetForegroundWindow(handle);
}
}
MyProgB works well when run alone.
1. Start MyProgB
2. Press [Minimize] button and then it is minimized to the System Tray
3. Doubleclick icon from the System Tray and window appear.
4. Press [Minimize] button
Works fine!
However, it doesn't work correctly when run with MyProgA like following.
1. Start MyProgB
2. Start MyProgA
3. Press [Minimize] button from MyProgB and then it is minimized to the System Tray
4. Doubleclick icon from the System Tray and window appear.
5. Press [Minimize] button from MyProgB and then it is minimized to the System Tray => so far works well!
6. Press [bttnMyProgB] button and then MyProgB sppear.
7. Press [Minimize] button from MyProgB and then it is not minimized to the System Tray => problem occur here!!!
Can anybody resolve this problem?
Continue reading...
Form1 has one button(bttnMinimize) which minimize program to the System Tray.
Here is the sample code.
//MyProgB
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Resize(object sender, EventArgs e)
{
notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
notifyIcon1.BalloonTipText = "You have successfully minimized your form.";
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
private void bttnMinimize_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
}
Problem occurs when MyProgB is activated from another program(MyProgA).
If bring front MyProgB from other program(MyProgA), then, MyProgB is not minimized.
If click bttnMyProgB in the MyProgA, then bring myPtogB to the front.
Here is MyProgA.
//MyProgA
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr handle);
const int SW_RESTORE = 9;
public Form1()
{
InitializeComponent();
}
private void bttnMyProgB_Click(object sender, EventArgs e)
{
IntPtr handle = (IntPtr)FindWindow(null, "MyProgB");
if (IsIconic(handle))
{
ShowWindow(handle, SW_RESTORE);
}
SetForegroundWindow(handle);
}
}
MyProgB works well when run alone.
1. Start MyProgB
2. Press [Minimize] button and then it is minimized to the System Tray
3. Doubleclick icon from the System Tray and window appear.
4. Press [Minimize] button
Works fine!
However, it doesn't work correctly when run with MyProgA like following.
1. Start MyProgB
2. Start MyProgA
3. Press [Minimize] button from MyProgB and then it is minimized to the System Tray
4. Doubleclick icon from the System Tray and window appear.
5. Press [Minimize] button from MyProgB and then it is minimized to the System Tray => so far works well!
6. Press [bttnMyProgB] button and then MyProgB sppear.
7. Press [Minimize] button from MyProgB and then it is not minimized to the System Tray => problem occur here!!!
Can anybody resolve this problem?
Continue reading...