test

test

2010年3月31日 星期三

MessageBox-使用訊息對話方塊

●The MessageBox function creates, displays, and operates a message box. The message box contains an application-defined message and title, plus any combination of predefined icons and push buttons.

●VB6建議宣告法: Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
VB2008宣告法: Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Integer, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Integer) As Integer

●用法
VB6:
傳回值 = MessageBox(擁有者的handle, "要顯示的文字", "視窗抬頭", 視窗種類)
Private Sub COmmand1_Click()
Dim Rtvl As Ling
Rtvl = MessageBox(Me.hWnd, "你好嗎?", "問候", 32 OR 4)
End Sub


VB2008:
Public Class Form1

Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hWnd As Integer, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Integer) As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rtl As Integer = MessageBox(Me.Handle, "開始學習Windows編程", "訊息對話框", 32 Or 4)
MsgBox(rtl)
End Sub
End Class


VC++:
#include <windows.h>

#pragma comment (lib, "User32.lib")

int WinMain(
HINSTANCE hInstance,
HINSTANCE hPreInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
MessageBox(NULL,
TEXT("開始學習Windows編程"),
TEXT("訊息對話框"),
MB_OK);
return 0;
}


視窗種類 = 按鈕種類 OR 圖示種類

按鈕種類
1 確定、取消
2 異常終止、重試、略過
3 是、否、取消
4 是、否
5 重試、取消
6 說明(無法關閉)
7 無按鈕(無法關閉)
8 取消、說明
9 無按鈕(無法關閉)
10 確定、取消、說明
11 無按鈕(無法關閉)
12 說明、確定、取消、說明
13 無按鈕(無法關閉)
14 確定、取消、說明、異常終止、取消

圖示種類
0 第一個按鈕為預設
16 叉叉
32 問號
48 驚嘆號
64 資訊
256 第二個按鈕為預設
512 第三個按鈕為預設

傳回值代表按下那個按鈕
1 確定
2 取消
3 中斷
4 重試
5 忽略
6 是
7 否

●VB也提供了MsgBox函數,兩者執行來最大的差別是,一樣都是視窗動結,然而MsgBox會使得整個程式停止,等待使用者結束MessageBox的使用,而MessageBox函數則只會凍結視窗而不會終止程式的執行。

MSDN:
MessageBox Function

--------------------------------------------------------------------------------

The MessageBox function creates, displays, and operates a message box. The message box contains an application-defined message and title, along with any combination of predefined icons and push buttons.


Syntax


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


hWnd
[in] Handle to the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window.
lpText
[in] Pointer to a null-terminated string that contains the message to be displayed.
lpCaption
[in] Pointer to a null-terminated string that contains the dialog box title. If this parameter is NULL, the default title Error is used.
uType
[in] Specifies the contents and behavior of the dialog box. This parameter can be a combination of flags from the following groups of flags.


To indicate the buttons displayed in the message box, specify one of the following values.


MB_ABORTRETRYIGNORE (0x00000002L)
The message box contains three push buttons: Abort, Retry, and Ignore.
MB_CANCELTRYCONTINUE (0x00000006L)
Microsoft Windows 2000/XP: The message box contains three push buttons: Cancel, Try Again, Continue. Use this message box type instead of MB_ABORTRETRYIGNORE.
MB_HELP (0x00004000L)
Windows 95/98/Me, Windows NT 4.0 and later: Adds a Help button to the message box. When the user clicks the Help button or presses F1, the system sends a WM_HELP message to the owner.
MB_OK (0x00000000L)
The message box contains one push button: OK. This is the default.
MB_OKCANCEL (0x00000001L)
The message box contains two push buttons: OK and Cancel.
MB_RETRYCANCEL (0x00000005L)
The message box contains two push buttons: Retry and Cancel.
MB_YESNO (0x00000004L)
The message box contains two push buttons: Yes and No.
MB_YESNOCANCEL (0x00000003L)
The message box contains three push buttons: Yes, No, and Cancel.
To display an icon in the message box, specify one of the following values.
MB_ICONEXCLAMATION (0x00000030L)
An exclamation-point icon appears in the message box.
MB_ICONWARNING (MB_ICONEXCLAMATION)
An exclamation-point icon appears in the message box.
MB_ICONINFORMATION (MB_ICONASTERISK)
An icon consisting of a lowercase letter i in a circle appears in the message box.
MB_ICONASTERISK (0x00000040L)
An icon consisting of a lowercase letter i in a circle appears in the message box.
MB_ICONQUESTION (0x00000020L)
A question-mark icon appears in the message box. The question-mark message icon is no longer recommended because it does not clearly represent a specific type of message and because the phrasing of a message as a question could apply to any message type. In addition, users can confuse the message symbol question mark with Help information. Therefore, do not use this question mark message symbol in your message boxes. The system continues to support its inclusion only for backward compatibility.
MB_ICONSTOP (MB_ICONHAND)
A stop-sign icon appears in the message box.
MB_ICONERROR (MB_ICONHAND)
A stop-sign icon appears in the message box.
MB_ICONHAND (0x00000010L)
A stop-sign icon appears in the message box.
To indicate the default button, specify one of the following values.
MB_DEFBUTTON1 (0x00000000L)
The first button is the default button.
MB_DEFBUTTON1 is the default unless MB_DEFBUTTON2, MB_DEFBUTTON3, or MB_DEFBUTTON4 is specified.

MB_DEFBUTTON2 (0x00000100L)
The second button is the default button.
MB_DEFBUTTON3 (0x00000200L)
The third button is the default button.
MB_DEFBUTTON4 (0x00000300L)
The fourth button is the default button.
To indicate the modality of the dialog box, specify one of the following values.
MB_APPLMODAL (0x00000000L)
The user must respond to the message box before continuing work in the window identified by the hWnd parameter. However, the user can move to the windows of other threads and work in those windows.
Depending on the hierarchy of windows in the application, the user may be able to move to other windows within the thread. All child windows of the parent of the message box are automatically disabled, but pop-up windows are not.

MB_APPLMODAL is the default if neither MB_SYSTEMMODAL nor MB_TASKMODAL is specified.

MB_SYSTEMMODAL (0x00001000L)
Same as MB_APPLMODAL except that the message box has the WS_EX_TOPMOST style. Use system-modal message boxes to notify the user of serious, potentially damaging errors that require immediate attention (for example, running out of memory). This flag has no effect on the user's ability to interact with windows other than those associated with hWnd.
MB_TASKMODAL (0x00002000L)
Same as MB_APPLMODAL except that all the top-level windows belonging to the current thread are disabled if the hWnd parameter is NULL. Use this flag when the calling application or library does not have a window handle available but still needs to prevent input to other windows in the calling thread without suspending other threads.
To specify other options, use one or more of the following values.
MB_DEFAULT_DESKTOP_ONLY (0x00020000L)
Windows NT/2000/XP: Same as desktop of the interactive window station. For more information, see Window Stations.
Windows NT 4.0 and earlier: If the current input desktop is not the default desktop, MessageBox fails.


Windows 2000/XP: If the current input desktop is not the default desktop, MessageBox does not return until the user switches to the default desktop.


Windows 95/98/Me: This flag has no effect.


MB_RIGHT (0x00080000L)
The text is right-justified.
MB_RTLREADING (0x00100000L)
Displays message and caption text using right-to-left reading order on Hebrew and Arabic systems.
MB_SETFOREGROUND (0x00010000L)
The message box becomes the foreground window. Internally, the system calls the SetForegroundWindow function for the message box.
MB_TOPMOST (0x00040000L)
The message box is created with the WS_EX_TOPMOST window style.
MB_SERVICE_NOTIFICATION (0x00200000L)
Windows NT/2000/XP: The caller is a service notifying the user of an event. The function displays a message box on the current active desktop, even if there is no user logged on to the computer.
Terminal Services: If the calling thread has an impersonation token, the function directs the message box to the session specified in the impersonation token.


If this flag is set, the hWnd parameter must be NULL. This is so that the message box can appear on a desktop other than the desktop corresponding to the hWnd.

For more information on the changes between Microsoft Windows NT 3.51 and Windows NT 4.0, see Remarks.

For information on security considerations in regard to using this flag, see Interactive Services.


MB_SERVICE_NOTIFICATION_NT3X (0x00040000L)
Windows NT/2000/XP: This value corresponds to the value defined for MB_SERVICE_NOTIFICATION for Windows NT version 3.51.
For more information on the changes between Windows NT 3.51 and Windows NT 4.0, see Remarks.

Return Value


If a message box has a Cancel button, the function returns the IDCANCEL value if either the ESC key is pressed or the Cancel button is selected. If the message box has no Cancel button, pressing ESC has no effect.


If the function fails, the return value is zero. To get extended error information, call GetLastError.


If the function succeeds, the return value is one of the following menu-item values.


IDABORT (3) Abort button was selected.
IDCANCEL (2) Cancel button was selected.
IDCONTINUE (11) Continue button was selected.
IDIGNORE (5) Ignore button was selected.
IDNO (7) No button was selected.
IDOK (1) OK button was selected.
IDRETRY (4) Retry button was selected.
IDTRYAGAIN (10) Try Again button was selected.
IDYES (6) Yes button was selected.



Remarks

Adding two right-to-left marks (RLMs), represented by Unicode formatting character U+200F, in the beginning of a MessageBox display string is interpreted by the Win32 MessageBox rendering engine so as to cause the reading order of the MessageBox to be rendered as right-to-left (RTL).

When you use a system-modal message box to indicate that the system is low on memory, the strings pointed to by the lpText and lpCaption parameters should not be taken from a resource file because an attempt to load the resource may fail.

