[API] 實作 MessageBox

60 篇文章 1 订阅

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值