Results 1 to 4 of 4

Thread: Centering CommonDialog

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Location
    Valencia,Valencia,Spain
    Posts
    1

    Post

    We are developing an application using VB 5.0 and in certain moment we display a SaveDialog. It is designed to be displayet at (0,0) position of the parent window (the window who throws it). Of course, if the parent window is maximized, the dialog appears at the left superior corner, and we do not like this, we should prefer to show it centered. We have tried to use API calls, but, as you know, the dialog windos are showed in modal way, so your code loses the flow control and, ovbiously, we can not apply the API calls to capture the window and modify its position. What can we do?
    Thank you everybody.

  2. #2
    Junior Member
    Join Date
    Sep 1999
    Posts
    19

    Post

    Try putting the CommonDialog control inside of a container such as a frame or picturebox. Set the visible property of the container to false and place the container in the center of your form.

    Hope this helps


    ------------------
    LM Ginn


  3. #3
    Guest

    Post


    Try:

    savedialog.left = (form.width /2) - (savedialog.width /2)
    savedialog.top = (form.height /2) - (savedialog.height /2)


    ------------------
    Boothman
    There is a war out there, and it is about who controls the information, it's all about the information.


  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    You can Hook the Threads Messages looking for any Dialogs that are being created and then Center them, ie:
    Code:
    'In a Module..
    Private Type CWPSTRUCT
            lParam As Long
            wParam As Long
            message As Long
            hwnd As Long
    End Type
    
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    
    Private Const SWP_FRAMECHANGED = &H20
    Private Const SWP_NOSIZE = &H1
    Private Const WM_CREATE = &H1
    Private Const WH_CALLWNDPROC = 4
    
    Private lHook As Long
    
    Private Function WindowHook(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Dim tCWP As CWPSTRUCT
        Dim tRECT As RECT
        Dim sClass As String
        
        CopyMemory tCWP, ByVal lParam, Len(tCWP)
        Select Case tCWP.message
        Case WM_CREATE
            'Check the Type of Windows Being Created
            sClass = Space(255)
            sClass = Left$(sClass, GetClassName(tCWP.hwnd, ByVal sClass, 255))
            'If it's a Dialog Window, Center it..
            If sClass = "#32770" Then
                Call GetWindowRect(tCWP.hwnd, tRECT)
                Call SetWindowPos(tCWP.hwnd, 0, ((Screen.Width / Screen.TwipsPerPixelX) - (tRECT.Right - tRECT.Left)) / 2, ((Screen.Height / Screen.TwipsPerPixelY) - (tRECT.Bottom - tRECT.Top)) / 2, 0, 0, SWP_NOSIZE Or SWP_FRAMECHANGED)
            End If
        End Select
        WindowHook = CallNextHookEx(lHook, nCode, wParam, lParam)
    End Function
    
    Public Sub SetHook()
        'Hook the Threads Messages
        lHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf WindowHook, App.hInstance, App.ThreadID)
    End Sub
    
    Public Sub ReleaseHook()
        'UnHook the Threads Messages
        Call UnhookWindowsHookEx(lHook)
    End Sub
    Code:
    'In the Form..
    Private Sub Command1_Click()
        CommonDialog1.ShowOpen
    End Sub
    
    Private Sub Form_Load()
        SetHook
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        ReleaseHook
    End Sub
    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


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