Winform窗体的一些设置

设置winform窗体上默认的按钮为Enter或Esc键,只需设置窗体的acceptbutton和cancelbutton两个属性。如果是asp.net页面只需设置form表单的defaultbutton属性。

 

在C# windows Form程序中添加托盘可以使用NotifyIcon控件,使程序不显示在工具栏上可以设置ShowInTaskbar 属性。

 

点击关闭按钮最小化窗体:

 

代码
protected   override   void  WndProc( ref    Message m)   
{   
    
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);   
 

定义快捷键退出:

 

代码
         protected   override   bool  ProcessCmdKey( ref  System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            
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 ;
        }

 

 

开机自动运行:

       

代码
private   void  AutoRun()
        {
            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;
}

你可能感兴趣的:(WinForm)