Skip to content

Commit e6947d4

Browse files
committed
Improved instance handling in AppMan, closes #642.
1 parent 9fe54cd commit e6947d4

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

External/Tools/AppMan/MainForm.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,33 @@ public MainForm(String[] args)
7777
if (!Win32.IsRunningOnMono) Application.AddMessageFilter(this);
7878
}
7979

80+
#region Instancing
81+
82+
/// <summary>
83+
/// Handle the instance message
84+
/// </summary>
85+
protected override void WndProc(ref Message m)
86+
{
87+
if (m.Msg == Win32.WM_SHOWME) this.RestoreWindow();
88+
base.WndProc(ref m);
89+
}
90+
91+
/// <summary>
92+
/// Restore the window of the first instance
93+
/// </summary>
94+
private void RestoreWindow()
95+
{
96+
if (this.WindowState == FormWindowState.Minimized)
97+
{
98+
this.WindowState = FormWindowState.Normal;
99+
}
100+
Boolean top = this.TopMost;
101+
this.TopMost = true;
102+
this.TopMost = top;
103+
}
104+
105+
#endregion
106+
80107
#region Initialization
81108

82109
/// <summary>

External/Tools/AppMan/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ static void Main(String[] args)
2828
}
2929
else if (Array.IndexOf(args, "-minimized") == -1)
3030
{
31-
MessageBox.Show("AppMan is already running.");
31+
if (Win32.IsRunningOnMono) MessageBox.Show("AppMan is already running.");
32+
else Win32.PostMessage((IntPtr)Win32.HWND_BROADCAST, Win32.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
3233
}
3334
}
35+
3436
}
37+
3538
}
39+

External/Tools/AppMan/Win32.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ namespace AppMan
1111
public class Win32
1212
{
1313
/// <summary>
14-
/// Tells if we are running on Mono runtime
14+
/// Public static props
1515
/// </summary>
16-
public static Boolean IsRunningOnMono = Type.GetType("Mono.Runtime") != null;
16+
public static Boolean IsRunningOnMono;
17+
public static Int32 HWND_BROADCAST = 0xffff;
18+
public static Int32 WM_SHOWME;
19+
20+
static Win32()
21+
{
22+
IsRunningOnMono = Type.GetType("Mono.Runtime") != null;
23+
if (IsRunningOnMono) WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
24+
}
1725

1826
#region Externs
1927

@@ -24,10 +32,16 @@ public class Win32
2432
public static extern IntPtr SendMessage(IntPtr hWnd, Int32 msg, IntPtr wp, IntPtr lp);
2533

2634
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
27-
internal static extern uint GetFullPathName(string lpFileName, uint nBufferLength, StringBuilder lpBuffer, IntPtr mustBeNull);
35+
internal static extern UInt32 GetFullPathName(String lpFileName, UInt32 nBufferLength, StringBuilder lpBuffer, IntPtr mustBeNull);
2836

2937
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
30-
internal static extern SafeFileHandle CreateFile(string lpFileName, EFileAccess dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
38+
internal static extern SafeFileHandle CreateFile(String lpFileName, EFileAccess dwDesiredAccess, UInt32 dwShareMode, IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);
39+
40+
[DllImport("user32.dll")]
41+
public static extern Boolean PostMessage(IntPtr hwnd, Int32 msg, IntPtr wparam, IntPtr lparam);
42+
43+
[DllImport("user32.dll")]
44+
public static extern Int32 RegisterWindowMessage(String message);
3145

3246
#endregion
3347

Binary file not shown.

0 commit comments

Comments
 (0)