您的位置:首页 > 编程语言 > C#

图解C# 调用Win32 API 示例程序

2016-04-21 22:18 525 查看

一 弹出消息框和发声

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

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,若未知则为255
public byte reserved1;
public int batteryLifeTime;//秒为单位的电池剩余电量, 若未知则为-1
public 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 MessageBox