111,129
社区成员
发帖
与我相关
我的任务
分享
public const int SW_SHOWNORMAL = 1;
[DllImport("User32.dll")]
public static extern bool ShowWindowAsync(
IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
public static extern bool
SetForegroundWindow(IntPtr hWnd);

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ComboBox cbx = new ComboBox();
cbx.Items.Add("Open");
cbx.Items.Add("Exit"); ;
cbx.SelectedIndexChanged += new EventHandler(cbx_SelectedIndexChanged);
}
void cbx_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cbx = (sender as ComboBox);
//选择Open则新开窗口
if(cbx.SelectedItem.ToString() == "Open")
{
Form newForm = new Form();
newForm.Show();
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.F1)
{
string helpChm = @"c:\Help.chm";
Process helpProcess = null;
if (File.Exists(helpChm))
{
//避免重复打开
if (helpProcess != null && !helpProcess.HasExited)
{
User32.ShowWindowAsync(helpProcess.MainWindowHandle, User32.SW_SHOWNORMAL);
User32.SetForegroundWindow(helpProcess.MainWindowHandle);
return;
}
helpProcess = Process.Start(helpChm);
helpProcess.Exited += delegate(object sender, EventArgs e)
{
helpProcess.Dispose();
helpProcess= null;
};
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
}