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

190 篇文章 6 订阅

一 弹出消息框和发声

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

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;
            }
        }
    }
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值