C#winform如何将隐藏的主窗体,用windows快捷键显示出来,如QQ按Ctr+Shift+alt即显示出来

如题:感谢!100分赏。

全局钩子
我做的例子
按F10,隐藏,Ctr+Shift+alt 显示
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.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Reflection;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Hook m_hook = new Hook();
public Form1()
{
InitializeComponent();
ActiveHook.SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 800, 600, ActiveHook.SWP_NOACTIVATE);
m_hook.OnKeyDown += new Hook.KeyboardDelegate(OnHookKeyDown);
}
void OnHookKeyDown(KeyEventArgs e)
{
OnKeyDown(e);
if (e.Handled)
{
return;
}
if (((Control.ModifierKeys & Keys.Shift) == Keys.Shift) && ((Control.ModifierKeys & Keys.Control) == Keys.Control)
&& ((Control.ModifierKeys & Keys.Alt) == Keys.Alt))
{

if (!this.Visible)
{
this.Show();
}
}
if (e.KeyCode == Keys.F10)
{
this.Hide();
}
}
private void Form1_Load(object sender, EventArgs e)
{
m_hook.SetHook(true);
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
m_hook.SetHook(false);
}
}
class ActiveHook
{
/*****************************************
* Created By: Jickie 阿文
* Created: 2009/10/29
* **************************************/

public const int WM_KEYDOWN = 0x0100;
public const int WM_KEYUP = 0x0101;
public const int WM_CHAR = 0x0102;

public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOMOVE = 0x0002;
public const int SWP_NOZORDER = 0x0004;
public const int SWP_NOREDRAW = 0x0008;
public const int SWP_NOACTIVATE = 0x0010;
public const int SWP_FRAMECHANGED = 0x0020; /* The frame changed: send WM_NCCALCSIZE */
public const int SWP_SHOWWINDOW = 0x0040;
public const int SWP_HIDEWINDOW = 0x0080;
public const int SWP_NOCOPYBITS = 0x0100;
public const int SWP_NOOWNERZORDER = 0x0200; /* Don't do owner Z ordering */
public const int SWP_NOSENDCHANGING = 0x0400; /* Don't send WM_WINDOWPOSCHANGING */

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int flags);

public const uint WS_OVERLAPPED = WS_BORDER | WS_CAPTION;
public const uint WS_CLIPSIBLINGS = 0x04000000;
public const uint WS_CLIPCHILDREN = 0x02000000;
public const uint WS_CAPTION = 0x00C00000; /* WS_BORDER | WS_DLGFRAME */
public const uint WS_BORDER = 0x00800000;
public const uint WS_DLGFRAME = 0x00400000;
public const uint WS_VSCROLL = 0x00200000;
public const uint WS_HSCROLL = 0x00100000;
public const uint WS_SYSMENU = 0x00080000;
public const uint WS_THICKFRAME = 0x00040000;
public const uint WS_MAXIMIZEBOX = 0x00020000;
public const uint WS_MINIMIZEBOX = 0x00010000;
public const uint WS_SIZEBOX = WS_THICKFRAME;
public const uint WS_POPUP = 0x80000000;
public const uint WS_CHILD = 0x40000000;
public const uint WS_VISIBLE = 0x10000000;
public const uint WS_DISABLED = 0x08000000;

public const uint WS_EX_DLGMODALFRAME = 0x00000001;
public const uint WS_EX_TOPMOST = 0x00000008;
public const uint WS_EX_TOOLWINDOW = 0x00000080;
public const uint WS_EX_WINDOWEDGE = 0x00000100;
public const uint WS_EX_CLIENTEDGE = 0x00000200;

public const uint WS_EX_CONTEXTHELP = 0x00000400;
public const uint WS_EX_STATICEDGE = 0x00020000;
public const uint WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);

public const int GWL_STYLE = (-16);
public const int GWL_EXSTYLE = (-20);