If you create a message box while a dialog box is present, use a handle to the dialog box as the hWnd parameter. The hWnd parameter should not identify a child window, such as a control in a dialog box.

Windows 95/98/Me: The system can support a maximum of 16,364 window handles.

Windows NT/2000/XP: The value of MB_SERVICE_NOTIFICATION changed starting with Windows NT 4.0. Windows NT 4.0 provides backward compatibility for preexisting services by mapping the old value to the new value in the implementation of MessageBox. This mapping is done only for executables that have a version number earlier than 4.0, as set by the linker.

To build a service that uses MB_SERVICE_NOTIFICATION and can run on both Microsoft Windows NT 3.x and Windows NT 4.0, you can do one of the following.


At link-time, specify a version number less than 4.0.
At link-time, specify version 4.0. At run-time, use the GetVersionEx function to check the system version. Then, when running on Windows NT 3.x, use MB_SERVICE_NOTIFICATION_NT3X; and on Windows NT 4.0, use MB_SERVICE_NOTIFICATION.

Windows 95/98/Me: Even though MessageBoxW exists, it is supported by the Microsoft Layer for Unicode on Windows 95/98/Me Systems to give more consistent behavior across all Windows operating systems.


Example

For an example, see Displaying a Message Box.

Function Information

Minimum DLL Version user32.dll
Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
Unicode Implemented as ANSI and Unicode versions.



WinUser.h中之宣告:

/*
* MessageBox() Flags
*/
#define MB_OK 0x00000000L
#define MB_OKCANCEL 0x00000001L
#define MB_ABORTRETRYIGNORE 0x00000002L
#define MB_YESNOCANCEL 0x00000003L
#define MB_YESNO 0x00000004L
#define MB_RETRYCANCEL 0x00000005L
#if(WINVER >= 0x0500)
#define MB_CANCELTRYCONTINUE 0x00000006L
#endif /* WINVER >= 0x0500 */


#define MB_ICONHAND 0x00000010L
#define MB_ICONQUESTION 0x00000020L
#define MB_ICONEXCLAMATION 0x00000030L
#define MB_ICONASTERISK 0x00000040L

#if(WINVER >= 0x0400)
#define MB_USERICON 0x00000080L
#define MB_ICONWARNING MB_ICONEXCLAMATION
#define MB_ICONERROR MB_ICONHAND
#endif /* WINVER >= 0x0400 */

#define MB_ICONINFORMATION MB_ICONASTERISK
#define MB_ICONSTOP MB_ICONHAND

#define MB_DEFBUTTON1 0x00000000L
#define MB_DEFBUTTON2 0x00000100L
#define MB_DEFBUTTON3 0x00000200L
#if(WINVER >= 0x0400)
#define MB_DEFBUTTON4 0x00000300L
#endif /* WINVER >= 0x0400 */

#define MB_APPLMODAL 0x00000000L
#define MB_SYSTEMMODAL 0x00001000L
#define MB_TASKMODAL 0x00002000L
#if(WINVER >= 0x0400)
#define MB_HELP 0x00004000L // Help Button
#endif /* WINVER >= 0x0400 */

#define MB_NOFOCUS 0x00008000L
#define MB_SETFOREGROUND 0x00010000L
#define MB_DEFAULT_DESKTOP_ONLY 0x00020000L

#if(WINVER >= 0x0400)
#define MB_TOPMOST 0x00040000L
#define MB_RIGHT 0x00080000L
#define MB_RTLREADING 0x00100000L


#endif /* WINVER >= 0x0400 */

#ifdef _WIN32_WINNT
#if (_WIN32_WINNT >= 0x0400)
#define MB_SERVICE_NOTIFICATION 0x00200000L
#else
#define MB_SERVICE_NOTIFICATION 0x00040000L
#endif
#define MB_SERVICE_NOTIFICATION_NT3X 0x00040000L
#endif

#define MB_TYPEMASK 0x0000000FL
#define MB_ICONMASK 0x000000F0L
#define MB_DEFMASK 0x00000F00L
#define MB_MODEMASK 0x00003000L
#define MB_MISCMASK 0x0000C000L


/*
* Dialog Box Command IDs
*/
#define IDOK 1
#define IDCANCEL 2
#define IDABORT 3
#define IDRETRY 4
#define IDIGNORE 5
#define IDYES 6
#define IDNO 7
#if(WINVER >= 0x0400)
#define IDCLOSE 8
#define IDHELP 9
#endif /* WINVER >= 0x0400 */

#if(WINVER >= 0x0500)
#define IDTRYAGAIN 10
#define IDCONTINUE 11
#endif /* WINVER >= 0x0500 */

#if(WINVER >= 0x0501)
#ifndef IDTIMEOUT
#define IDTIMEOUT 32000
#endif
#endif /* WINVER >= 0x0501 */

沒有留言: