这是一个c#写的winform程序,一个网页管理器。
using InputManager;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowController
{
public partial class Form1 : Form
{
private WebBrowser webBrowser = null;
private string URL = null;
private int WindowIndex = 0;
private Form1 parentForm = null;
private Screen[] Screens = null;
private List<Form> FormList = new List<Form>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitMethod();
}
/// <summary>
/// 初始化
/// </summary>
private void InitMethod()
{
//PowerBootMethod();
parentForm = this;
KeyboardHook.KeyDown += new KeyboardHook.KeyDownEventHandler(MyGetKeyDown);
//KeyboardHook.KeyUp += new KeyboardHook.KeyUpEventHandler(MyGetKeyUp);
KeyboardHook.InstallHook();
this.FormBorderStyle = FormBorderStyle.None;
GetScreenMethod();
}
/// <summary>
/// 开机自启
/// </summary>
private void PowerBootMethod()
{
string path = Application.ExecutablePath;
RegistryKey rk = Registry.CurrentUser;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue("WindowController", path);
rk2.Close();
rk.Close();
MessageBox.Show("设置成功,重启计算机生效", "提示");
}
/// <summary>
/// 取消开机自启
/// </summary>
private void DeletePowerBootMethod()
{
string path = Application.ExecutablePath;
RegistryKey rk = Registry.CurrentUser;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
if (rk2.GetValue("WindowController") != null)
{
rk2.DeleteValue("WindowController", false);
}
if (rk2.GetValue("WindowController") == null)
{
MessageBox.Show("设置成功", "提示");
}
rk2.Close();
rk.Close();
}
/// <summary>
/// 添加新的窗口
/// </summary>
private void AddNewWebBrowserMethod()
{
if (string.IsNullOrEmpty(URL))
return;
Form form = new Form();
form.Name = "Window" + WindowIndex.ToString();
form.StartPosition = FormStartPosition.Manual;
form.Location = new Point(Screens[WindowIndex].Bounds.Left, Screens[WindowIndex].Bounds.Top);
form.FormBorderStyle = FormBorderStyle.None;
form.WindowState = FormWindowState.Maximized;
form.FormClosing += Form_FormClosing;
webBrowser = new WebBrowser();
this.webBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser.Location = new System.Drawing.Point(0, 0);
this.webBrowser.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser.Name = "webBrowser" + WindowIndex.ToString();
this.webBrowser.Size = new System.Drawing.Size(1185, 560);
this.webBrowser.TabIndex = 0;
string url = null;
if (URL.Length > 4 && URL.ToLower().Trim().Substring(0, 4).Contains("http"))
{
url = URL;
}
else
{
url = "http://" + URL;
}
try
{
webBrowser.Url = new Uri(url.Trim());
}
catch (Exception e)
{
MessageBox.Show("URLError: " + e.Data);
}
form.Controls.Add(webBrowser);
//WindowIndex++;
this.Hide();
form.TopMost = true;
form.Show();
URLList.Items.Add(url);
URLList.SelectedIndex = 0;
FormList.Add(form);
//Console.WriteLine("click button" + form.KeyPreview);
}
/// <summary>
/// 屏蔽退出键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
if (!parentForm.Visible)
{
e.Cancel = true;
}
}
/// <summary>
/// 全局监听钩子
/// </summary>
/// <param name="vkCode"></param>
private void MyGetKeyDown(int vkCode)
{
//Console.WriteLine("Keycode: " + vkCode);
if (vkCode == 112)
{
PassWord.Text = "";
GetScreenMethod();
parentForm.TopMost = true;
parentForm.Show();
}
else
{
}
}
/// <summary>
/// 添加新窗口的按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddButton_Click(object sender, EventArgs e)
{
this.URL = this.URLText.Text;
AddNewWebBrowserMethod();
}
/// <summary>
/// 检测显示器
/// </summary>
private void GetScreenMethod()
{
Screens = Screen.AllScreens;
if (Screens.Length > 0)
{
this.ScreenList.Items.Clear();
foreach (var item in Screens)
{
this.ScreenList.Items.Add(item.DeviceName.Trim());
}
this.ScreenList.SelectedIndex = 0;
WindowIndex = this.ScreenList.SelectedIndex;
}
}
/// <summary>
/// 选择屏幕
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenList_SelectedIndexChanged(object sender, EventArgs e)
{
WindowIndex = this.ScreenList.SelectedIndex;
}
/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Quit_Click(object sender, EventArgs e)
{
if (this.PassWord.Text.Equals("000"))
{
this.Close();
}
else
{
MessageBox.Show("密码错误", "提示");
}
}
/// <summary>
/// 关闭打开的窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Close_Click(object sender, EventArgs e)
{
int index = URLList.SelectedIndex;
FormList[index].Close();
URLList.Items.RemoveAt(index);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox1.Checked)
{
PowerBootMethod();
}
else
{
DeletePowerBootMethod();
}
}
private void URLText_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.URL = this.URLText.Text;
AddNewWebBrowserMethod();
}
}
private void PassWord_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (this.PassWord.Text.Equals("000"))
{
this.Close();
}
else
{
MessageBox.Show("密码错误", "提示");
}
}
}
}
}