CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: msgbox position

Hybrid View

  1. #1
    Join Date
    Nov 2000
    Location
    Tokyo and Memphis
    Posts
    238

    msgbox position

    my msgbox comes up over the spot that I want the user to see. How can I do something like

    msgbox.top = 100
    msgbox.left = 200

    Can that be done?

    Cheers all

    Phil



  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: msgbox position

    not sure whether there is a built-in option for messagebox. but you can just create a new form without any titlebar that emulate a message box. then you can set the form top & left just like normal. when u r ready, just call the form method show.

    of course, if you still want to use msgbox, there is a way, what you need to do is after you call the msgbox, there must be a extra handle in the system, use the FindWindow API to get the window handle for the newly created message box. afterward, call SetWindowPos to set the position of the message box.


    HTH

    cksiow
    http://vblib.virtualave.net - share our codes


  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: msgbox position

    There is the code to position and/or close msgbox
    Code:
    'module
      ' the max length of a path for the system (usually 260 or there abouts)
      ' this is used to size the buffer string for retrieving the class name of the active window below
      public Const MAX_PATH as Long = 260&
    
      public Const API_FALSE as Long = 0&
       
      ' made up constants for setting our timer
      public Const NV_CLOSEMSGBOX as Long = &H5000&
      public Const NV_MOVEMSGBOX as Long = &H5001&
    
      ' MessageBox() Flags
      public Const MB_ICONQUESTION as Long = &H20&
      public Const MB_TASKMODAL as Long = &H2000&
    
      ' SetWindowPos Flags
      public Const SWP_NOSIZE as Long = &H1&
      public Const SWP_NOZORDER as Long = &H4&
      public Const HWND_TOP as Long = 0&
    
      Type RECT
        Left as Long
        Top as Long
        Right as Long
        Bottom as Long
      End Type
    
      ' API declares
      public Declare Function LockWindowUpdate& Lib "user32" (byval hwndLock&)
       
      public Declare Function GetActiveWindow& Lib "user32" ()
       
      public Declare Function GetDesktopWindow& Lib "user32" ()
       
      public Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (byval lpClassName$, _
                                                                            byval lpWindowName$)
    
      public Declare Function SetForegroundWindow& Lib "user32" (byval hWnd&)
       
      public Declare Function GetClassName& Lib "user32" Alias "GetClassNameA" (byval hWnd&, _
                                                            byval lpClassName$, byval nMaxCount&)
    
      public Declare Function GetWindowRect& Lib "user32" (byval hWnd&, lpRect as RECT)
       
      public Declare Function SetWindowPos& Lib "user32" (byval hWnd&, byval hWndInsertAfter&, _
                                          byval x&, byval y&, byval cx&, byval cy&, byval wFlags&)
                                           
      public Declare Function MessageBox& Lib "user32" Alias "MessageBoxA" (byval hWnd&, _
                                                    byval lpText$, byval lpCaption$, byval wType&)
    
      public Declare Function SetTimer& Lib "user32" (byval hWnd&, byval nIDEvent&, byval uElapse&, _
                                                                                byval lpTimerFunc&)
       
      public Declare Function KillTimer& Lib "user32" (byval hWnd&, byval nIDEvent&)
    
    public Sub TimerProc(byval hWnd&, byval uMsg&, byval idEvent&, byval dwTime&)
      ' this is a callback function. This means that windows "calls back" to this function
      ' when it's time for the timer event to fire
       
      ' first thing we do is kill the timer so that no other timer events will fire
      KillTimer hWnd, idEvent
       
      ' select the type of manipulation that we want to perform
      Select Case idEvent
        Case NV_CLOSEMSGBOX '<-- we want to close this messagebox after 4 seconds
          Dim hMessageBox&
           
          ' find the messagebox window
          hMessageBox = FindWindow("#32770", "Self Closing Message Box")
           
          ' if we found it make sure it has the keyboard focus and then send it an enter to dismiss it
          If hMessageBox then
            Call SetForegroundWindow(hMessageBox)
            SendKeys "{enter}"
          End If
           
        Case NV_MOVEMSGBOX '<-- we want to move this messagebox
          Dim hMsgBox&, xPoint&, yPoint&
          Dim stMsgBoxRect as RECT, stParentRect as RECT
           
          ' find the messagebox window
          hMsgBox = FindWindow("#32770", "Position A Message Box")
         
          ' if we found it then move it
          If hMsgBox then
            ' get the rect for the parent window and the messagebox
            Call GetWindowRect(hMsgBox, stMsgBoxRect)
            Call GetWindowRect(hWnd, stParentRect)
             
            ' calculate the position for putting the messagebox in the middle of the form
            xPoint = stParentRect.Left + (((stParentRect.Right - stParentRect.Left) \ 2) - _
                                                  ((stMsgBoxRect.Right - stMsgBoxRect.Left) \ 2))
            yPoint = stParentRect.Top + (((stParentRect.Bottom - stParentRect.Top) \ 2) - _
                                                  ((stMsgBoxRect.Bottom - stMsgBoxRect.Top) \ 2))
             
            ' move the messagebox
            Call SetWindowPos(hMsgBox, HWND_TOP, xPoint, yPoint, _
                                            API_FALSE, API_FALSE, SWP_NOZORDER Or SWP_NOSIZE)
          End If
           
          ' unlock the desktop
          Call LockWindowUpdate(API_FALSE)
       
      End Select
       
    End Sub
    
    '-------------------
    'form
    private Sub Command1_Click()
      ' this shows a messagebox that will be dismissed after 4 seconds
       
      ' set the callback timer and pass our application defined ID (NV_CLOSEMSGBOX)
      ' set the time for 4 seconds (4000& microseconds)
      SetTimer hWnd, NV_CLOSEMSGBOX, 4000&, AddressOf TimerProc
    
      ' call the messagebox API function
      Call MessageBox(hWnd, "Watch this message box close itself after four seconds", _
          "Self Closing Message Box", MB_ICONQUESTION Or MB_TASKMODAL)
       
    End Sub
    
    private Sub Command2_Click()
      ' this positions the messagebox in the desired location on the screen.
      ' the location is defined in the callback timer function
       
      ' lock the desktop so that the initial position is not shown
      Call LockWindowUpdate(GetDesktopWindow())
       
      ' set the callback timer with our application defined ID (NV_MOVEMSGBOX)
      ' set the time for 10 microseconds to allow the messagebox time to become active
      SetTimer hWnd, NV_MOVEMSGBOX, 10&, AddressOf TimerProc
    
      ' call the messagebox API function
      Call MessageBox(hWnd, "message box in't in the middle of the screen?", _
          "Position A Message Box", MB_ICONQUESTION Or MB_TASKMODAL)
    
    End Sub

    Iouri Boutchkine
    iouri@hotsheet.com

    [Cimperiali colorized and indented for better reading]
    Last edited by Cimperiali; December 7th, 2002 at 05:46 AM.
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: msgbox position

    Hey, this is great!
    (too bad I am out of votes!)
    Cheers,
    Cesare

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Apr 2001
    Posts
    95

    Re: msgbox position

    Yes it is.... I'll rate for you, Cesare!


  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured