/// <summary>
        /// 添加菜单项并返回其ID
        /// </summary>
        /// <param name="hMenu">要操作的菜单句柄</param>
        /// <param name="text">菜单项文本。当文本为"-"时,会视为分隔条;当文本指定为<see cref="string.Empty"/>时,为避免可能引起的其它项的快捷键不可用的问题,会将文本更改为一个空格</param>
        /// <param name="onClick">点击事件处理程序</param>
        /// <param name="subMenu">子菜单。赋值此参数将使<paramref name="onClick"/>参数失效,即不会触发点击事件</param>
        /// <param name="beforeItem">要插在哪一项的前边。该参数可以是那个项的位置索引,也可以是其ID,由<paramref name="byPosition"/>参数解释。若找不到项,则在末尾添加</param>
        /// <param name="byPosition">是否将<paramref name="beforeItem"/>参数解释为位置,为否则表示是ID</param>
        /// <param name="isChecked">是否勾选。注意项的勾选状态并不会因点击自动切换,需自行响应<paramref name="onClick"/>事件并用<see cref="SetItemChecked(IntPtr, int, bool, bool)"/>处理</param>
        /// <param name="enabled">是否可用</param>
        /// <exception cref="Win32Exception" />
        public static int AddItem(IntPtr hMenu, string text, EventHandler <SystemMenuItemEventArgs> onClick = null, ContextMenu subMenu = null, int beforeItem = -1, bool byPosition = true, bool isChecked = false, bool enabled = true)
        {
            //先检测菜单句柄有效性,避免无谓的ID分配
            if (!IsMenu(hMenu))
            {
                throw new Win32Exception(ERROR_INVALID_MENU_HANDLE);
            }

            var info = new MENUITEMINFO
            {
                fMask = 0x2     //MIIM_ID
                        | 0x1   //MIIM_STATE
                        | 0x4   //MIIM_SUBMENU
                        | 0x40  //MIIM_STRING
                        | 0x100 //MIIM_FTYPE
            };

            var mi = new SystemMenuItem(hMenu, onClick);

            info.wID = mi.Id;

            if (text == "-")
            {
                info.fType |= 0x800; //MFT_SEPARATOR
            }
            else
            {
                // Windows issue: Items with empty captions sometimes block keyboard
                // access to other items in same menu.
                info.dwTypeData = string.IsNullOrEmpty(text) ? " " : text;
            }

            if (isChecked)
            {
                info.fState |= 0x8;
            }

            if (!enabled)
            {
                info.fState |= 0x3;
            }

            if (subMenu != null)
            {
                info.hSubMenu = subMenu.Handle;
            }

            //添加成功再将项放进容器
            if (InsertMenuItem(hMenu, beforeItem, byPosition, info))
            {
                CustomizedItems[mi.Id] = mi;
            }
            else
            {
                mi.Dispose();
                throw new Win32Exception();
            }

            return(info.wID);
        }
Exemple #2
0
        public static EnableMenuItemOptions EnableMenuItem(IntPtr hMenu, SystemMenuItem uIDEnableItem, EnableMenuItemOptions uEnable)
        {
            // Returns the previous state of the menu item, or -1 if the menu item does not exist.
            int iRet = _EnableMenuItem(hMenu, uIDEnableItem, uEnable);

            return((EnableMenuItemOptions)iRet);
        }
Exemple #3
0
        public SystemMenuItem CreateMenuItem(string text = null, EventHandler clickHandler = null)
        {
            var menuItem = new SystemMenuItem(_menuItemIdCounter++) { Text = text };

            if (clickHandler != null)
            {
                menuItem.Clicked += clickHandler;
            }

            return menuItem;
        }
        /// <summary>
        /// Creates a new instance of SystemMenuItem and appends it to menu
        /// </summary>
        /// <param name="text">Text to be displayed as menu item caption</param>
        /// <param name="clicked">Menu item Clicked event handler</param>
        /// <returns>Just created menu item</returns>
        public SystemMenuItem AppendNewItem(string text, EventHandler clicked = null)
        {
            var item = new SystemMenuItem(text, this);
            if (clicked != null)
            {
                item.Click += clicked;
            }

            AppendMenu(_systemMenuHandle, item.Flags, item.MenuId, text);
            _items.Add(item);
            return item;
        }
