Ⅱ. 必須環境
- .NET 5 以上
- C# 9 以上
- Visual Studio 16.8 以上
Ⅲ. やり方
1. 必要なパッケージをインストールする
dotnet add package Microsoft.Windows.CsWin32 --version 0.2.188-beta
2. NativeMethods.txt を新規作成する
NativeMethods.txt
MessageBox
3. サンプルプログラムを書く
using System.Diagnostics; using Windows.Win32; var windowHandle = Process.GetCurrentProcess().MainWindowHandle; var hWnd = (Windows.Win32.Foundation.HWND)windowHandle; var text = "Hello World!"; var caption = "Title"; var messageboxStyle = Windows.Win32.UI.WindowsAndMessaging.MESSAGEBOX_STYLE.MB_OK; PInvoke.MessageBox(hWnd, text, caption, messageboxStyle);
実行結果