C#调用windows api示例

这是运行结果:

hovertree

Api函数是构筑Windws应用程序的基石,每一种Windows应用程序开发工具,它提 
供的底层函数都间接或直接地调用了Windows API函数,同时为了实现功能扩 
展,一般也都提供了调用WindowsAPI函数的接口, 也就是说具备调用动态连接 
库的能力。Visual C#和其它开发工具一样也能够调用动态链接库的API函 
数。.NET框架本身提供了这样一种服务,允许受管辖的代码调用动态链接库中实 
现的非受管辖函数,包括操作系统提供的Windows API函数。它能够定位和调用输 
出函数,根据需要,组织其各个参数(整型、字符串类型、数组、和结构等等)跨 
越互操作边界。 

参考:http://hovertree.com/h/bjaf/tc63n4t2.htm

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace HoverTreeWinForm
{
    public partial class FormHewenqi : Form
    {
        /// <summary>
        /// http://hovertree.com/h/bjaf/v4y0b2l6.htm
        /// </summary>
        /// <param name="h"></param>
        /// <param name="m"></param>
        /// <param name="c"></param>
        /// <param name="type"></param>
        /// <returns></returns>

        [DllImport("User32.dll")]
        public static extern int MessageBox(int h, string m, string c, int type);
        public FormHewenqi()
        {
            InitializeComponent();
        }

        private void button_hewenqi_Click(object sender, EventArgs e)
        {
            MessageBox(0, "Hello Win32 API HoverTree", "何问起网", 4);
        }

        private void linkLabel_help_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://hovertree.com/h/bjaf/v4y0b2l6.htm");
        }
    }
}

转载自:http://hovertree.com/h/bjaf/psjdasa2.htm

源码下载:http://pan.baidu.com/s/1bnPPgL5

http://roucheng.cnblogs.com/

上面的示例,弹出信息框窗口并没有指定父窗口句柄,所以弹出后,不用关掉就可以操作主窗体,比如你可以多次点击按钮弹出多个信息框。如果要弹出模式对话框,需先获取当前窗体的句柄,请参考:http://hovertree.com/h/bjaf/8vw6i2yr.htm

模式对话框演示:  http://pan.baidu.com/s/1bnNN2b1 密码: dq8u

源码下载:https://github.com/shangyuxian/HoverTree

原文地址:https://www.cnblogs.com/roucheng/p/csaip.html