小功能代码二

一、格式化磁盘

 [DllImport("shell32.dll")]

        private static extern int SHFormatDrive(IntPtr hWnd, int drive, long fmtID, int Options);

        public const long SHFMT_ID_DEFAULT = 0xFFFF;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //调用API函数SHFormatDrive执行格式化磁盘操作
                SHFormatDrive(this.Handle, comboBox1.SelectedIndex, SHFMT_ID_DEFAULT, 0);
                MessageBox.Show("格式化完成""信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("格式化失败""信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

 

 二、设置打印机:ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");//查找打印设备

                ManagementObjectCollection queryCollection = query.Get();//获取查找到的打印设备
                foreach (ManagementObject mo in queryCollection)//遍历查找到的打印设备
                {
                    if (string.Compare(mo["Name"].ToString(), "PrinterName"true) == 0)//判断默认的打印设备是否为PrinterName
                    {
                        mo.InvokeMethod("SetDefaultPrinter"null);//设置默认打印设备
                        break;
                    }
                }

 

 三、设置桌面背景:string P_str_Path = textBox1.Text;//记录图片路径

            RegistryKey myRKey = Registry.CurrentUser;//获取册注表中的基表
            myRKey = myRKey.OpenSubKey("Control Panel\\Desktop"true);//检索指定的子项
            
//通过调用RegistryKey对象的SetValue方法设置桌面背景
            myRKey.SetValue("WallPaper", P_str_Path);
            myRKey.SetValue("TitleWallPaper""2");
            myRKey.Close();//关闭注册表
            MessageBox.Show("桌面背景已经更改,请重新启动计算机!""信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

 

 四、修改计算机名:[DllImport("kernel32.dll")]

        private static extern int SetComputerName(string ipComputerName);//重写API函数

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            Computer computer = new Computer();//创建计算机对象
            textBox1.Text = computer.Name;//显示计算机名称
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "")//判断计算机名称是否为空
            {
                MessageBox.Show("计算机名称不能为空!");
            }
            else
            {
                SetComputerName(textBox2.Text);//修改计算机名称
                MessageBox.Show("计算机名称修改成功,请重新启动计算机使之生效!");
            }
        }

 

 五、安装键盘钩子: private IntPtr pKeyboardHook = IntPtr.Zero;//键盘钩子句柄

        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);// 钩子委托声明
        
//键盘钩子委托实例不能省略变量
        private HookProc KeyboardHookProcedure;
        //底层键盘钩子
        public const int idHook = 13;
        //安装钩子
        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn,
            IntPtr pInstance, int threadId);
        //卸载钩子
        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(IntPtr pHookHandle);
        //键盘钩子处理函数
        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            KeyMSG m = (KeyMSG)Marshal.PtrToStructure(lParam, typeof(KeyMSG));
            if (pKeyboardHook != IntPtr.Zero)
            {
                switch (((Keys)m.vkCode))
                {
                    case Keys.LWin:
                    case Keys.RWin:
                    case Keys.Delete:
                    case Keys.Alt:
                    case Keys.Escape:
                    case Keys.F4:
                    case Keys.Control:
                    case Keys.Tab:
                        return 1;
                }
            }
            return 0;
        }
        //安装钩子
        public bool InsertHook()
        {
            IntPtr pIn = (IntPtr)4194304;
            if (this.pKeyboardHook == IntPtr.Zero)//如果没安装钩子
            {
                this.KeyboardHookProcedure = new HookProc(KeyboardHookProc);
                //安装钩子
                this.pKeyboardHook = SetWindowsHookEx(idHook, KeyboardHookProcedure, pIn, 0);
                if (this.pKeyboardHook == IntPtr.Zero)//如果安装钩子失败
                {
                    this.UnInsertHook();//卸载钩子
                    return false;//返回false
                }
            }
            return true;
        }
        //卸载钩子
        public bool UnInsertHook()
        {
            bool result = true;
            if (this.pKeyboardHook != IntPtr.Zero)//如果存在键盘钩子
            {
                result = (UnhookWindowsHookEx(this.pKeyboardHook) && result);//卸载钩子
                this.pKeyboardHook = IntPtr.Zero;
            }
            return result;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct KeyMSG
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

 

六、获取内存使用情况: Microsoft.VisualBasic.Devices.Computer myInfo = new Microsoft.VisualBasic.Devices.Computer();

            //获取物理内存总量
            pbMemorySum.Maximum = Convert.ToInt32(myInfo.Info.TotalPhysicalMemory/1024/1024);
            pbMemorySum.Value = Convert.ToInt32(myInfo.Info.TotalPhysicalMemory / 1024 / 1024);
            lblSum.Text = (myInfo.Info.TotalPhysicalMemory / 1024).ToString();
            //获取可用物理内存总量
            pbMemoryUse.Maximum = Convert.ToInt32(myInfo.Info.TotalPhysicalMemory / 1024 / 1024);
            pbMemoryUse.Value = Convert.ToInt32(myInfo.Info.AvailablePhysicalMemory / 1024 / 1024);
            lblMuse.Text = (myInfo.Info.AvailablePhysicalMemory / 1024).ToString();
            //获取虚拟内存总量
            pbVmemorysum.Maximum = Convert.ToInt32(myInfo.Info.TotalVirtualMemory / 1024 / 1024);
            pbVmemorysum.Value = Convert.ToInt32(myInfo.Info.TotalVirtualMemory / 1024 / 1024);
            lblVinfo.Text = (myInfo.Info.TotalVirtualMemory / 1024).ToString();
            //获取可用虚拟内存总量
            pbVmemoryuse.Maximum = Convert.ToInt32(myInfo.Info.TotalVirtualMemory / 1024 / 1024);
            pbVmemoryuse.Value = Convert.ToInt32(myInfo.Info.AvailableVirtualMemory/ 1024 / 1024);

            lblVuse.Text = (myInfo.Info.AvailableVirtualMemory / 1024).ToString(); 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2012-02-20 22:35  独臂侠  阅读(264)  评论(0编辑  收藏  举报