当前位置: 首页>>代码示例>>C#>>正文


C# ToolStripDropDown.Focus方法代码示例

本文整理汇总了C#中System.Windows.Forms.ToolStripDropDown.Focus方法的典型用法代码示例。如果您正苦于以下问题:C# ToolStripDropDown.Focus方法的具体用法?C# ToolStripDropDown.Focus怎么用?C# ToolStripDropDown.Focus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Windows.Forms.ToolStripDropDown的用法示例。


在下文中一共展示了ToolStripDropDown.Focus方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ToolStripDropDownAttacher

	///<summary></summary>
	///<param name="keepMenuOpen">An open to keep the drop down menu open if the main window is no longer the active window.</param>
	public ToolStripDropDownAttacher(ToolStripDropDown menu, Control c, bool keepMenuOpen = true) {
		Menu = menu;
		Control = c;
		KeepMenuOpen = keepMenuOpen;
		menu.AutoClose = false;

		menu.Click += delegate {
			isFocusing = true;
			menu.Focus();
			IntPtr h = GetTopWindow(c.Handle);
			// after the main window is minimized and restored, clicking on
			// clock will deactivate the main window title bar unless this is called:
			SendMessage(h, WM_NCACTIVATE, (IntPtr) 1, (IntPtr) 0);
			isFocusing = false;
		};

		menu.Closing += (o, e) => {
			if (isClosing)
				e.Cancel = false;
		};

		menu.KeyDown += (o, e) => {
			if (e.KeyCode == Keys.Escape && !menu.AutoClose) {
				CloseMenu(ToolStripDropDownCloseReason.Keyboard);
				e.Handled = true;
				//e.SuppressKeyPress = true;
			}
		};

		menu.GotFocus += delegate {
			// click on the control, then tab to focus on the drop down
			// then minimize the main window, then restore the main window
			// tabbing into the drop down deactivates the main window
			var tl = GetTopWindow(c.Handle);
			SendMessage(tl, WM_NCACTIVATE, (IntPtr) 1, (IntPtr) 0);
		};

		menu.LostFocus += delegate {
			// if another window is clicked, then two issues occur:
			// 1) The top level window doesn't deactivate, so two windows on the
			// desktop both appear active.
			// 2) When the focus returns to this program's main window, clicking
			// on thedrop down shows this window's caption as deactivated.

			if (!c.Focused && !isFocusing && !isClosing) {
				if (KeepMenuOpen) {
					IntPtr fg = GetForegroundWindow();
					IntPtr tl = GetTopWindow(c.Handle);
					bool b = fg == tl;
					if (!b) {
						SendMessage(tl, WM_NCACTIVATE, (IntPtr) 0, (IntPtr) 0);
						return;
					}
				}

				CloseMenu(ToolStripDropDownCloseReason.CloseCalled);
			}
		};

		c.MouseDown += delegate {
			ShowMenu();
		};

		c.GotFocus += delegate {
			if (!menu.Visible && !isClosing)
				ShowMenu();
		};

		c.LostFocus += delegate {
			// if the focus was transferred to another window, then keep
			// the drop down open. Otherwise if the focus was transfered to
			// another control in the same window then close the menu
			bool b = true;
			if (KeepMenuOpen) {
				IntPtr fg = GetForegroundWindow();
				IntPtr tl = GetTopWindow(c.Handle);
				b = tl == fg;
			}

			if (!menu.Focused && b) {
				CloseMenu(ToolStripDropDownCloseReason.CloseCalled);
			}
		};

		c.KeyDown += (o, e) => {
			if (e.KeyData == Keys.Escape) {
				e.Handled = true;
				e.SuppressKeyPress = true; // stops the beep
				if (menu.Visible) {
					CloseMenu(ToolStripDropDownCloseReason.Keyboard);
				}
			}
		};

		c.EnabledChanged += delegate {
			menu.Enabled = c.Enabled;
		};

//.........这里部分代码省略.........
开发者ID:SaitDev,项目名称:MyAIAsisstent,代码行数:101,代码来源:ToolStripDropDownAttacher.cs


注:本文中的System.Windows.Forms.ToolStripDropDown.Focus方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。