Exemple #5
0
        /// <summary>システムメニュー項目の有効/無効を設定します。</summary>
        /// <param name="hWnd">対象のウィンドウハンドルを表すIntPtr。</param>
        /// <param name="menuItem">システムメニュー項目を表すSystemMenuItem列挙型の内の1つ</param>
        /// <param name="isEnabled">設定する有効状態を表すbool。</param>
        public static void ChangeSystemMenuItemEnabled(IntPtr hWnd, SystemMenuItem menuItem, bool isEnabled)
        {
            var hMenu = WindowApis.GetSystemMenu(hWnd, false);

            if (hMenu == IntPtr.Zero)
            {
                return;
            }

            var enabled = isEnabled ? ApiConstants.MF_BYCOMMAND | ApiConstants.MF_ENABLED :
                          ApiConstants.MF_BYCOMMAND | ApiConstants.MF_GRAYED;

            WindowApis.EnableMenuItem(hMenu, (uint)menuItem, enabled);
        }
Exemple #6
0
 private static extern int _EnableMenuItem(IntPtr hMenu, SystemMenuItem uIDEnableItem, EnableMenuItemOptions uEnable);
Exemple #7
0
        public void UpdateMenu(SystemMenuItem menuItem)
        {
            var mii = new MENUITEMINFO
                      {
                          fMask = MIIM_CHECKMARKS | MIIM_DATA | MIIM_FTYPE | MIIM_ID | MIIM_STATE | MIIM_STRING
                      };
            mii.cbSize = (uint) Marshal.SizeOf(mii);

            PInvokeUtils.Try(() => GetMenuItemInfo(_hSysMenu, menuItem.Id, false, ref mii));

            if (menuItem.Enabled)
                mii.fState &= (~MFS_DISABLED); // clear "disabled" flag
            else
                mii.fState |= MFS_DISABLED;    // set "disabled" flag

            if (menuItem.Checked)
                mii.fState |= MFS_CHECKED;    // set "checked" flag
            else
                mii.fState &= (~MFS_CHECKED); // clear "checked" flag

            mii.fMask = MIIM_STATE;

            PInvokeUtils.Try(() => SetMenuItemInfo(_hSysMenu, menuItem.Id, false, ref mii));

            // TODO: From my observations, this function always returns false, even though it appears to succeed.
            //       Am I using it incorrectly?
            DrawMenuBar(_hSysMenu);
        }
Exemple #8
0
 public void InsertMenu(uint position, SystemMenuItem menuItem)
 {
     PInvokeUtils.Try(() => InsertMenu(_hSysMenu, position, MF_BYPOSITION | MF_STRING, menuItem.Id, menuItem.Text));
     _items.Add(menuItem);
 }
Exemple #9
0
 public void AppendMenu(SystemMenuItem menuItem)
 {
     PInvokeUtils.Try(() => AppendMenu(_hSysMenu, MF_STRING, menuItem.Id, menuItem.Text));
     _items.Add(menuItem);
 }
