博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小功能代码二
阅读量:4483 次
发布时间:2019-06-08

本文共 4824 字,大约阅读时间需要 16 分钟。

一、格式化磁盘

 [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(); 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/jiangguanghe/archive/2012/02/20/2360609.html

你可能感兴趣的文章
2017.7.10 C组总结
查看>>
SourceTree下载 及使用
查看>>
MyEclipse下安装FatJar打包工具
查看>>
什么是域名-视频讲解?
查看>>
大道至简第六章-从编程到工程
查看>>
单元测试——隔离神器:mockito
查看>>
[Web Tools] 实用的Web开发工具
查看>>
ContentProvider
查看>>
欢迎来到Attention的博客
查看>>
获取IOS bundle中的文件
查看>>
document
查看>>
Hadoop下大矩阵乘法Version2
查看>>
iPhone内存溢出——黑白苹果
查看>>
Struts2学习笔记(十二) 类型转换(Type Conversion)(下)
查看>>
tcpdump学习
查看>>
局域网内传输文件速度慢
查看>>
Linux的核心版本(摘抄)
查看>>
CASE表达式
查看>>
后缀自动机
查看>>
zkw线段树
查看>>