1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok pbartok@novell.com
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
34 namespace System.Windows.Forms {
35 [DefaultProperty("Text")]
36 [DefaultEvent("MouseDown")]
37 [Designer ("System.Windows.Forms.Design.NotifyIconDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
38 [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
39 public sealed class NotifyIcon : System.ComponentModel.Component {
40 #region Local Variables
41 private ContextMenu context_menu;
43 private Bitmap icon_bitmap;
46 private NotifyIconWindow window;
47 private bool systray_active;
48 private ToolTip tooltip;
49 #endregion // Local Variables
51 #region NotifyIconWindow Class
52 internal class NotifyIconWindow : Control {
56 public NotifyIconWindow(NotifyIcon owner) {
59 rect = new Rectangle(0, 0, 1, 1);
63 Paint += new PaintEventHandler(HandlePaint);
64 SizeChanged += new EventHandler(HandleSizeChanged);
66 // Events that need to be sent to our parent
67 Click += new EventHandler(HandleClick);
68 DoubleClick += new EventHandler(HandleDoubleClick);
69 MouseDown +=new MouseEventHandler(HandleMouseDown);
70 MouseUp +=new MouseEventHandler(HandleMouseUp);
71 MouseMove +=new MouseEventHandler(HandleMouseMove);
72 ContextMenu = owner.context_menu;
75 protected override CreateParams CreateParams {
79 cp = base.CreateParams;
81 cp.Parent = IntPtr.Zero;
82 cp.Style = (int)WindowStyles.WS_POPUP;
83 cp.Style |= (int)WindowStyles.WS_CLIPSIBLINGS;
85 cp.ExStyle = (int)(WindowStyles.WS_EX_TOOLWINDOW);
91 protected override void WndProc(ref Message m) {
93 case Msg.WM_NCPAINT: {
94 PaintEventArgs paint_event;
96 paint_event = XplatUI.PaintEventStart(Handle, false);
98 XplatUI.PaintEventEnd(Handle, false);
103 switch ((Msg)m.LParam.ToInt32()) {
104 case Msg.WM_LBUTTONDOWN: {
105 HandleMouseDown(this, new MouseEventArgs(MouseButtons.Left, 1, Control.MousePosition.X, Control.MousePosition.Y, 0));
109 case Msg.WM_LBUTTONUP: {
110 HandleMouseUp(this, new MouseEventArgs(MouseButtons.Left, 1, Control.MousePosition.X, Control.MousePosition.Y, 0));
111 HandleClick(this, EventArgs.Empty);
115 case Msg.WM_LBUTTONDBLCLK: {
116 HandleDoubleClick(this, EventArgs.Empty);
120 case Msg.WM_MOUSEMOVE: {
121 HandleMouseMove(this, new MouseEventArgs(MouseButtons.None, 1, Control.MousePosition.X, Control.MousePosition.Y, 0));
125 case Msg.WM_RBUTTONDOWN: {
126 HandleMouseDown(this, new MouseEventArgs(MouseButtons.Right, 1, Control.MousePosition.X, Control.MousePosition.Y, 0));
130 case Msg.WM_RBUTTONUP: {
131 HandleMouseUp(this, new MouseEventArgs(MouseButtons.Right, 1, Control.MousePosition.X, Control.MousePosition.Y, 0));
132 HandleClick(this, EventArgs.Empty);
136 case Msg.WM_RBUTTONDBLCLK: {
137 HandleDoubleClick(this, EventArgs.Empty);
144 base.WndProc (ref m);
147 internal void CalculateIconRect() {
148 if (owner != null && owner.icon != null) {
153 // Icons are always square. Try to center them in the window
154 if (ClientRectangle.Width < ClientRectangle.Height) {
155 size = ClientRectangle.Width;
157 size = ClientRectangle.Height;
159 x = this.ClientRectangle.Width / 2 - size / 2;
160 y = this.ClientRectangle.Height / 2 - size / 2;
161 rect = new Rectangle(x, y, size, size);
163 // Force our window to be square
168 if (Height != size) {
174 private void HandlePaint(object sender, PaintEventArgs e) {
175 if (owner.icon != null) {
176 e.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(SystemColors.Window), rect);
177 e.Graphics.DrawImage(owner.icon_bitmap, rect);
182 private void HandleSizeChanged(object sender, EventArgs e) {
186 private void HandleClick(object sender, EventArgs e) {
187 if (owner.Click != null) {
188 owner.Click(owner, e);
192 private void HandleDoubleClick(object sender, EventArgs e) {
193 if (owner.DoubleClick != null) {
194 owner.DoubleClick(owner, e);
198 private void HandleMouseDown(object sender, MouseEventArgs e) {
199 if (owner.MouseDown != null) {
200 owner.MouseDown(owner, e);
204 private void HandleMouseUp(object sender, MouseEventArgs e) {
205 if (owner.context_menu != null) {
206 owner.context_menu.Show(this, new Point(e.X, e.Y));
209 if (owner.MouseUp != null) {
210 owner.MouseUp(owner, e);
214 private void HandleMouseMove(object sender, MouseEventArgs e) {
215 if (owner.MouseMove != null) {
216 owner.MouseMove(owner, e);
220 #endregion // NotifyIconWindow Class
222 #region Public Constructors
223 public NotifyIcon() {
224 window = new NotifyIconWindow(this);
225 systray_active = false;
228 public NotifyIcon(System.ComponentModel.IContainer container) : this() {
230 #endregion // Public Constructors
232 #region Private Methods
233 private void ShowSystray(bool property_changed) {
234 if (property_changed) {
235 window.CalculateIconRect();
238 if (systray_active) {
239 if (property_changed) {
249 icon_bitmap = icon.ToBitmap();
251 systray_active = true;
252 XplatUI.SystrayAdd(window.Handle, text, icon, out tooltip);
255 private void HideSystray() {
256 if (!systray_active) {
260 systray_active = false;
261 XplatUI.SystrayRemove(window.Handle, ref tooltip);
264 private void UpdateSystray() {
265 if (icon_bitmap != null) {
266 icon_bitmap.Dispose();
270 icon_bitmap = icon.ToBitmap();
273 XplatUI.SystrayChange(window.Handle, text, icon, ref tooltip);
276 #endregion // Private Methods
278 #region Public Instance Properties
280 public ContextMenu ContextMenu {
286 if (context_menu != value) {
287 context_menu = value;
288 window.ContextMenu = value;
316 if (value.Length >= 64) {
317 throw new ArgumentException("ToolTip length must be less than 64 characters long", "Text");
320 if (text == string.Empty && icon == null) {
330 [DefaultValue(false)]
331 public bool Visible {
337 if (visible != value) {
340 // Let our control know, too
341 window.is_visible = value;
351 #endregion // Public Instance Properties
353 #region Protected Instance Methods
354 protected override void Dispose(bool disposing) {
355 if (icon_bitmap != null) {
356 icon_bitmap.Dispose();
362 base.Dispose (disposing);
365 #endregion // Protected Instance Methods
369 public event EventHandler Click;
372 public event EventHandler DoubleClick;
374 public event MouseEventHandler MouseDown;
375 public event MouseEventHandler MouseMove;
376 public event MouseEventHandler MouseUp;