当前位置: 澳门新濠3559 > 操作系统 > 正文

右键处理的代码在哪里,(澳门新濠3559与系统托

时间:2019-12-21 19:57来源:操作系统
别人的程序,有全部源代码。运行后任务栏右下方显示出了一个图标。鼠标左键点击,有下面的代码处理。_notifyIcon.MouseClick+=notifyIcon1_Click;privatevoidnotifyIcon1_Click(objectsender,MouseEventArgse

别人的程序,有全部源代码。运行后任务栏右下方显示出了一个图标。鼠标左键点击,有下面的代码处理。_notifyIcon.MouseClick+=notifyIcon1_Click;privatevoidnotifyIcon1_Click(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){...}}右键点击就怪了,有反应,弹出了菜单,但是处理不在notifyIcon1_Click中,没有找到e.Button==MouseButtons.Right;类似的代码,不知道是什么代码处理的。请指导一下,右键处理的代码在哪里?谢谢

最近都没怎么接触WPF了,闲来无事为以前做的WPF DEMO添加托盘管理。 

原文链接:

1、如果不想让程序在任务栏中显示,请把窗体的属性ShowInTaskbar设置为false;
2、如果想让程序启动时就最小化,请设置窗体的属性WindowState设置为Minimized。(Minimized 最小化,Normal正常启动,Maximized最大化)
3、拉一个NotifyIcon控件notifyIcon,为控件notifyIcon的属性Icon添加一个icon图标。
4、可以为NotifyIcon加一个ContextMenuStrip右键菜单menu_右键处理的代码在哪里,(澳门新濠3559与系统托盘同义)。Notify。
5、本例子禁用了窗体最大化按钮。(设置窗体的属性MaximizeBox的属性为false)
6、主要代码:
#region 私有方法 处理窗体的 显示 隐藏 关闭(退出)
private void ExitMainForm()
{
if (MessageBox.Show("您确定要退出化验数据接收程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
this.notifyIcon.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}
}

 

C# winForm启动最小化到任务栏右侧通知栏并交互操作

private void HideMainForm()
{
this.Hide();
}

其中,主要添加了NotificationAreaIcon类,代码如下:

一。主要功能:
(1)、程序启动自动隐藏到任务栏右侧通知栏显示。(与系统托盘同义)
(2)、双击系统托盘图标显示、隐藏窗口;
(3)、右击系统托盘图标提供三个菜单选项,“退出”、“隐藏”、“显示”;

private void ShowMainForm()
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
#endregion#region 右键菜单处理,显示 隐藏 退出
private void menuItem_Show_Click(object sender, EventArgs e)
{
ShowMainForm();
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using Drawing = System.Drawing;
using Forms = System.Windows.Forms;

二。相关控件:
1、建一个WinForm程序—IconForm,将Form属性ShowInTaskbar改为false,这样程序将不会在任务栏中显示。
2、将Form属性WindowState选择为 Minimized,以便起来自动最小化隐藏
3、在工具栏中的“公共控件”里,拖入NotifyIcon控件—notifyIcon1,这个是程序运行任务栏右侧通知区域图标显示控件,为控件notifyIcon的属性Icon添加一个icon图标,或从代码中加入。
4、在工具栏中的“菜单和工具栏”里,拖入ContextMenuStrip—contextMenuStrip1,这个控件是右击时关联菜单。
5、右键notifyIcon1选择属性,将其属性ContextMenuStrip改加为contextMenuStrip1,这个时候notifyIcon1和contextMenuStrip1两个控件就关联了。
6、右键contextMenuStrip1,选择属性,进入Items,然后点击“添加”,这里添加三个菜单选项:exitMenuItem、hideMenuItem、showMenuItem,同时分别将其Text属性改为:退出、隐藏和显示。

private void menuItem_Hide_Click(object sender, EventArgs e)
{
HideMainForm();
}

namespace WpfApplication1
{
    /// <summary>
    /// Represents a thin wrapper for <see cref="Forms.NotifyIcon"/>
    /// </summary>
    [ContentProperty("Text")]
    [DefaultEvent("MouseDoubleClick")]
    public class NotificationAreaIcon : FrameworkElement
    {
        Forms.NotifyIcon notifyIcon;

三。主要代码:
1、双击IconForm,即添加Load事件然后

private void menuItem_Exit_Click(object sender, EventArgs e)
{
ExitMainForm();
}
#endregion#region 双击托盘上图标时,显示窗体
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Minimized;

        public static readonly RoutedEvent MouseClickEvent = EventManager.RegisterRoutedEvent(
            "MouseClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(NotificationAreaIcon));

//一 右击窗体,选择属性,转到事件页面,双击 Load,SizeChanged事件,给窗体添加代码
        private void Form1_Load(object sender, EventArgs e)
        {
         //1.将Form属性ShowInTaskbar改为false,这样程序将不会在任务栏中显示。
         //2.将Form属性WindowState选择为 Minimized,以便起来自动最小化隐藏。
            string startup = Application.ExecutablePath;       //取得程序路径   
            int pp = startup.LastIndexOf("\");
            startup = startup.Substring(0, pp);
            string icon = startup + "\testIcon.ico";
         //3.一定为notifyIcon1其设置图标,否则无法显示在通知栏。或者在其属性中设置
            notifyIcon1.Icon = new Icon(icon);

HideMainForm();
}
else if(this.WindowState == FormWindowState.Minimized)
{
ShowMainForm();
}
}
#endregion

        public static readonly RoutedEvent MouseDoubleClickEvent = EventManager.RegisterRoutedEvent(
            "MouseDoubleClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(NotificationAreaIcon));

        }

#region 点最小化按钮时,最小化到托盘
private void frmMain_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
HideMainForm();
}
}
#endregion

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(ImageSource), typeof(NotificationAreaIcon));

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide(); //或者是this.Visible = false;
                this.notifyIcon1.Visible = true;
            }

#region 窗体关闭时最小化到托盘
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(NotificationAreaIcon));

        }

