C#调用windows API函数声明代码解释

源代码如下,麻烦帮我详细解释一下代码,谢谢了!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CallAPIApp
{
public partial class Form1 : Form
{
#region DllImport
[DllImport("user32.dll")]

public static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
[DllImport("user32.dll", EntryPoint = "MessageBox")]

public static extern int MsgBox(IntPtr hWnd, String text, String caption, uint type);
[DllImport("kernel32.dll")]

private static extern int Beep(int dwFreq, int dwDuration);
#endregion

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
try
{
AnimateWindow(this.Handle, 5000, 0x00040000);
}
catch(Exception ex) {
MsgBox(this.Handle, ex.ToString(), "错误", 0);
}
}

private void textBox2_TextChanged(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
MsgBox(this.Handle, "扬声器发声!", "提示", 0);
try
{
Beep(Int16.Parse(textBox2.Text), Int16.Parse(textBox1.Text));
}
catch (Exception ex)
{
MsgBox(this.Handle, ex.ToString(), "错误", 0);
}
}
}
}

[DllImport("user32.dll")]
public static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
//user32.dll中本来就存在一个名叫AnimateWindow的API调用,在C#中想调用,就把这个函数的声明照原样写出来。(AnimateWindow函数的原型可以上百度百科上查,下同)

[DllImport("user32.dll", EntryPoint = "MessageBox")]
public static extern int MsgBox(IntPtr hWnd, String text, String caption, uint type);
//user32.dll里有一个叫MessageBox的API调用。如果不想像上面那个函数那样照API函数的原样子写的话,是可以改名的,不过必须加一个EntryPoint的参数。

[DllImport("kernel32.dll")]
private static extern int Beep(int dwFreq, int dwDuration);
//kernel32.dll中的Beep函数,作用是发声。
//这三个函数声明好以后就可以直接用了。
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答