zoukankan      html  css  js  c++  java
  • C#编写WIN32系统托盘程序

    基本功能概述:

    1. 程序运行后驻留系统托盘,左键呼出,右键退出。后续可加右键菜单。
    2. 注册系统案件WIN+F10,呼出程序。
    3. 重写系统消息,最小化和关闭按钮隐藏程序
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    public enum HotkeyModifiers
    {
        Alt = 1,
        Control = 2,
        Shift = 4,
        Win = 8
    }
    
    public class MyForm:Form
    {
        [DllImport ("user32.dll")]
        private static extern bool RegisterHotKey (IntPtr hWnd, int id, int modifiers, Keys vk);
    
        [DllImport ("user32.dll")]
        private static extern bool UnregisterHotKey (IntPtr hWnd, int id);
    
        const int WM_HOTKEY = 0x312;
        const int WM_SYSCOMMAND = 0X112;
        const int SC_MAXMIZE = 0xf030;
        const int SC_MINMIZE = 0xf020;
        const int SC_CLOSE = 0xf060;
    
        public MyForm ()
        {
            NotifyIcon ni = new NotifyIcon (){ Icon = this.Icon, Visible = true };
            //RegisterHotKey
            bool bOK = RegisterHotKey (this.Handle, 0, (int)HotkeyModifiers.Win, Keys.F10);
    
            this.Closing += delegate {
                UnregisterHotKey (this.Handle, 0);
            };
    
            ni.MouseDown += (sender, e) => {
                if (e.Button == MouseButtons.Left) {
                    this.Activate ();
                    this.Visible = true;
                }
                if (e.Button == MouseButtons.Right) {
                    if (DialogResult.Yes==MessageBox.Show("Quit? Realy?","Quit",MessageBoxButtons.YesNo)) {
                        this.Close ();
                    }
                }
            };
        }
    
        //WndProc
        protected override void WndProc (ref Message m)
        {
            switch (m.Msg) {
            case WM_SYSCOMMAND:
                int code = m.WParam.ToInt32 ();
                if (code == SC_CLOSE || code == SC_MINMIZE) {
                    this.Visible = false;
                    return;//Must Prevent WndProc
                }
                break; //others, such as SC_MAXMIZE must in WndProc.
            case WM_HOTKEY:
                this.Text = DateTime.Now.ToString ();
                this.Activate ();
                this.Visible = true;
                break;
            }
            base.WndProc (ref m);
        }
    }
    
    public class MyClass
    {
        public static void Main ()
        {
            MyForm form = new MyForm ();
            Application.Run (form);
        }
    }
  • 相关阅读:
    Spring JDBC配置数据源
    Eclipse创建一个Maven Web项目
    部署基于Maven的war文件到Tomcat
    使用“mvn site-deploy”部署站点(WebDAV例子)
    生成基于Maven的项目文档站点
    将项目安装到Maven本地资源库
    使用Maven运行单元测试
    使用Maven清理项目
    使用Maven构建项目
    Dubbo的使用入门
  • 原文地址:https://www.cnblogs.com/flaaash/p/5319078.html
Copyright © 2011-2022 走看看