mirror of
https://github.com/openhardwaremonitor/openhardwaremonitor
synced 2025-09-01 14:55:13 +00:00
Added a wrapper for the NotifyIconAdv to use the normal NotifyIcon class on Linux systems and the (fixed) custom implementation on Windows systems.
This commit is contained in:
@@ -16,7 +16,313 @@ using System.Reflection;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace OpenHardwareMonitor.GUI {
|
||||
public class NotifyIconAdv : Component {
|
||||
|
||||
public class NotifyIconAdv : IDisposable {
|
||||
|
||||
private NotifyIcon genericNotifyIcon;
|
||||
private NotifyIconWindowsImplementation windowsNotifyIcon;
|
||||
|
||||
public NotifyIconAdv() {
|
||||
int p = (int)Environment.OSVersion.Platform;
|
||||
if ((p == 4) || (p == 128)) { // Unix
|
||||
genericNotifyIcon = new NotifyIcon();
|
||||
} else { // Windows
|
||||
windowsNotifyIcon = new NotifyIconWindowsImplementation();
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler BalloonTipClicked {
|
||||
add {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.BalloonTipClicked += value;
|
||||
else
|
||||
windowsNotifyIcon.BalloonTipClicked += value;
|
||||
}
|
||||
remove {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.BalloonTipClicked -= value;
|
||||
else
|
||||
windowsNotifyIcon.BalloonTipClicked -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler BalloonTipClosed {
|
||||
add {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.BalloonTipClosed += value;
|
||||
else
|
||||
windowsNotifyIcon.BalloonTipClosed += value;
|
||||
}
|
||||
remove {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.BalloonTipClosed -= value;
|
||||
else
|
||||
windowsNotifyIcon.BalloonTipClosed -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler BalloonTipShown {
|
||||
add {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.BalloonTipShown += value;
|
||||
else
|
||||
windowsNotifyIcon.BalloonTipShown += value;
|
||||
}
|
||||
remove {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.BalloonTipShown -= value;
|
||||
else
|
||||
windowsNotifyIcon.BalloonTipShown -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler Click {
|
||||
add {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.Click += value;
|
||||
else
|
||||
windowsNotifyIcon.Click += value;
|
||||
}
|
||||
remove {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.Click -= value;
|
||||
else
|
||||
windowsNotifyIcon.Click -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler DoubleClick {
|
||||
add {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.DoubleClick += value;
|
||||
else
|
||||
windowsNotifyIcon.DoubleClick += value;
|
||||
}
|
||||
remove {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.DoubleClick -= value;
|
||||
else
|
||||
windowsNotifyIcon.DoubleClick -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public event MouseEventHandler MouseClick {
|
||||
add {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.MouseClick += value;
|
||||
else
|
||||
windowsNotifyIcon.MouseClick += value;
|
||||
}
|
||||
remove {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.MouseClick -= value;
|
||||
else
|
||||
windowsNotifyIcon.MouseClick -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public event MouseEventHandler MouseDoubleClick {
|
||||
add {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.MouseDoubleClick += value;
|
||||
else
|
||||
windowsNotifyIcon.MouseDoubleClick += value;
|
||||
}
|
||||
remove {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.MouseDoubleClick -= value;
|
||||
else
|
||||
windowsNotifyIcon.MouseDoubleClick -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public event MouseEventHandler MouseDown {
|
||||
add {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.MouseDown += value;
|
||||
else
|
||||
windowsNotifyIcon.MouseDown += value;
|
||||
}
|
||||
remove {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.MouseDown -= value;
|
||||
else
|
||||
windowsNotifyIcon.MouseDown -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public event MouseEventHandler MouseMove {
|
||||
add {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.MouseMove += value;
|
||||
else
|
||||
windowsNotifyIcon.MouseMove += value;
|
||||
}
|
||||
remove {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.MouseMove -= value;
|
||||
else
|
||||
windowsNotifyIcon.MouseMove -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public event MouseEventHandler MouseUp {
|
||||
add {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.MouseUp += value;
|
||||
else
|
||||
windowsNotifyIcon.MouseUp += value;
|
||||
}
|
||||
remove {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.MouseUp -= value;
|
||||
else
|
||||
windowsNotifyIcon.MouseUp -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public string BalloonTipText {
|
||||
get {
|
||||
if (genericNotifyIcon != null)
|
||||
return genericNotifyIcon.BalloonTipText;
|
||||
else
|
||||
return windowsNotifyIcon.BalloonTipText;
|
||||
}
|
||||
set {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.BalloonTipText = value;
|
||||
else
|
||||
windowsNotifyIcon.BalloonTipText = value;
|
||||
}
|
||||
}
|
||||
|
||||
public ToolTipIcon BalloonTipIcon {
|
||||
get {
|
||||
if (genericNotifyIcon != null)
|
||||
return genericNotifyIcon.BalloonTipIcon;
|
||||
else
|
||||
return windowsNotifyIcon.BalloonTipIcon;
|
||||
}
|
||||
set {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.BalloonTipIcon = value;
|
||||
else
|
||||
windowsNotifyIcon.BalloonTipIcon = value;
|
||||
}
|
||||
}
|
||||
|
||||
public string BalloonTipTitle {
|
||||
get {
|
||||
if (genericNotifyIcon != null)
|
||||
return genericNotifyIcon.BalloonTipTitle;
|
||||
else
|
||||
return windowsNotifyIcon.BalloonTipTitle;
|
||||
}
|
||||
set {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.BalloonTipTitle = value;
|
||||
else
|
||||
windowsNotifyIcon.BalloonTipTitle = value;
|
||||
}
|
||||
}
|
||||
|
||||
public ContextMenu ContextMenu {
|
||||
get {
|
||||
if (genericNotifyIcon != null)
|
||||
return genericNotifyIcon.ContextMenu;
|
||||
else
|
||||
return windowsNotifyIcon.ContextMenu;
|
||||
}
|
||||
set {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.ContextMenu = value;
|
||||
else
|
||||
windowsNotifyIcon.ContextMenu = value;
|
||||
}
|
||||
}
|
||||
|
||||
public ContextMenuStrip ContextMenuStrip {
|
||||
get {
|
||||
if (genericNotifyIcon != null)
|
||||
return genericNotifyIcon.ContextMenuStrip;
|
||||
else
|
||||
return windowsNotifyIcon.ContextMenuStrip;
|
||||
}
|
||||
set {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.ContextMenuStrip = value;
|
||||
else
|
||||
windowsNotifyIcon.ContextMenuStrip = value;
|
||||
}
|
||||
}
|
||||
|
||||
public object Tag { get; set; }
|
||||
|
||||
public Icon Icon {
|
||||
get {
|
||||
if (genericNotifyIcon != null)
|
||||
return genericNotifyIcon.Icon;
|
||||
else
|
||||
return windowsNotifyIcon.Icon;
|
||||
}
|
||||
set {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.Icon = value;
|
||||
else
|
||||
windowsNotifyIcon.Icon = value;
|
||||
}
|
||||
}
|
||||
|
||||
public string Text {
|
||||
get {
|
||||
if (genericNotifyIcon != null)
|
||||
return genericNotifyIcon.Text;
|
||||
else
|
||||
return windowsNotifyIcon.Text;
|
||||
}
|
||||
set {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.Text = value;
|
||||
else
|
||||
windowsNotifyIcon.Text = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Visible {
|
||||
get {
|
||||
if (genericNotifyIcon != null)
|
||||
return genericNotifyIcon.Visible;
|
||||
else
|
||||
return windowsNotifyIcon.Visible;
|
||||
}
|
||||
set {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.Visible = value;
|
||||
else
|
||||
windowsNotifyIcon.Visible = value;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.Dispose();
|
||||
else
|
||||
windowsNotifyIcon.Dispose();
|
||||
}
|
||||
|
||||
public void ShowBalloonTip(int timeout) {
|
||||
ShowBalloonTip(timeout, BalloonTipTitle, BalloonTipText, BalloonTipIcon);
|
||||
}
|
||||
|
||||
public void ShowBalloonTip(int timeout, string tipTitle, string tipText,
|
||||
ToolTipIcon tipIcon) {
|
||||
if (genericNotifyIcon != null)
|
||||
genericNotifyIcon.ShowBalloonTip(timeout, tipTitle, tipText, tipIcon);
|
||||
else
|
||||
windowsNotifyIcon.ShowBalloonTip(timeout, tipTitle, tipText, tipIcon);
|
||||
}
|
||||
|
||||
private class NotifyIconWindowsImplementation : Component {
|
||||
|
||||
private static int nextId = 0;
|
||||
|
||||
@@ -92,7 +398,7 @@ namespace OpenHardwareMonitor.GUI {
|
||||
}
|
||||
}
|
||||
|
||||
public NotifyIconAdv() {
|
||||
public NotifyIconWindowsImplementation() {
|
||||
BalloonTipText = "";
|
||||
BalloonTipTitle = "";
|
||||
|
||||
@@ -101,7 +407,7 @@ namespace OpenHardwareMonitor.GUI {
|
||||
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public,
|
||||
null, new Type[] { typeof(int) }, null);
|
||||
|
||||
id = ++NotifyIconAdv.nextId;
|
||||
id = ++NotifyIconWindowsImplementation.nextId;
|
||||
window = new NotifyIconNativeWindow(this);
|
||||
UpdateNotifyIcon(visible);
|
||||
}
|
||||
@@ -132,8 +438,7 @@ namespace OpenHardwareMonitor.GUI {
|
||||
}
|
||||
|
||||
public void ShowBalloonTip(int timeout, string tipTitle, string tipText,
|
||||
ToolTipIcon tipIcon)
|
||||
{
|
||||
ToolTipIcon tipIcon) {
|
||||
if (timeout < 0)
|
||||
throw new ArgumentOutOfRangeException("timeout");
|
||||
|
||||
@@ -252,8 +557,7 @@ namespace OpenHardwareMonitor.GUI {
|
||||
}
|
||||
|
||||
private void ProcessMouseDown(ref Message message, MouseButtons button,
|
||||
bool doubleClick)
|
||||
{
|
||||
bool doubleClick) {
|
||||
if (doubleClick) {
|
||||
if (DoubleClick != null)
|
||||
DoubleClick(this, new MouseEventArgs(button, 2, 0, 0, 0));
|
||||
@@ -364,7 +668,7 @@ namespace OpenHardwareMonitor.GUI {
|
||||
}
|
||||
}
|
||||
|
||||
if (message.Msg == NotifyIconAdv.WM_TASKBARCREATED) {
|
||||
if (message.Msg == NotifyIconWindowsImplementation.WM_TASKBARCREATED) {
|
||||
lock (syncObj) {
|
||||
created = false;
|
||||
}
|
||||
@@ -375,10 +679,10 @@ namespace OpenHardwareMonitor.GUI {
|
||||
}
|
||||
|
||||
private class NotifyIconNativeWindow : NativeWindow {
|
||||
private NotifyIconAdv reference;
|
||||
private NotifyIconWindowsImplementation reference;
|
||||
private GCHandle referenceHandle;
|
||||
|
||||
internal NotifyIconNativeWindow(NotifyIconAdv component) {
|
||||
internal NotifyIconNativeWindow(NotifyIconWindowsImplementation component) {
|
||||
this.reference = component;
|
||||
}
|
||||
|
||||
@@ -499,4 +803,5 @@ namespace OpenHardwareMonitor.GUI {
|
||||
public static extern bool SetForegroundWindow(HandleRef hWnd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -73,9 +73,7 @@
|
||||
<Compile Include="GUI\GadgetWindow.cs" />
|
||||
<Compile Include="GUI\Gadget.cs" />
|
||||
<Compile Include="GUI\HardwareTypeImage.cs" />
|
||||
<Compile Include="GUI\NotifyIconAdv.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="GUI\NotifyIconAdv.cs" />
|
||||
<Compile Include="GUI\PlotPanel.cs">
|
||||
<SubType>UserControl</SubType>
|
||||
</Compile>
|
||||
|
Reference in New Issue
Block a user