HideMainForm();
}
#endregion

        public static readonly DependencyProperty FormsContextMenuProperty =
            DependencyProperty.Register("MenuItems", typeof(List<Forms.MenuItem>), typeof(NotificationAreaIcon), new PropertyMetadata(new List<Forms.MenuItem>()));

 

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

//二 双击窗体上的菜单项,添加相关代码
        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要退出程序吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }

            // Create and initialize the window forms notify icon based
            notifyIcon = new Forms.NotifyIcon();
            notifyIcon.Text = Text;
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                notifyIcon.Icon = FromImageSource(Icon);
            }
            notifyIcon.Visible = FromVisibility(Visibility);

        }

            if (this.MenuItems != null && this.MenuItems.Count > 0)
            {
                notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(this.MenuItems.ToArray());
            }

        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();

            notifyIcon.MouseDown += OnMouseDown;
            notifyIcon.MouseUp += OnMouseUp;
            notifyIcon.MouseClick += OnMouseClick;
            notifyIcon.MouseDoubleClick += OnMouseDoubleClick;

        }

            Dispatcher.ShutdownStarted += OnDispatcherShutdownStarted;
        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();

        private void OnDispatcherShutdownStarted(object sender, EventArgs e)
        {
            notifyIcon.Dispose();
        }

        }

        private void OnMouseDown(object sender, Forms.MouseEventArgs e)
        {
            OnRaiseEvent(MouseDownEvent, new MouseButtonEventArgs(
                InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
        }

//三 转到窗体设计模式,右击notifyIcon1 ,选择属性,双击其中DoubleClick,添加相关代码
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }

        private void OnMouseUp(object sender, Forms.MouseEventArgs e)
        {
            OnRaiseEvent(MouseUpEvent, new MouseButtonEventArgs(
                InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
        }

        }

        private void OnMouseDoubleClick(object sender, Forms.MouseEventArgs e)
        {
            OnRaiseEvent(MouseDoubleClickEvent, new MouseButtonEventArgs(
                InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
        }

 

        private void OnMouseClick(object sender, Forms.MouseEventArgs e)
        {
            OnRaiseEvent(MouseClickEvent, new MouseButtonEventArgs(
                InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
        }

四。完整的代码如下:

        private void OnRaiseEvent(RoutedEvent handler, MouseButtonEventArgs e)
        {
            e.RoutedEvent = handler;
            RaiseEvent(e);
        }

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

        public List<Forms.MenuItem> MenuItems
        {
            get { return (List<Forms.MenuItem>)GetValue(FormsContextMenuProperty); }
            set { SetValue(FormsContextMenuProperty, value); }
        }

namespace IconForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
//说明,程序运行后自动隐藏到任务栏右侧的通知栏里, 
//1        右击选择退出,隐藏,显示
//2        双击可以隐藏和显示切换

        public ImageSource Icon
        {
            get { return (ImageSource)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

 //一 右击窗体,选择属性,转到事件页面,双击 Load,SizeChanged事件,给窗体添加代码
        private void Form1_Load(object sender, EventArgs e)
        {
         //1.将Form属性ShowInTaskbar改为false,这样程序将不会在任务栏中显示。
         //2.将Form属性WindowState选择为 Minimized,以便起来自动最小化隐藏。
            string startup = Application.ExecutablePath;       //取得程序路径   
            int pp = startup.LastIndexOf("\");
            startup = startup.Substring(0, pp);
            string icon = startup + "\testIcon.ico";
         //3.一定为notifyIcon1其设置图标,否则无法显示在通知栏。或者在其属性中设置
            notifyIcon1.Icon = new Icon(icon);

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        }

        public event MouseButtonEventHandler MouseClick
        {
            add { AddHandler(MouseClickEvent, value); }
            remove { RemoveHandler(MouseClickEvent, value); }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide(); //或者是this.Visible = false;
                this.notifyIcon1.Visible = true;
            }

        public event MouseButtonEventHandler MouseDoubleClick
        {
            add { AddHandler(MouseDoubleClickEvent, value); }
            remove { RemoveHandler(MouseDoubleClickEvent, value); }
        }

        }

        #region Conversion members

//二 双击窗体上的菜单项,添加相关代码
        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要退出程序吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }

        private static Drawing.Icon FromImageSource(ImageSource icon)
        {
            if (icon == null)
            {
                return null;
            }
            Uri iconUri = new Uri(icon.ToString());
            return new Drawing.Icon(Application.GetResourceStream(iconUri).Stream);
        }

        }

        private static bool FromVisibility(Visibility visibility)
        {
            return visibility == Visibility.Visible;
        }

        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private MouseButton ToMouseButton(Forms.MouseButtons button)
        {
            switch (button)
            {
                case Forms.MouseButtons.Left:
                    return MouseButton.Left;
                case Forms.MouseButtons.Right:
                    return MouseButton.Right;
                case Forms.MouseButtons.Middle:
                    return MouseButton.Middle;
                case Forms.MouseButtons.XButton1:
                    return MouseButton.XButton1;
                case Forms.MouseButtons.XButton2:
                    return MouseButton.XButton2;
            }
            throw new InvalidOperationException();
        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();

        #endregion Conversion members
    }

        }

  } 

//三 转到窗体设计模式,右击notifyIcon1 ,选择属性,双击其中DoubleClick,添加相关代码
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;

  

                this.Hide();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }

在主窗口上引用该命名控件 xmlns:l="clr-namespace:WpfApplication1"

        }
    }
}

