はじめに
使う機会は少ないが、Win32API「MessageBoxTimeout」の使用例を残す
※試しにMessageBoxTimeoutに関する記事が無いか検索したが、
1件のみで寂しい気持ちになったから書いたのもある。探し方が悪かったのだろうか?
参考元
ExcelVBA
C#及びVB.NET
VB.NET側
定義
MsgTimeOut.vb
Imports System.Runtime.InteropServices
Public Class MsgTimeOut
<DllImport("user32.dll", EntryPoint:="MessageBoxTimeoutW", SetLastError:=True, CharSet:=CharSet.Unicode)>
Private Shared Function MessageBoxTimeOut _
(ByVal hWnd As IntPtr, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal uType As UInteger, _
ByVal wLanguage As Int16, _
ByVal dwMilliseconds As Int32) As UInteger
End Function
''' <summary>簡易式</summary>
Public Shared Function Show(ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal uType As UInteger, _
ByVal dwMilliseconds As Int32) As UInteger
Return MessageBoxTimeOut(CType(0, IntPtr), lpText, lpCaption, uType, 0, dwMilliseconds)
End Function
''' <summary>全指定</summary>
Public Shared Function Show(ByVal hWnd As IntPtr, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal uType As UInteger, _
ByVal wLanguage As Int16, _
ByVal dwMilliseconds As Int32) As UInteger
Return MessageBoxTimeOut(hWnd, lpText, lpCaption, uType, wLanguage, dwMilliseconds)
End Function
End Class
VB.NET使用例
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' 簡易式
MsgTimeOut.Show("簡易式", "テストタイトル", MessageBoxButtons.OK, 5000)
' フル設定
MsgTimeOut.Show(CType(0, IntPtr), _
"フル設定", _
"テストタイトル", _
MessageBoxButtons.OK, _
0, _
1000)
End Sub
End Class
C#側
定義
MsgTimeOut.cs
using System.Runtime.InteropServices;
namespace WinFormsApp
{
public class MsgBoxTimeout
{
#pragma warning disable SA1121
[DllImport("user32.dll", SetLastError=true)]
private static extern int MessageBoxTimeout(IntPtr hWnd,
string text,
string title,
uint type,
Int16 wLanguageId,
Int32 milliseconds);
/// <summary>簡易版</summary>
public void Show(string text, string title, uint type, Int32 milliseconds)
=> MessageBoxTimeout((IntPtr)0, text, title, type, 0, milliseconds);
/// <summary>全部指定</summary>
public void Show(IntPtr hWnd,
string text,
string title,
uint type,
Int16 wLanguageId,
Int32 milliseconds)
=> MessageBoxTimeout(hWnd, text, title, type, wLanguageId, milliseconds);
#pragma warning restore SA1121
}
}
C#使用例
namespace WinFormsApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MsgBoxTimeout cMBT = new MsgBoxTimeout();
// 簡易式
cMBT.Show("簡易式", "テストタイトル", (uint)MessageBoxButtons.OK, 5000);
// フル設定
cMBT.Show((IntPtr)0, "フル設定", "テストタイトル", (uint)MessageBoxButtons.OK, 0, 1000);
}
}
}