3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MessageBoxTimeoutを簡単に使う

Posted at

はじめに

使う機会は少ないが、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);
        }
    }
}
3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?