我看了下博客园提供的一些方案,由于窗体最小化和最大化,窗体的句柄会发生变化,所以,按热键之后,会有可能无效。因为句柄发生了变化。
那么,如果在快捷键按下时候,准确获得winform的句柄,并使得之前最小化的窗体,恢复之前的状态呢。
[C#] 纯文本查看 复制代码 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.Runtime.InteropServices;
using System.Diagnostics;
using System.Collections;
namespace 丝材管控
{
public partial class Form1 : Form
{
#region
//创建NotifyIcon对象
NotifyIcon notifyicon = new NotifyIcon();
//创建托盘图标对象
Icon ico = new Icon("snow.ico");
//创建托盘菜单对象
ContextMenu notifyContextMenu = new ContextMenu();
#endregion
public Form1()
{
InitializeComponent();
}
public void OnHotkey(int HotkeyID) //Alt +F3 显示和隐藏窗体
{
if (HotkeyID == Hotkey.Hotkey1)
{
if (this.Visible == true)
this.Visible = false;
else
this.Visible = true;
showForm();
}
else
{
this.Visible = false;
}
}
public void showForm()
{
//判断是否已经最小化于托盘
//if (WindowState == FormWindowState.Minimized)
//{
//还原窗体显示
WindowState = FormWindowState.Normal;
//激活窗体并给予它焦点
this.Activate();
//任务栏区显示图标
this.ShowInTaskbar = true;
//托盘区图标隐藏
notifyicon.Visible = false;
//}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 取消关闭窗体
e.Cancel = true;
// 将窗体变为最小化
this.WindowState = FormWindowState.Minimized;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
button1.Enabled = false;
comboBox1.Enabled = false;
dateTimePicker1.Enabled = false;
DateTime action = DateTime.Now;
TimeSpan ts = DateTime.Now - dateTimePicker1.Value;
int m = (Int32)ts.TotalMinutes;
int TotalHour = judgeHour();
int RsultMinutes = TotalHour * 60 - m;
int H = (int)(RsultMinutes / 60);
int M = RsultMinutes % 60;
string Text = H + "小时" + M + "分钟";
if (comboBox1.Text == "")
{
toolStripStatusLabel2.Text = "--";
MessageBox.Show("请选择丝材!", "Message:");
}
else
toolStripStatusLabel2.Text = Text;
if (H < 0)
{
MessageBox.Show("不能为负数!", "Message:");
}
if (H > TotalHour)
{
MessageBox.Show("有效期不能大于" + TotalHour + "小时!", "Message:");
}
if (M < 0)
{
MessageBox.Show("丝材已经过期,请及时更换丝材!", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
}
}
public int judgeHour()
{
int Hour = 0;
string caseSwitch = comboBox1.Text;
switch (caseSwitch)
{
case "Cu(铜线)":
Hour = 72;
break;
case "Pd(镀钯线)":
Hour = 96;
break;
case "Al(合金线)":
Hour = 96;
break;
}
return Hour;
}
private void timer1_Tick(object sender, EventArgs e)
{
DateTime action = DateTime.Now;
TimeSpan ts = DateTime.Now - dateTimePicker1.Value;
int m = (Int32)ts.TotalMinutes;
int TotalHour = judgeHour();
int RsultMinutes = TotalHour * 60 - m;
int H = (int)(RsultMinutes / 60);
int M = RsultMinutes % 60;
string Text = H + "小时" + M + "分钟";
if (comboBox1.Text == "")
{
toolStripStatusLabel2.Text = "--";
}
else
toolStripStatusLabel2.Text = Text;
if ((H == 0) && (M == 0))
{
MessageBox.Show("丝材已经过期,请及时更换丝材!", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Hotkey hotkey;
hotkey = new Hotkey(this.Handle);
Hotkey.Hotkey1 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F3, Hotkey.KeyFlags.MOD_ALT); //定义快键(Alt+ F3)
hotkey.OnHotkey += new HotkeyEventHandler(OnHotkey);
//设置鼠标放在托盘图标上面的文字
this.notifyIcon1.Text = "丝材管控";
}
private void 查看记录ToolStripMenuItem_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = true;
comboBox1.Enabled = true;
dateTimePicker1.Enabled = true;
toolStripStatusLabel2.Text = "--";
timer1.Stop();
}
//private void Form1_SizeChanged(object sender, EventArgs e)
//{
// //判断是否选择的是最小化按钮
// if (WindowState == FormWindowState.Minimized)
// {
// //托盘显示图标等于托盘图标对象
// //注意notifyIcon1是控件的名字而不是对象的名字
// notifyIcon1.Icon = ico;
// //隐藏任务栏区图标
// this.ShowInTaskbar = false;
// //图标显示在托盘区
// notifyicon.Visible = true;
// }
//}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
//判断是否已经最小化于托盘
if (WindowState == FormWindowState.Minimized)
{
//还原窗体显示
WindowState = FormWindowState.Normal;
//激活窗体并给予它焦点
this.Activate();
//任务栏区显示图标
this.ShowInTaskbar = true;
//托盘区图标隐藏
notifyicon.Visible = false;
}
}
private void 切换ToolStripMenuItem_Click(object sender, EventArgs e)
{
//判断是否选择的是最小化按钮
if (this.Visible == true)
this.Visible = false;
else
this.Visible = true;
}
}
#region 获取当前程序窗体句柄
class CurrentProcess
{
private static Hashtable processWnd = null;
public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
static CurrentProcess()
{
if (processWnd == null)
{
processWnd = new Hashtable();
}
}
[DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
[DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
[DllImport("user32.dll", EntryPoint = "IsWindow")]
public static extern bool IsWindow(IntPtr hWnd);
[DllImport("kernel32.dll", EntryPoint = "SetLastError")]
public static extern void SetLastError(uint dwErrCode);
public IntPtr GetCurrentWindowHandle(Process process)
{
IntPtr ptrWnd = IntPtr.Zero;
uint uiPid = (uint)process.Id; // 进程 ID
object objWnd = processWnd[uiPid];
if (objWnd != null)
{
ptrWnd = (IntPtr)objWnd;
if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd)) // 从缓存中获取句柄
{
return ptrWnd;
}
else
{
ptrWnd = IntPtr.Zero;
}
}
bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
// 枚举窗口返回 false 并且没有错误号时表明获取成功
if (!bResult && Marshal.GetLastWin32Error() == 0)
{
objWnd = processWnd[uiPid];
if (objWnd != null)
{
ptrWnd = (IntPtr)objWnd;
}
}
return ptrWnd;
}
private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
{
uint uiPid = 0;
if (GetParent(hwnd) == IntPtr.Zero)
{
GetWindowThreadProcessId(hwnd, ref uiPid);
if (uiPid == lParam) // 找到进程对应的主窗口句柄
{
processWnd[uiPid] = hwnd; // 把句柄缓存起来
SetLastError(0); // 设置无错误
return false; // 返回 false 以终止枚举窗口
}
}
return true;
}
}
#endregion
}
|