2021年12月3日 星期五

Messagebox delay in Delphi MessageBoxTimeOut function

 Messagebox delay in Delphi MessageBoxTimeOut function
 
#include <windows.h>
#include <tchar.h>

//Functions & other definitions required-->
typedef int (__stdcall *MSGBOXAAPI)(IN HWND hWnd,
        IN LPCSTR lpText, IN LPCSTR lpCaption,
        IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);
typedef int (__stdcall *MSGBOXWAPI)(IN HWND hWnd,
        IN LPCWSTR lpText, IN LPCWSTR lpCaption,
        IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);

int MessageBoxTimeoutA(IN HWND hWnd, IN LPCSTR lpText,
    IN LPCSTR lpCaption, IN UINT uType,
    IN WORD wLanguageId, IN DWORD dwMilliseconds);
int MessageBoxTimeoutW(IN HWND hWnd, IN LPCWSTR lpText,
    IN LPCWSTR lpCaption, IN UINT uType,
    IN WORD wLanguageId, IN DWORD dwMilliseconds);

#define MB_TIMEDOUT 32000

    static MSGBOXAAPI MsgBoxTOA = NULL;

    if (!MsgBoxTOA)
    {
        HMODULE hUser32 = GetModuleHandle(_T("user32.dll"));
        if (hUser32)
        {
            MsgBoxTOA = (MSGBOXAAPI)GetProcAddress(hUser32,
                                      "MessageBoxTimeoutA");
        HMODULE hUser32 = GetModuleHandle(_T("user32.dll"));
        if (hUser32)
        {
            MsgBoxTOW = (MSGBOXWAPI)GetProcAddress(hUser32,
                                      "MessageBoxTimeoutW");
HMODULE hUser32 = LoadLibrary(_T("user32.dll"));
 
    UINT uiFlags = MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
    
    iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 2 seconds."),
                      _T("MessageBoxTimeout Test"), uiFlags, 0, 2000);
    //iRet will = 1

    uiFlags = MB_YESNO|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;

    iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 5 seconds."),
                      _T("MessageBoxTimeout Test"), uiFlags, 0, 5000);
    //iRet will = MB_TIMEDOUT if no buttons pressed, button values otherwise
        
    //only unload user32.dll when you have no further need
    //for the MessageBoxTimeout function
    FreeLibrary(hUser32);
https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.debugger.messageboxflags?view=visualstudiosdk-2022

MB_OK
MB_OKCANCEL
MB_ABORTRETRYIGNORE
MB_YESNOCANCEL
MB_YESNO
MB_RETRYCANCEL

MB_APPLMODAL
MB_SYSTEMMODAL

        MB_SIMPLEBEEP (Only when Title is not used)
        MB_HELP (Probably won't work)
        MB_DEFAULT_DESKTOP_ONLY
        MB_RIGHT
        MB_RTLREADING
        MB_SETFOREGROUND
        MB_TOPMOST
        MB_SERVICE_NOTIFICATION
    https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-messagebox
    https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-messagebeep

MB_TASKMODAL

------------------------------
function MessageBoxTimeOut(hWnd: HWND; lpText: PChar; lpCaption: PChar;
                           uType: UINT; wLanguageId: WORD; dwMilliseconds: DWORD): Integer; stdcall;

function MessageBoxTimeOutA(hWnd: HWND; lpText: PChar; lpCaption: PChar;
                            uType: UINT; wLanguageId: WORD; dwMilliseconds: DWORD): Integer; stdcall;


function MessageBoxTimeOutW(hWnd: HWND; lpText: PWideChar; lpCaption: PWideChar;
                            uType: UINT; wLanguageId: WORD; dwMilliseconds: DWORD): Integer; stdcall;

  Windows.pas
const
  MB_TIMEDOUT = 32000;

function MessageBoxTimeOut; externaluser32 name 'MessageBoxTimeoutA';
function MessageBoxTimeOutA; external user32 name 'MessageBoxTimeoutA';
function MessageBoxTimeOutW; external user32 name 'MessageBoxTimeoutW';
  // Define a MessagBox  with an OK button and a timeout of 2 seconds
  iFlags  := MB_OK or MB_SETFOREGROUND or MB_SYSTEMMODAL or MB_ICONINFORMATION;
  iResult := MessageBoxTimeout(Application.Handle, 'Test a timeout of 2 seconds.',
                             'MessageBoxTimeout Test', iFlags, 0, 2000);
  // iResult will = 1 (IDOK)
  ShowMessage(IntToStr(iRet));

  // Define a MessageBox with a Yes and No button and a timeout of 5 seconds
  iFlags  := MB_YESNO or MB_SETFOREGROUND or MB_SYSTEMMODAL or MB_ICONINFORMATION;
  iResult := MessageBoxTimeout(Application.Handle, 'Test a timeout of 5 seconds.',
                              'MessageBoxTimeout Test', iFlags, 0, 5000);
  // iResult = MB_TIMEDOUT if no buttons clicked, otherwise
  // iResult will return the value of the button clicked
  case iResult of
  IDYES:  // Pressed Yes button
    ShowMessage('Yes');
  IDNO:  // Pressed the No button
    ShowMessage('No');
  MB_TIMEDOUT: // MessageBox timed out
    ShowMessage('TimedOut'); 


https://edn.embarcadero.com/print/32736

 Dialogs createMessageDialog createOptionDialog

Vcl.Dialogs.CreateMessageDialog

https://github.com/Leandros/WindowsHModular/blob/master/include/win32/window.h 


https://www.codeproject.com/Articles/18399/Localizing-System-MessageBox

沒有留言: