一 弹出消息框和发声

先上代码;相关函数不解释;网上比较容易查到;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace win32demo1
{public partial class Form1 : Form{[DllImport("User32.dll")]public static extern int MessageBox(int h, string m, string c, int type);[DllImport("kernel32.dll")]public static extern bool Beep(int frequency, int duration);public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){MessageBox(0, "Hello Win32 API", "C#", comboBox1.SelectedIndex);}private void button2_Click(object sender, EventArgs e){Random random = new Random();for (int i = 0; i < 10000; i++){Beep(random.Next(10000), 100);}}private void Form1_Load(object sender, EventArgs e){comboBox1.Items.Add("确定按钮");comboBox1.Items.Add("确定、取消按钮");comboBox1.Items.Add("终止、重试、忽略按钮");comboBox1.Items.Add("是、否、取消按钮");comboBox1.Items.Add("是、否按钮");comboBox1.Items.Add("重试取消钮");comboBox1.Items.Add("终止、重试、继续");}}
}

可以选择弹出不同类别的消息框;如下图;

另上面发声的代码,重复1万,能响一段时间了;

当调用非托管API函数时,它将依次执行以下操作: 
1.查找包含该函数的 DLL。
2.将该 DLL 加载到内存中。
3.查找函数在内存中的地址并将其参数推到堆栈上,以封送所需的数据(注意:只在第一次调用函数时,才会查找和加载 DLL 并查找函数在内存中的地址。)。
4.将控制权转移给非托管函数。
5.对非托管 DLL 函数的“平台调用”调用
平台调用会向托管调用方引发由非托管函数生成的异常。

二 系统电源状态

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace Power1
{public partial class Form1 : Form{public struct SystemPowerStatus{public byte ACLineStatus; //交流电源状态public byte batteryFlag; //电池充电状态public byte batteryLifePercent;//电池还有百分之几能充满.0~100,若未知则为255public byte reserved1;public int batteryLifeTime;//秒为单位的电池剩余电量, 若未知则为-1public int batteryFullLifeTime;//秒为单位的电池充满电的电量,若未知则为-1}enum ACLineStatus : byte{Offline = 0,Online = 1,Unknown = 255,}enum BatteryFlag : byte{High = 1,//高,电量大于66%Low = 2,//低,小于33%Critical = 4,//极低,小于5%Charging = 8,//充电中NoSystemBattery = 128,//没有电池Unknown = 255,}[DllImport("kernel32.dll")]public static extern bool GetSystemPowerStatus(ref SystemPowerStatus systemPowerStatus);public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){SystemPowerStatus sps = new SystemPowerStatus();//SystemPowerStatus sps;GetSystemPowerStatus(ref sps);textBox1.Text = "交流电源状态:" + getACLineStr(sps.ACLineStatus);textBox1.Text = textBox1.Text + Environment.NewLine + "电池充电状态:" + getBatteryFlag(sps.batteryFlag);textBox1.Text = textBox1.Text + Environment.NewLine + "电量百分比:" + sps.batteryLifePercent.ToString();textBox1.Text = textBox1.Text + Environment.NewLine + "秒为单位的电池剩余电量:"+sps.batteryLifeTime.ToString();textBox1.Text = textBox1.Text + Environment.NewLine + "秒为单位的电池充满电的电量:"+sps.batteryFullLifeTime.ToString();}private void Form1_Load(object sender, EventArgs e){}private string getACLineStr(int n){switch (n){case 0:return "离线";break;case 1:return "在线";break;case 255:return "未知";break;default:return "未知";break;}}private string getBatteryFlag(int n){switch (n){case 1:return "高,电量大于66%";break;case 2:return "低,小于33%";break;case 4:return "极低,小于5%";break;case 8:return "充电中";break;case 128:return "没有电池";break;case 255:return "未知";break;default:return "未知";break;}}}
}

图解C# 调用Win32 API 示例程序相关推荐

  1. 控制台调用win32 API 示例二则

    一 控制台显示消息框 #include <stdio.h> #include <windows.h>int main(int argc, char* argv[]) {int ...

  2. java 调用win32 api 学习总结

    java使用JInvoke调用windows API 使用jinvoke调用windowsAPI.jna使用比较麻烦,需要写c代码和参数转换,jinvoke的使用就像jdk中的包一样. 官网使用参考: ...

  3. C#调用Win32 api学习总结

    转载:https://blog.csdn.net/bcbobo21cn/article/details/50930221 从.NET平台调用Win32 API Win32 API可以直接控制Micro ...

  4. MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API(转)...

    转自:http://www.cnblogs.com/Yahong111/archive/2007/08/16/857574.html 续上文[翻译]MSIL 教程(一) ,本文继续讲解数组.分支.循环 ...

  5. C# 获取笔记本电池信息 调用 Win32 Api

    C# 获取笔记本电池信息 获取电池信息需要调用Win32 Api 相关函数为 GetSystemPowerStatus 函数结构: BOOL GetSystemPowerStatus(LPSYSTEM ...

  6. c#调用win32 API函数修改系统时间

    一般来说,系统时间的修改可以通过win32 API函数库中的SetLocalTime函数进行设置.对于C#语言来说,虽然win32 API大部分函数都已经封装在了.NET Framework类库中,但 ...

  7. C++调用win32 API操作打印机实现驱动打印

    方法说明: 1.s2w(string &content, int length):将string类型的变量转换为wchar_t*类型,用于调用TextOut()方法时传参,其中length为需 ...

  8. php调用win32 api,C#_c#使用win32api实现获取光标位置,方法一:需要调用win32api,winfo - phpStudy...

    c#使用win32api实现获取光标位置 方法一:需要调用win32api,winform.wpf通用 [DllImport("user32.dll")] public stati ...

  9. Python发送微信消息(文字、图片、文件)给指定好友和微信群(调用Win32 API模拟人的手动操作来发送消息)

    本示例是调用Windows API模拟发送,用Python调用win32api这个库来调用Windows API模拟人的手动操作来发送消息. 在使用前,请将你微信的窗口设置为在最前面,这样就便于程序找 ...

最新文章

  1. 干货丨数据科学、机器学习、人工智能,究竟有什么区别?
  2. python最高版本-python最新版
  3. 轻轻松松明白什么是反射,反射有什么用,简单上手反射以及反射的优缺点
  4. 常用器件选型——电源篇
  5. 2020蓝桥杯省赛---java---B---1(指数计算)
  6. PP视频如何将默认缓存清晰度设置成超清
  7. 请写出至少五个块级元素_2020高考元素化合物命题特点及复习思路
  8. 什么是补码-网上找到的,非原创
  9. 学生社团管理系统PHP源码,学生社团管理系统 附带源码
  10. VB--Adodc控件
  11. 强烈推荐这个Java学习文档——不看后悔系列
  12. __attribute__ 关键字小结
  13. 瞬态抑制二极管型号参数对照表,想要的都在这里
  14. Android常见面试题字节跳动、阿里、腾讯2019实习生Android岗部分面试题
  15. 搜索引擎优化系统知名乐云seo_北京网络优化知名乐云seo
  16. 插入法排序c语言程序,C语言直接插入排序算法
  17. qq空间播放器肤代码
  18. 《牧羊少年奇幻之旅》读书笔记
  19. HCNA-Telnet
  20. 企业通信基础设施行业调研报告 - 市场现状分析与发展前景预测

热门文章

  1. 包装类 || 装箱与拆箱
  2. MATLAB知识点2
  3. CTF 大小写字母转换 try lower and upper
  4. jquery遍历函数siblings()
  5. jquery中eq和get
  6. CTFshow php特性 web128
  7. CTFshow 文件包含 web87
  8. CTFshow 命令执行 web40
  9. Maximum Product Subarray
  10. 微型计算机原理答案第四章,微机原理第四章习题答案.doc