Laden...

[Snipped] Add-In Express ADXOlExplorerCommandBar / CommandBarEvents

Erstellt von Andreas.May vor 15 Jahren Letzter Beitrag vor 15 Jahren 3.332 Views
Andreas.May Themenstarter:in
915 Beiträge seit 2006
vor 15 Jahren
[Snipped] Add-In Express ADXOlExplorerCommandBar / CommandBarEvents

Hallo Community,

hier ein kleines Codesnipped für Add-In Express ADXOlExplorerCommandBar.
Folgendes Problem soll damit behoben werden, ADXCommandBarControl's bieten zum Teil nur Events an wie z.B. beim ADXCommandBarButton nur der Klick oder beim ADXCommandBarPopup sogar gar keine Events an.

Um dieses Problem zu beheben, habe ich eine kleine Komponente geschrieben.
Evtl. kann diese noch jemand gebrauchen. Die Komponente kann auch beliebig dann noch durch hinzufügen bzw. abfangen der Windowsnachrichten erweitert werden.

Hiwneis:

  • Es wird AddinExpress dafür benötigt! Die Events können über den Designer herkömmlich über das Event Symbol via doppelklick automatisch hinzugefügt werden, dazu einfach die Komponente auf den Designer ziehen.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using AddinExpress.MSO;
using Office;
using System.Windows.Forms;
using IApplication = Outlook.Application;
using IExplorer = Outlook.Explorer;

namespace CobraOlAddIn12
{
	[ComVisible(false)]
	[DesignTimeVisible(true)]
	[ClassInterface(ClassInterfaceType.AutoDispatch)]
	[ToolboxBitmap(typeof(Button))]
	[ToolboxItem(true)]
	public class ADXOlExplorerCommandBarEvents : ADXBaseAppEvents, ISupportInitialize
	{
		#region Declarations
		/// <summary>
		/// 
		/// </summary>
		private class CommandBarNative : NativeWindow
		{
			#region Imports
			[DllImport("User32.dll")]
			private static extern IntPtr FindWindow(string strClassName, string strWindowName);

			[DllImport("user32.dll", SetLastError = true)]
			private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

			[DllImport("user32.dll")]
			private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);

			private enum MouseMessages
			{
				WM_LBUTTONDOWN = 0x0201,
				WM_LBUTTONUP = 0x202,
				WM_MBUTTONDOWN = 0x207,
				WM_MBUTTONUP = 0x208,
				WM_RBUTTONDOWN = 0x204,
				WM_RBUTTONUP = 0x205,
				WM_MOUSEMOVE = 0x200,
				WM_MOUSEWHEEL = 0x20A,
				WM_MOUSEHWHEEL = 0x20E,
			}

			private static readonly IntPtr TRUE = new IntPtr(1);
			private static readonly IntPtr FALSE = IntPtr.Zero;

			[StructLayoutAttribute(LayoutKind.Sequential)]
			private struct WINDOWINFO
			{
				/// DWORD->unsigned int
				public uint cbSize;

				/// RECT->tagRECT
				public Rectangle rcWindow;

				/// RECT->tagRECT
				public Rectangle rcClient;

				/// DWORD->unsigned int
				public uint dwStyle;

				/// DWORD->unsigned int
				public uint dwExStyle;

				/// DWORD->unsigned int
				public uint dwWindowStatus;

				/// UINT->unsigned int
				public uint cxWindowBorders;

				/// UINT->unsigned int
				public uint cyWindowBorders;

				/// ATOM->WORD->unsigned short
				public ushort atomWindowType;

				/// WORD->unsigned short
				public ushort wCreatorVersion;
			}
			#endregion

			#region Declarations
			public event EventHandler<CommandBarNativeMouseEventArgs> MouseUpEvent;
			public event EventHandler<CommandBarNativeMouseEventArgs> MouseDownEvent;
			public event EventHandler<CommandBarNativeMouseEventArgs> MouseMoveEvent;
			#endregion

			#region Properties
			/// <summary>
			/// 
			/// </summary>
			public ADXOlExplorerCommandBar CommandBar { get; private set; }
			/// <summary>
			/// 
			/// </summary>
			public IntPtr ObjHandle
			{
				get
				{
					if (this.CommandBar == null) return IntPtr.Zero;

					CommandBar cmdBar = this.CommandBar.CommandBarObj as CommandBar;

					if (cmdBar == null) return IntPtr.Zero;

					IApplication app = cmdBar.Application as IApplication;

					if (app == null) return IntPtr.Zero;

					IExplorer ex = app.Application.ActiveExplorer() as IExplorer;
					IntPtr hWnd = FindWindow("rctrl_renwnd32", ex.Caption);
					IntPtr hDocktop = FindWindowEx(hWnd, IntPtr.Zero, "MsoCommandBarDock", "MsoDockTop");
					IntPtr hCmdBar = FindWindowEx(hDocktop, IntPtr.Zero, "MsoCommandBar", cmdBar.Name);
					return hCmdBar;
				}
			}
			#endregion

			#region Constructors
			/// <summary>
			/// 
			/// </summary>
			/// <param name="cmdBar"></param>
			public CommandBarNative(ADXOlExplorerCommandBar cmdBar)
			{
				this.CommandBar = cmdBar;
			}
			#endregion

			#region Methods
			/// <summary>
			/// 
			/// </summary>
			/// <param name="m"></param>
			protected override void WndProc(ref Message m)
			{
				switch (m.Msg)
				{
					case (int)MouseMessages.WM_MBUTTONDOWN:
					case (int)MouseMessages.WM_RBUTTONDOWN:
					case (int)MouseMessages.WM_LBUTTONDOWN:
						{
							this.WmMBDown(ref m);
							if (m.Result == TRUE) return;
							break;
						}
					case (int)MouseMessages.WM_MBUTTONUP:
					case (int)MouseMessages.WM_RBUTTONUP:
					case (int)MouseMessages.WM_LBUTTONUP:
						{
							this.WmMBUp(ref m);
							if (m.Result == TRUE) return;
							break;
						}
					case (int)MouseMessages.WM_MOUSEMOVE:
						{
							this.WmMBMove(ref m);
							if (m.Result == TRUE) return;
							break;
						}
				}
				base.WndProc(ref m);
			}
			/// <summary>
			/// 
			/// </summary>
			/// <param name="m"></param>
			private void WmMBMove(ref Message m)
			{
				CommandBarNativeMouseEventArgs args = new CommandBarNativeMouseEventArgs();
				args.MouseEvent = this.CreateMouseEventArgs(m.LParam, m.Msg);

				if (this.MouseMoveEvent != null)
					this.MouseMoveEvent.Invoke(this, args);
			}
			/// <summary>
			/// 
			/// </summary>
			/// <param name="msg"></param>
			/// <returns></returns>
			private MouseButtons GetMouseButton(int msg)
			{
				switch (msg)
				{
					case (int)MouseMessages.WM_LBUTTONUP:
					case (int)MouseMessages.WM_LBUTTONDOWN:
						return MouseButtons.Left;

					case (int)MouseMessages.WM_MBUTTONUP:
					case (int)MouseMessages.WM_MBUTTONDOWN:
						return MouseButtons.Middle;

					case (int)MouseMessages.WM_RBUTTONUP:
					case (int)MouseMessages.WM_RBUTTONDOWN:
						return MouseButtons.Right;
				}
				return MouseButtons.None;
			}
			/// <summary>
			/// 
			/// </summary>
			/// <param name="m"></param>
			private void WmMBDown(ref Message m)
			{
				CommandBarNativeMouseEventArgs args = new CommandBarNativeMouseEventArgs();
				args.MouseEvent = this.CreateMouseEventArgs(m.LParam, m.Msg);

				if (this.MouseDownEvent != null)
					this.MouseDownEvent.Invoke(this, args);

				m.Result = (args.Cancel) ? TRUE : FALSE;
			}
			/// <summary>
			/// 
			/// </summary>
			/// <param name="m"></param>
			private void WmMBUp(ref Message m)
			{
				CommandBarNativeMouseEventArgs args = new CommandBarNativeMouseEventArgs();
				args.MouseEvent = this.CreateMouseEventArgs(m.LParam, m.Msg);

				if (this.MouseUpEvent != null)
					this.MouseUpEvent.Invoke(this, args);

				m.Result = (args.Cancel) ? TRUE : FALSE;
			}
			/// <summary>
			/// 
			/// </summary>
			/// <param name="lParam"></param>
			/// <param name="m"></param>
			/// <returns></returns>
			private MouseEventArgs CreateMouseEventArgs(IntPtr lParam, int m)
			{
				Point pt = new Point(lParam.ToInt32());
				Point pt2 = this.PointToCmd(pt);

				return new MouseEventArgs(this.GetMouseButton(m), 0,
					pt2.X, pt2.Y, 0);
			}
			/// <summary>
			/// 
			/// </summary>
			/// <param name="screenPoint"></param>
			/// <returns></returns>
			private Point PointToCmd(Point screenPoint)
			{
				WINDOWINFO windowInfo = new WINDOWINFO();
				GetWindowInfo(base.Handle, ref windowInfo);
				return new Point(windowInfo.rcClient.Location.X + screenPoint.X, windowInfo.rcClient.Location.Y + screenPoint.Y);
			}
			#endregion
		}
		/// <summary>
		/// 
		/// </summary>
		private class CommandBarNativeMouseEventArgs : CancelEventArgs
		{
			public MouseEventArgs MouseEvent { get; set; }
		}
		#endregion

		#region Properties
		/// <summary>
		/// 
		/// </summary>
		private Dictionary<ADXOlExplorerCommandBar, CommandBarNative> CommandBars { get; set; }
		/// <summary>
		/// 
		/// </summary>
		public override string ClassName
		{
			get
			{
				this.Bind();
				return this.GetType().ToString();
			}
		}
		#endregion

		#region Events
		[Category("Explorer")]
		[Description("Occurs before command bars events from the active explorer.")]
		public event EventHandler<CommandBarControlMouseEventArgs> MouseDown;
		[Category("Explorer")]
		[Description("Occurs before command bars events from the active explorer.")]
		public event EventHandler<CommandBarControlMouseEventArgs> MouseUp;
		[Category("Explorer")]
		[Description("Occurs before command bars events from the active explorer.")]
		public event EventHandler<CommandBarControlMouseEventArgs> MouseMove;
		#endregion

		#region Constructors
		/// <summary>
		/// 
		/// </summary>
		public ADXOlExplorerCommandBarEvents()
			: base()
		{
			this.CommandBars = new Dictionary<ADXOlExplorerCommandBar, CommandBarNative>();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="container"></param>
		public ADXOlExplorerCommandBarEvents(IContainer container)
			: base(container)
		{
			this.CommandBars = new Dictionary<ADXOlExplorerCommandBar, CommandBarNative>();
			container.Add(this);
		}
		#endregion

		#region Methods
		/// <summary>
		/// 
		/// </summary>
		public void Bind()
		{
			foreach (ADXOlExplorerCommandBar bar in CommandBars.Keys)
			{
				CommandBars[bar].ReleaseHandle();
				CommandBars[bar].AssignHandle(CommandBars[bar].ObjHandle);
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="disposing"></param>
		protected override void Dispose(bool disposing)
		{
			// release handles (stop wndproc/hook)
			foreach (ADXOlExplorerCommandBar bar in CommandBars.Keys)
				CommandBars[bar].ReleaseHandle();

			base.Dispose(disposing);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnMouseUpEvent(object sender, CommandBarNativeMouseEventArgs e)
		{
			if (sender is CommandBarNative)
			{
				CommandBarNative native = sender as CommandBarNative;

				foreach (ADXCommandBarControl cmdCtr in native.CommandBar.Controls)
				{
					CommandBarControl control = cmdCtr.ControlObj as CommandBarControl;

					if (control.accHitTest(e.MouseEvent.X, e.MouseEvent.Y) != null)
					{
						CommandBarControlMouseEventArgs ctrEvnt = new CommandBarControlMouseEventArgs(
									e.Cancel,
									e.MouseEvent.Button,
									e.MouseEvent.Location);

						if (this.MouseUp != null) this.MouseUp(cmdCtr, ctrEvnt);
						e.Cancel = ctrEvnt.Cancel;
					}
				}
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnMouseDownEvent(object sender, CommandBarNativeMouseEventArgs e)
		{
			if (sender is CommandBarNative)
			{
				CommandBarNative native = sender as CommandBarNative;

				foreach (ADXCommandBarControl cmdCtr in native.CommandBar.Controls)
				{
					CommandBarControl control = cmdCtr.ControlObj as CommandBarControl;

					if (control.accHitTest(e.MouseEvent.X, e.MouseEvent.Y) != null)
					{
						CommandBarControlMouseEventArgs ctrEvnt = new CommandBarControlMouseEventArgs(
									e.Cancel,
									e.MouseEvent.Button,
									e.MouseEvent.Location);

						if (this.MouseDown != null) this.MouseDown(cmdCtr, ctrEvnt);

						e.Cancel = ctrEvnt.Cancel;
					}
				}
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnMouseMoveEvent(object sender, ADXOlExplorerCommandBarEvents.CommandBarNativeMouseEventArgs e)
		{
			if (sender is CommandBarNative)
			{
				CommandBarNative native = sender as CommandBarNative;

				foreach (ADXCommandBarControl cmdCtr in native.CommandBar.Controls)
				{
					CommandBarControl control = cmdCtr.ControlObj as CommandBarControl;

					if (control.accHitTest(e.MouseEvent.X, e.MouseEvent.Y) != null)
					{
						CommandBarControlMouseEventArgs ctrEvnt = new CommandBarControlMouseEventArgs(
									e.Cancel,
									e.MouseEvent.Button,
									e.MouseEvent.Location);

						if (this.MouseMove != null) this.MouseMove(cmdCtr, ctrEvnt);
						e.Cancel = ctrEvnt.Cancel;
					}
				}
			}
		}
		/// <summary>
		/// 
		/// </summary>
		public void BeginInit() { }
		/// <summary>
		/// 
		/// </summary>
		public void EndInit()
		{
			foreach (IComponent component in base.Container.Components)
			{
				if (component is ADXOlExplorerCommandBar)
				{
					ADXOlExplorerCommandBar bar = component as ADXOlExplorerCommandBar;

					CommandBarNative native = new CommandBarNative(bar);
					native.MouseDownEvent += new EventHandler<CommandBarNativeMouseEventArgs>(this.OnMouseDownEvent);
					native.MouseUpEvent += new EventHandler<CommandBarNativeMouseEventArgs>(this.OnMouseUpEvent);
					native.MouseMoveEvent += new EventHandler<CommandBarNativeMouseEventArgs>(this.OnMouseMoveEvent);
					CommandBars.Add(bar, native);
				}
			}
		}
		#endregion
	}

	public class CommandBarControlMouseEventArgs : CancelEventArgs
	{
		#region Properties
		/// <summary>
		// Summary:
		///     Gets which mouse button was pressed.
		///
		/// Returns:
		///     One of the System.Windows.Forms.MouseButtons values.
		/// </summary>
		public MouseButtons Button { get; private set; }
		/// <summary>
		/// 		Summary:
		///     Gets the location of the mouse during the generating mouse event.
		///
		/// Returns:
		///     A System.Drawing.Point containing the x- and y- coordinate of the mouse,
		///     in pixels.
		/// </summary>
		public Point Location { get; private set; }
		/// <summary>
		// Summary:
		///     Gets the x-coordinate of the mouse during the generating mouse event.
		///
		/// Returns:
		///     The x-coordinate of the mouse, in pixels.
		/// </summary>
		public int X { get; private set; }
		/// <summary>
		/// Summary:
		///     Gets the y-coordinate of the mouse during the generating mouse event.
		///
		/// Returns:
		///     The y-coordinate of the mouse, in pixels.
		/// </summary>
		public int Y { get; private set; }
		#endregion

		#region Constructors
		/// <summary>
		/// 
		/// </summary>
		/// <param name="cancel"></param>
		public CommandBarControlMouseEventArgs(bool cancel, MouseButtons btn, Point location)
		{
			this.Cancel = cancel;
			this.Button = btn;
			this.Location = location;
			this.X = location.X;
			this.Y = location.Y;
		}
		#endregion
	}
}


Hinweis:
Das snipped kann noch verschönert werden, aber ist im derzeitigen Zustand Funktionsfähig. Man kann den Weg den ich gegangen bin auch verwenden und ein Control ADXCommandPopup (Custom) zu erstellen und z.B. auch Bilder zusätzlich vom Menüpunkt hinzuzufügen - was standardmäßig ja nicht geht. Hierzu einfach den Weg über WindowsNative gehen ähnlich wie bei diesem Snipped.

Anwendungsbeispiel:
Clickevent für z.B. ADXCommandBarPopup mit verhindern des aufklappen der

Menüleiste.
Erst nach Dialogabfrage == true wird dann ein aufklappen der Menüleiste möglich.

/Ps
Habe es nicht unter Snipped gestellt da es eher um Office Anwendungen geht. Falls es doch an der falschen Stelle sein sollte, entschuldige ich mich jetzt schon mal dafür.

Viel Spaß damit!

Wie vernichtet stand Andreas unter den flammenden Augen seiner Kunden.
Ihm war's, als stünde des Schicksals dunkle Wetterwolke über seinem Haupte X(