C# winform 程序在后台运行 点击快捷键使程序做出相应反应。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# winform 程序在后台运行 点击快捷键使程序做出相应反应。相关的知识,希望对你有一定的参考价值。

如题:网上都说用注册热键的方法,但是我一直没弄出来,望高手指点。

如果没有报错的话.看看是不是内存不够.或者系统的类库不足.例如你调用COM组件.引用了其他的DLL等等.都是可能存在的问题。

对这个需求完全可以在单击“关闭”按钮的时候弹出一个对话框,来让用户确定是否真的要退出。这是一个很好的解决方法,并且实现也是很容易的。但是人家不想这样,想要拥有类似QQ在托盘区后台运行的那种效果,没办法,只能想办法来实现了。www.2cto.com
 
.
 
[csharp]
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  
  
using System.Windows;  
  
  
namespace winform窗体托盘后台运行  
  
    public partial class Form1 : Form   
      
        //这里在窗体上没有拖拽一个NotifyIcon控件,而是在这里定义了一个变量  
        private NotifyIcon notifyIcon = null;  
  
        public Form1()  
          
            InitializeComponent();  
            //调用初始化托盘显示函数  
            InitialTray();  
          
  
        private void Form1_Load(object sender, EventArgs e)  
          
            //这里写其他代码  
          www.2cto.com
  
        /// <summary>  
        /// 窗体关闭的单击事件  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
          
            e.Cancel = true;  
            //通过这里可以看出,这里的关闭其实不是真正意义上的“关闭”,而是将窗体隐藏,实现一个“伪关闭”  
            this.Hide();  
          
  
        private void InitialTray()  
          
            //隐藏主窗体  
            this.Hide();  
  
            //实例化一个NotifyIcon对象  
            notifyIcon = new NotifyIcon();  
            //托盘图标气泡显示的内容  
            notifyIcon.BalloonTipText = "正在后台运行";     
            //托盘图标显示的内容  
            notifyIcon.Text = "窗体托盘后台运行测试程序";  
            //注意:下面的路径可以是绝对路径、相对路径。但是需要注意的是:文件必须是一个.ico格式  
            notifyIcon.Icon = new System.Drawing.Icon("E:/ASP项目/images/3.5 inch Floppy.ico");  
            //true表示在托盘区可见,false表示在托盘区不可见  
            notifyIcon.Visible = true;  
            //气泡显示的时间(单位是毫秒)  
            notifyIcon.ShowBalloonTip(2000);              
            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);  
              
            ////设置二级菜单  
            //MenuItem setting1 = new MenuItem("二级菜单1");  
            //MenuItem setting2 = new MenuItem("二级菜单2");  
            //MenuItem setting = new MenuItem("一级菜单", new MenuItem[]setting1,setting2);  
  
            //帮助选项,这里只是“有名无实”在菜单上只是显示,单击没有效果,可以参照下面的“退出菜单”实现单击事件  
            MenuItem help = new MenuItem("帮助");  
  
            //关于选项  
            MenuItem about = new MenuItem("关于");  
  
            //退出菜单项  
            MenuItem exit = new MenuItem("退出");  
            exit.Click += new EventHandler(exit_Click);  
  
            ////关联托盘控件  
            //注释的这一行与下一行的区别就是参数不同,setting这个参数是为了实现二级菜单  
            //MenuItem[] childen = new MenuItem[]  setting, help, about, exit ;  
            MenuItem[] childen = new MenuItem[] help,about,exit;  
            notifyIcon.ContextMenu = new ContextMenu(childen);  
  
            //窗体关闭时触发  
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);  
          
        
        /// <summary>  
        /// 鼠标单击  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)  
          
            //鼠标左键单击  
            if (e.Button == MouseButtons.Left)  
              
                //如果窗体是可见的,那么鼠标左击托盘区图标后,窗体为不可见  
                if (this.Visible == true )  
                  
                    this.Visible = false;  
                  
                else  
                  
                    this.Visible = true;  
                    this.Activate();  
                  
              
          
  
        /// <summary>  
        /// 退出选项  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void exit_Click(object sender, EventArgs e)  
          
            //退出程序  
           System.Environment.Exit(0);    
          
      

追问

你这个是做窗体最小化到托盘把?!!我是想实现,窗体在托盘运行,当我按下快捷键后,触发程序作出相应反应。

参考技术A         //初始化函数里加入以下两条就可以用了,我现在定义的是ALT+X与ALT+S
        //这两种组合键,你自己可以改
        public Frmain()
        
            InitializeComponent();
            RegisterHotKey(this.Handle, 10, MODKEY.ALT, Keys.X);
            RegisterHotKey(this.Handle, 11, MODKEY.ALT, Keys.S);
        
        protected override void WndProc(ref Message m)
        
            if(m.Msg == 0x0312)
            
                switch (m.WParam.ToInt32())
                
                    case 10:
                        退出ToolStripMenuItem_Click(null,null);
                        break;
                    case 11:
                        notifyIcon1_Click(null,null);
                        break;
                    default:
                        break;
                
                return;
            
            base.WndProc(ref m);
        

        //要定义热键的窗口的句柄
        //定义热键ID(不能与其它ID重复)int id, 
        //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效KeyModifiers fsModifiers,                 Keys vk                     //定义热键的内容
        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr wnd, int id, MODKEY mode, Keys vk);

        [DllImport("user32.dll")]
        public static extern bool UnregisterHotKey(IntPtr wnd, int id);

        [Flags()]
        public enum MODKEY
        
            None = 0,
            ALT = 0x0001,
            CTRL = 0x0002,
            SHIFT = 0x0004,
            WIN = 0x0008,
        

刚好写完现成的。还热着呢,给你了,试过可用了。不过不能开同个窗口或是与这热键有冲突的程序。要不然会有其中的一个程序会没有响应的

本回答被提问者采纳
参考技术B 热键是在界面不在后台的情况下使用的 你应该是用windows消息回调函数来判断按下了组合键没有

1、向Windows窗口发送Alt组合键
先看WM_SYSKEYDOWN的help:The WM_SYSKEYDOWN message is posted to the window with the keyboard focus when the user holds down the ALT key and then presses another key. It also occurs when no window currently has the keyboard focus; in this case, the WM_SYSKEYDOWN message is sent to the active window. The window that receives the message can distinguish between these two contexts by checking the context code in the lKeyData parameter.WM_SYSKEYDOWN nVirtKey = (int) wParam; // virtual-key code lKeyData = lParam; // key data ParametersnVirtKeyValue of wParam. Specifies the virtual-key code of the key being pressed. lKeyDataValue of lParam. Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table:Value Description0-15 Specifies the repeat count. The value is the number of times the keystroke is repeated as a result of the user holding down the key.16-23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM).24 Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.25-28 Reserved; do not use.29 Specifies the context code. The value is 1 if the ALT key is down while the key is pressed; it is 0 if the WM_SYSKEYDOWN message is posted to the active window because no window has the keyboard focus.30 Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.31 Specifies the transition state. The value is always 0 for a WM_SYSKEYDOWN message.之前曾经修改过keyData的16-23位为VK_MENU,第30位参数为1,但没效果请看位29的说明!!The value is 1 if the ALT key is down while the key is pressed; 当值为1时表示ALT键被按下!这不正是我需要的吗?于是把29位设置为1,函数调用变成PostMessage(hWnd,WM_SYSKEYDOWN,0x41,1<<29);经过测试,发现这个就是Alt+A的效果!!追问

太乱了。。。。看不清。。。

追答

http://blog.csdn.net/pjl1119/article/details/6914902

c#的 winform怎么使弹出的窗口在屏幕的正中间,就是当我点击链接弹出的窗口。 急~在线等!!!!!!

C#的winform窗口居中只需要设置startposition属性即可,接下来用visual studio 2015创建一个winform程序具体演示一下:

1,打开visual studio,窗口windows窗体应用程序,如下图所示

2,右键单击窗体,选择属性,如下图所示

3,找到StartPosition属性,设置为CenterScreen,如下图所示

4,运行程序以后,winform窗口就在屏幕中间显示了,如下图所示

参考技术A ⒈ Form.StartPosition 属性可以调整窗口初始的显示位置。
⒉ 如果是外部窗体,则需要通过 API 函数查找标题,获取窗口句柄,然后调整位置。
比如 MessageBox位置 和 链接打开的浏览器窗口 位置,就不是 C# 能够直接获取的。
相关函数名为 FindWindow、MoveWindow、GetWindowRect

以上是关于C# winform 程序在后台运行 点击快捷键使程序做出相应反应。的主要内容,如果未能解决你的问题,请参考以下文章

c#的 winform怎么使弹出的窗口在屏幕的正中间,就是当我点击链接弹出的窗口。 急~在线等!!!!!!

【C#】WinForm中如何让窗口最小化时直接最小化到托盘,后台运行

C# winForm窗体程序 锁定问题求赐教。

小5聊C# Winform窗体,程序点击运行第二次自动判断显示,解决不重复打开两个相同窗体

如何关闭电脑后台运行的程序

C# - 为啥全屏 Winform 应用程序不总是覆盖任务栏?