Winform窗體的一些設置
設置winform窗體上默認的按鈕為Enter或Esc鍵,只需設置窗體的acceptbutton和cancelbutton兩個屬性。如果是asp.net頁面只需設置form表單的defaultbutton屬性。
在C# windows Form程序中添加托盤可以使用NotifyIcon控件,使程序不顯示在工具欄上可以設置ShowInTaskbar 屬性。
點擊關閉按鈕最小化窗體:
代碼
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
{//捕捉關閉窗體消息
// User clicked close button
this.WindowState = FormWindowState.Minimized;
return;
}
base.WndProc(ref m);
}
定義快捷鍵退出:
代碼
{
int WM_KEYDOWN = 256;
int WM_SYSKEYDOWN = 260;
//if (keyData==Keys.F4)
//{
// this.Close();
//}
if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
{
switch (keyData)
{
case Keys.Escape:
this.Close();//Esc退出
break;
}
}
return false;
}
開機自動運行:
代碼
{
RegistryKey R_local = Registry.LocalMachine;
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
//鍵值不存在
if (R_run.GetValue("123") == null)
{
string R_startPath = Application.ExecutablePath;
R_run.SetValue("123", R_startPath);
}
//存在
else
{
try
{
R_run.DeleteValue("123", false);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
R_run.Close();
R_local.Close();
}
//取得設備硬盤的卷標號
public string GetDiskVolumeSerialNumber()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"d:\"");
disk.Get();
return disk.GetPropertyValue("VolumeSerialNumber").ToString();
}
//獲得CPU的序列號
public string getCpu()
{
string strCpu = null;
ManagementClass myCpu = new ManagementClass("win32_Processor");
ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
foreach (ManagementObject myObject in myCpuConnection)
{
strCpu = myObject.Properties["Processorid"].Value.ToString();
break;
}
return strCpu;
}


浙公網安備 33010602011771號