[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLongPtr32(IntPtr hWnd, int nIndex, int dwNewLong);

public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

public const int WH_KEYBOARD = 2;
public const int WH_MOUSE = 7;
public const int WH_KEYBOARD_LL = 13;
public const int WH_MOUSE_LL = 14;

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);

[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}

[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
public class KeyboardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
}

public class Hook
{
public delegate void KeyboardDelegate(KeyEventArgs e);
public KeyboardDelegate OnKeyDown;
int m_hHook = 0;
ActiveHook.HookProc m_HookCallback;

public void SetHook(bool enable)
{
if (enable && m_hHook == 0)
{
m_HookCallback = new ActiveHook.HookProc(HookCallbackProc);
Module module = Assembly.GetExecutingAssembly().GetModules()[0];
m_hHook = ActiveHook.SetWindowsHookEx(ActiveHook.WH_KEYBOARD_LL, m_HookCallback, Marshal.GetHINSTANCE(module), 0);
return;
}

if (enable == false && m_hHook != 0)
{
ActiveHook.UnhookWindowsHookEx(m_hHook);
m_hHook = 0;
}
}
int HookCallbackProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < 0)
{
return ActiveHook.CallNextHookEx(m_hHook, nCode, wParam, lParam);
}
else
{
ActiveHook.KeyboardHookStruct hookstruct = (ActiveHook.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(ActiveHook.KeyboardHookStruct));

if (OnKeyDown != null && wParam.ToInt32() == ActiveHook.WM_KEYDOWN)
{
Keys key = (Keys)hookstruct.vkCode;
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
key |= Keys.Shift;
if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
key |= Keys.Control;
if ((Control.ModifierKeys & Keys.Alt) == Keys.Alt)
key |= Keys.Alt;

KeyEventArgs e = new KeyEventArgs(key);
e.Handled = false;
OnKeyDown(e);
if (e.Handled)
return 1;
}
int result = 0;
if (m_hHook != 0)
result = ActiveHook.CallNextHookEx(m_hHook, nCode, wParam, lParam);
return result;
}
}
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-10-29
using System.Runtime.InteropServices;

Boolean hide = true;

[DllImport("user32.dll", SetLastError = true)] //导入注册函数
public static extern bool RegisterHotKey(
IntPtr hWnd, // handle to window
int id, // hot key identifier
KeyModifiers fsModifiers, // key-modifier options
Keys vk // virtual-key code
);

[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);

[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
private void Form1_Load(object sender, EventArgs e)
{
RegisterHotKey(Handle, 100, 0, Keys.F10);//窗体载入时注册热键
}

protected override void WndProc(ref Message m)//监视Windows消息
{
const int WM_HOTKEY = 0x0312;//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
this.menuItem10.PerformClick(); //调用主处理程序
break;
}
base.WndProc(ref m);
}

private void menuItem10_Click(object sender, EventArgs e)
{

if (hide == true)
{
this.ShowInTaskbar = false;
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
hide = false;
}
else
{
this.ShowInTaskbar = true;
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
hide = true;

}
}
以上代码按下F10键能够将窗体隐藏,但隐藏后再无法调出,按下alt+tab能调出,可是再按F10键就再也无法隐藏了,我只能做到这

或者你可以使用下面:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//只按一个键
if (e.KeyCode == Keys.Enter)
{
//...............
}
else if (e.KeyCode == Keys.A)
{
//......................
}
}

//和alt,ctrl,shift组合使用的快捷键
private void Form1_KeyDown(object sender, KeyEventArgs e)
{

//这里可以是e.Alt,e.shift,e.ctrl
if (e.Alt && e.KeyCode == Keys.A)
{
MessageBox.Show(“ “);
}
}
第2个回答  2009-10-29
在百度搜索

"易学网 注册系统热键(MainFormHotKey)"
这里有个源码实例!
第3个回答  2009-10-29
易学网 注册系统热键(MainFormHotKey)"
第4个回答  2009-10-30
留下邮箱。。我发给你源码

C#winform如何将隐藏的主窗体,用windows快捷键显示出来,如QQ按Ctr+S...
按F10,隐藏,Ctr+Shift+alt 显示 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.Drawing.Drawing2D;using System.Runtime.InteropServices;using System.Reflect...

c#:winform如何让一堆控件平时隐藏,在点击触发时才 显示出来?如图。
每个控件都有一个属性值Visibility是控制这个,有三个属性值,Visible是默认属性值可见的,Hidden是隐藏不占空间,如果是个button控件就不能点击,collapsed是隐藏但是占空间,如果是个button控件就依旧可以在那个位置点击但是看不见,只需创建个事件控制这个属性值就可以达到你要的效果了 ...

C#WinForm如何在点击按钮获取数据后,将数据在按钮所在窗体上显示...
根据返回的数据行,动态计算出需要增加的高度,重新设置窗体的高度。再将数据显示到表格中即可。如果数据为空,则隐藏表格。

C#如何激活外部隐藏窗体
我们知道托盘程序运行后是无法看见主窗体的,他只会显示在工具栏上。在用Visual C#设计此类程序的时候,可以用二种方法使得程序运行后不显示主窗体。其中一种方法是重载主窗体中的OnActivated( )事件,OnActivated( )事件是在窗体激活的时候才触发的。通过重载此事件可以达到隐藏主窗体的目的。具体程序代码...

C#中WinForm窗体里面的一些按钮或是菜单选项如何设置快捷键呢
选中你要设置快捷键的菜单项,在属性栏中找到shortcutkeys,单击下拉箭头,选择一个修饰符,ctrl或者alt,再选择一个按键即可~~~

C# 给Winform 控件添加快捷键的几种方法
在大家给button、label、menuStrip等控件设置Text属性时在名字后边加&键名就可以了,比如button1.text= "确定(&O)"。就会有快捷键了,这时候按Alt+O就可以执行按钮单击事件。第二种:Ctrl+*及其他组合键 在WinForm中设置要使用组合键的窗体的KeyPreview(向窗体注册键盘事件)属性为True;然后使用窗体的Key...

如何在C#的应用程序中添加快捷键设置?
第一种:Alt + *(按钮快捷键)\\x0d\\x0a\\x0d\\x0a 在大家给button、label、menuStrip等控件设置Text属性时在名字后边加&键名就可以了,比如button1.text= "确定(&O)"。就会有快捷键了,这时候按Alt+O就可以执行按钮单击事件。\\x0d\\x0a\\x0d\\x0a第二种:Ctrl+*及其他组合键\\x0d\\x...

C#Winform 怎么实现隐藏主窗体,关闭子窗体后又回到主窗体?
m1为主窗体 m2为子窗体 在m1中.this.visible=false;\/\/在m2中定义一个变量,用于记忆主窗体;public Form ParentForm; \/\/用公共变量或者属性都可以 \/\/把当前窗体送给子窗体记忆;m2.ParentForm=this;m2.Show();\/\/在m2关闭时;this.ParentForm.Visible=true;...

C# WinForm窗体界面设置问题
设置方法:一:Form对象 属性:设计中的Name:窗体类的类名 AcceptButton:窗口的确定按钮 CancelButton:窗口按ESC的取消按钮 1.外观 Backcolor:背景颜色 Forecolor:字体颜色 backgroundImage:背景图片 Font:设置字体 Formborderstyle:边框样式,常用Fixedsingle固定,sizeable可调 Text:标题栏文字 2.窗口...

C# MessageBox弹不出来,Alt键按下才显示
原因是我在窗体的Paint事件里写了很多绘图的代码,弹出MessageBox的时候(即将弹出而未弹出)触发了Paint事件,程序转而执行绘图等其它操作了,而且无法跳出Paint事件,直到按下Alt键才能回到原来的位置继续执行MessageBox弹出语句。因此将Paint中的代码移走,问题立马解决。其实我的程序不需要在Paint里面写代码,...

相似回答