Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Yet another centered msgbox

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Yet another centered msgbox

    'I reworked Semen's code into a reusable implementation as a replacement for
    'a standard msgbox. It always centers over it's parent. This was useful when our customers were running two instances
    'of a program and needed to keep any messageboxes associated with it's parent window indstead of popping up over
    'the other instance of the application. This works for 1 or two monitors. Probably used too many globals though.
    'It will return a Yes/No or any other value as determined by the flags.

    Code:
    #COMPILE EXE
    #DIM NONE
    DEFINT A-Z 
    #INCLUDE "WIN32API.INC"
    GLOBAL szWinPos AS ASCIIZ * %MAX_PATH
    GLOBAL FULL_SCREENx AS LONG
    GLOBAL FULL_SCREENy AS LONG
    GLOBAL hHook AS LONG,hdlg AS LONG                  
    %ID_LABEL    = 101
    %ID_TEXT     = 501
    %ID_BUTTON   = 11 
    FUNCTION SBProc(BYVAL lMsg AS LONG, BYVAL wParam AS LONG, _
       BYVAL lParam AS LONG) AS LONG
       IF lMsg = %HCBT_ACTIVATE THEN
          DIM rc1 AS RECT,rc2 AS RECT
          GetWindowRect wParam, rc2
          GetEnvironmentVariable "WINPOS",szWinPos,%MAX_PATH
          C2_WINPOS_Y&  = VAL(PARSE$(szWinPos,"|",1))
          C2_WINPOS_X&  = VAL(PARSE$(szWinPos,"|",2))
          FULL_SCREENx& = VAL(PARSE$(szWinPos,"|",3))
          FULL_SCREENy& = VAL(PARSE$(szWinPos,"|",4))
          CALLINGPRGM$  = PARSE$(szWinPos,"|",5)
          'hdlgiconic =  VAL(PARSE$(szWinPos,"|",6))
          rc1.nTop      = C2_WINPOS_Y&
          rc1.nLeft     = C2_WINPOS_X&
          rc1.nBottom   = C2_WINPOS_Y& + FULL_SCREENy&
          rc1.nRight    = C2_WINPOS_X& + FULL_SCREENx&
          SetWindowPos wParam, %HWND_TOPMOST, (rc1.nLeft + rc1.nRight - rc2.nRight + rc2.nLeft) / 2 , _
             (rc1.nTop + rc1.nBottom - rc2.nBottom + rc2.nTop) / 2, 0, 0, %SWP_NOSIZE OR %SWP_NOACTIVATE
          UnhookWindowsHookEx hHook
       END IF
    END FUNCTION
    FUNCTION MSGBX(MSG AS STRING,FLAGS AS LONG,HDG AS STRING) AS LONG
      hHook = SetWindowsHookEx(%WH_CBT, CODEPTR(SBProc), GetModuleHandle(BYVAL 0&), _
      GetCurrentThreadId)
      FUNCTION = MSGBOX(MSG,FLAGS,HDG)
    END FUNCTION
    CALLBACK FUNCTION hDlgProc
    
        SELECT CASE AS LONG CBMSG
            CASE %WM_INITDIALOG
    
              DIALOG GET LOC CBHNDL TO WINPOS_X&,WINPOS_Y&
              DIALOG UNITS CBHNDL, WINPOS_X&,WINPOS_Y& TO PIXELS C2_WINPOS_X&,C2_WINPOS_Y&
              DIALOG GET SIZE CBHNDL TO WINPOS_X&,WINPOS_Y&
              DIALOG UNITS CBHNDL, WINPOS_X&,WINPOS_Y& TO PIXELS FULL_SCREENx&,FULL_SCREENy&
              EnvironWinPos$ = STR$(C2_WINPOS_Y&) + "|" + STR$(C2_WINPOS_X&) + "|" + STR$(FULL_SCREENx&) + _
              "|" + STR$(FULL_SCREENy&) + "|" + CPROGID$ + "|" + STR$(CBHNDL)
              SetEnvironmentVariable "WINPOS",BYCOPY EnvironWinPos$
                ' Initialization handler
    
            CASE %WM_NCACTIVATE
                STATIC hWndSaveFocus AS DWORD
                IF ISFALSE CBWPARAM THEN
                    ' Save control focus
                    hWndSaveFocus = GetFocus()
                ELSEIF hWndSaveFocus THEN
                    ' Restore control focus
                    SetFocus(hWndSaveFocus)
                    hWndSaveFocus = 0
                END IF
    
            CASE %WM_MOVE
    
              DIALOG GET LOC CBHNDL TO WINPOS_X&,WINPOS_Y&
              DIALOG UNITS CBHNDL, WINPOS_X&,WINPOS_Y& TO PIXELS C2_WINPOS_X&,C2_WINPOS_Y&
              DIALOG GET SIZE CBHNDL TO WINPOS_X&,WINPOS_Y&
              DIALOG UNITS CBHNDL, WINPOS_X&,WINPOS_Y& TO PIXELS FULL_SCREENx&,FULL_SCREENy&
              EnvironWinPos$ = STR$(C2_WINPOS_Y&) + "|" + STR$(C2_WINPOS_X&) + "|" + STR$(FULL_SCREENx&) + _
              "|" + STR$(FULL_SCREENy&) + "|" + CPROGID$ + "|" + STR$(CBHNDL)
              SetEnvironmentVariable "WINPOS",BYCOPY EnvironWinPos$
    
            CASE %WM_COMMAND
                ' Process control notifications
                SELECT CASE AS LONG CBCTL
                    CASE %ID_LABEL
    
                    CASE %ID_TEXT
    
                    CASE %ID_BUTTON
                        IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                            MSGBX "%ID_BUTTON=" + FORMAT$(%ID_BUTTON), _
                                %MB_TASKMODAL,"Centered in parent"
                        END IF
    
                END SELECT
        END SELECT
    END FUNCTION
    FUNCTION PBMAIN
      DIALOG NEW  0, "Another centered msgbox", 145, 81, 176, 107, _
        %WS_POPUP OR %WS_BORDER OR %WS_DLGFRAME OR %WS_SYSMENU OR _
        %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_CLIPSIBLINGS OR _
        %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR _
        %DS_SETFONT, %WS_EX_WINDOWEDGE OR %WS_EX_CONTROLPARENT OR _
        %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
      CONTROL ADD LABEL,   hDlg, %ID_LABEL, "Field", 10, 25, 35, 10
      CONTROL ADD TEXTBOX, hDlg, %ID_TEXT, "", 50, 25, 90, 10
      CONTROL ADD BUTTON,  hDlg, %ID_BUTTON, "Click Me!", 70, 70, 45, 15
      DIALOG SHOW MODAL hDlg, CALL hDlgProc
    END FUNCTION
    Last edited by BOB MECHLER; 1 Jan 2008, 05:29 PM. Reason: More info
Working...
X