在主窗口里添加如下代码

        <l:NotificationAreaIcon
                      Text="NotificationAreaApplication1"
                      Icon="ResourcesNotificationAreaIcon.ico"
                      MouseDoubleClick="OnNotificationAreaIconDoubleClick" Grid.ColumnSpan="2">
            <l:NotificationAreaIcon.MenuItems>
                <forms:MenuItem Text="关于作者" Click="OnMenuItemAboutClick"/>
                <forms:MenuItem Text="-" />
                <forms:MenuItem Text="Open" Click="OnMenuItemOpenClick" DefaultItem="True" />
                <forms:MenuItem Text="-" />
                <forms:MenuItem Text="Exit" Click="OnMenuItemExitClick" />
            </l:NotificationAreaIcon.MenuItems>

   </l:NotificationAreaIcon>  

后台定义如下状态,并重载其Window类的OnStateChanged和OnClosing方法,代码如下:

        WindowState lastWindowState;
        bool shouldClose;

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DataBind();
        }

        protected override void OnStateChanged(EventArgs e)
        {
            lastWindowState = WindowState;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (!shouldClose)
            {
                e.Cancel = true;
                Hide();
            }

并为托盘菜单添加事件:

        protected void OnMenuItemAboutClick(object sender, EventArgs e)
        {
            new Window1().Show();
        }

        private void OnNotificationAreaIconDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                Open();
            }
        }

        private void OnMenuItemOpenClick(object sender, EventArgs e)
        {
            Open();
        }

        private void Open()
        {
            Show();
            WindowState = lastWindowState;
        }

        private void OnMenuItemExitClick(object sender, EventArgs e)
        {
            shouldClose = true;
            Close();

完成,运行我们的程序,看到托盘已经有我们添加的菜单了:

澳门新濠3559 1 

 DEMO:/Files/leeolevis/NotifiWpfAppl.rar

编辑:操作系统 本文来源:右键处理的代码在哪里,(澳门新濠3559与系统托

关键词: