1. 主页 > 世界杯2021 >

【C#】使用user32.dll的MessageBox弹窗消息

要使用user32.dll的MessageBox弹窗消息,自然需要引入user32.dll到项目中。

一个最简单的实例如下:

using System;

using System.Runtime.InteropServices;

class Example

{

// Use DllImport to import the Win32 MessageBox function.

[DllImport("user32.dll", CharSet = CharSet.Unicode)]

public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

static void Main()

{

// Call the MessageBox function using platform invoke.

MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);

}

}

运行项目后,直接弹出弹窗消息。

这是C#使用外部DLL中的方法,项目的引用中并没有显示该user32.dll。