[API] 實作 MessageBox

  • 4119
  • 0

[API] 實作 MessageBox

Introduction

製作一個 Console 專案,來測試訊息視窗。

 

Example

引用 “user32.dll” 參考函式原型


int MessageBox(      
    HWND hWnd,
    LPCTSTR lpText,
    LPCTSTR lpCaption,
    UINT uType
);

 

必須打造相對於原型的 C# 版本函式,以及其他相關資料。

 


using System.Runtime.InteropServices;
namespace TestWin32ApiMessageBox {
    class Program {

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern MessageBoxResult MessageBox(IntPtr hWnd, String text, String caption, MessageBoxOptions options);

        /// <summary>
        /// Flags that define appearance and behaviour of a standard message box displayed by a call to the MessageBox function.
        /// </summary>        
        [Flags]
        public enum MessageBoxOptions : uint {
            Ok = 0x000000,
            OkCancel = 0x000001,
            AbortRetryIgnore = 0x000002,
            YesNoCancel = 0x000003,
            YesNo = 0x000004,
            RetryCancel = 0x000005,
            CancelTryContinue = 0x000006,

            IconHand = 0x000010,
            IconQuestion = 0x000020,
            IconExclamation = 0x000030,
            IconAsterisk = 0x000040,
            UserIcon = 0x000080,

            IconWarning = IconExclamation,
            IconError = IconHand,
            IconInformation = IconAsterisk,
            IconStop = IconHand,

            DefButton1 = 0x000000,
            DefButton2 = 0x000100,
            DefButton3 = 0x000200,
            DefButton4 = 0x000300,

            ApplicationModal = 0x000000,
            SystemModal = 0x001000,
            TaskModal = 0x002000,

            Help = 0x004000, //Help Button
            NoFocus = 0x008000,

            SetForeground = 0x010000,
            DefaultDesktopOnly = 0x020000,
            Topmost = 0x040000,
            Right = 0x080000,
            RTLReading = 0x100000,
        }

        /// <summary>
        /// Represents possible values returned by the MessageBox function.
        /// </summary>
        public enum MessageBoxResult : uint {
            Ok = 1,
            Cancel,
            Abort,
            Retry,
            Ignore,
            Yes,
            No,
            Close,
            Help,
            TryAgain,
            Continue,
            Timeout = 32000
        }

        static void Main(string[] args) {
            MessageBox(IntPtr.Zero, "MyText", "MyCaption",
                MessageBoxOptions.IconWarning | MessageBoxOptions.YesNoCancel | MessageBoxOptions.Help);
        }

 

輸出結果

2010-01-10_202709

 

 程式碼下載

TestWin32ApiMessageBox.rar

 

Link

MessageBox Function

http://www.dotblogs.com.tw/atowngit/archive/2009/08/30/10313.aspx

http://www.pinvoke.net/default.aspx/user32/MessageBox.html

三小俠  小弟獻醜,歡迎指教