Exemple #10
0
        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                setTitle(null, null);

                this._systemMenu = new SystemMenu(this);
                this._systemMenu.AppendSeparator();

                this._showHideMainMenuLineItem = this._systemMenu.AppendNewItem("", (s, args) =>
                {
                    this._model.Settings.ShowMainMenu = !this._model.Settings.ShowMainMenu;

                    UpdateMainMenuVisibility(this._model.Settings, null);
                });

                UpdateShowHideMainMenuLineItem();

                if (this.isDisableMainMenu)
                {
                    this._model.Settings.ShowMainMenu = false;
                    UpdateMainMenuVisibility(this._model.Settings, true);
                }

                RestoreSizeAndLocation(this._model.Settings);

                this._model.SettingsChanged += (o, args) => ApplySettings();

                if (strServerName != null && strTemplate != null && strDatabaseType != null)
                {
                    // log.DebugFormat(
                    //    "strServerName:'{0}';strTemplate:'{1}';strDatabaseType:'{2}'",
                    //    strServerName ?? "<Null>",
                    //    strTemplate ?? "<Null>",
                    //    strDatabaseType ?? "<Null>"
                    // );

                    InstanceInfo instanceInfo = new InstanceInfo
                    {
                        Authentication = new AuthenticationInfo
                        {
                            IsWindows = true,
                            Password  = string.Empty,
                            Username  = string.Empty
                        },
                        Instance  = strServerName,
                        IsEnabled = true,
                        Name      = strServerName,
                        IsODBC    = false,
                        DbType    = strDatabaseType
                    };

                    ConnectionGroupInfo connectionGroupInfo = new ConnectionGroupInfo();
                    connectionGroupInfo.Connections.Add(instanceInfo);

                    string strTemplateFileName       = Path.GetFileName(strTemplate);
                    string strFullPathToTemplateFile = Path.GetFullPath(strTemplate);
                    string strTemplateDir            = Path.GetDirectoryName(strTemplate);

                    if (string.IsNullOrEmpty(strTemplateDir))
                    {
                        strTemplateDir                 = Program.Model.Settings.TemplateDirectory;
                        strFullPathToTemplateFile      = Path.GetFullPath(strTemplateDir + "\\" + strTemplate);
                        connectionGroupInfo.IsExternal = false;
                    }
                    else
                    {
                        connectionGroupInfo.IsExternal = true;
                    }

                    connectionGroupInfo.IsDirectConnection = true;

                    connectionGroupInfo.TemplateDir      = strTemplateDir;
                    connectionGroupInfo.TemplateFileName = strTemplateFileName;
                    connectionGroupInfo.Name             = strServerName;

                    var targetTab = GetTargetTab(connectionGroupInfo.Name + "{" + connectionGroupInfo.TemplateFileName + "}");

                    Template selectedTemplate = TemplateNodesLoader.GetTemplateByFile(strFullPathToTemplateFile);
                    targetTab.SelectedTemplate = selectedTemplate;

                    targetTab.OpenConnection(connectionGroupInfo);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
Exemple #11
0
		private void frmMain_Load(object sender, EventArgs e)
		{
			try
			{
				setTitle(null, null);

				this._systemMenu = new SystemMenu(this);
				this._systemMenu.AppendSeparator();

				this._showHideMainMenuLineItem = this._systemMenu.AppendNewItem("", (s, args) =>
				{
					this._model.Settings.ShowMainMenu = !this._model.Settings.ShowMainMenu;

					UpdateMainMenuVisibility(this._model.Settings, null);
				});

				UpdateShowHideMainMenuLineItem();

				if (this.isDisableMainMenu)
				{
					this._model.Settings.ShowMainMenu = false;
					UpdateMainMenuVisibility(this._model.Settings, true);
				}

				RestoreSizeAndLocation(this._model.Settings);

				this._model.SettingsChanged += (o, args) => ApplySettings();

				if (strServerName != null && strTemplate != null && strDatabaseType != null)
				{
					// log.DebugFormat(
					//    "strServerName:'{0}';strTemplate:'{1}';strDatabaseType:'{2}'",
					//    strServerName ?? "<Null>",
					//    strTemplate ?? "<Null>",
					//    strDatabaseType ?? "<Null>"
					// );

					InstanceInfo instanceInfo = new InstanceInfo
					{
						Authentication = new AuthenticationInfo
						{
							IsWindows = true,
							Password = string.Empty,
							Username = string.Empty
						},
						Instance  = strServerName,
						IsEnabled = true,
						Name      = strServerName,
						IsODBC    = false,
						DbType    = strDatabaseType
					};

					ConnectionGroupInfo connectionGroupInfo = new ConnectionGroupInfo();
					connectionGroupInfo.Connections.Add(instanceInfo);

					string strTemplateFileName       = Path.GetFileName(strTemplate);
					string strFullPathToTemplateFile = Path.GetFullPath(strTemplate);
					string strTemplateDir            = Path.GetDirectoryName(strTemplate);

					if (string.IsNullOrEmpty(strTemplateDir))
					{
						strTemplateDir                 = Program.Model.Settings.TemplateDirectory;
						strFullPathToTemplateFile      = Path.GetFullPath(strTemplateDir + "\\" + strTemplate);
						connectionGroupInfo.IsExternal = false;
					}
					else
					{
						connectionGroupInfo.IsExternal = true;
					}

					connectionGroupInfo.IsDirectConnection = true;

					connectionGroupInfo.TemplateDir      = strTemplateDir;
					connectionGroupInfo.TemplateFileName = strTemplateFileName;
					connectionGroupInfo.Name             = strServerName;

					var targetTab = GetTargetTab(connectionGroupInfo.Name + "{" + connectionGroupInfo.TemplateFileName + "}");

					Template selectedTemplate = TemplateNodesLoader.GetTemplateByFile(strFullPathToTemplateFile);
					targetTab.SelectedTemplate = selectedTemplate;

					targetTab.OpenConnection(connectionGroupInfo);
				}
			}
			catch (Exception ex)
			{
				log.Error(ex);
			}
		}