Results 1 to 5 of 5

Thread: SetWindowPos example...?

  1. #1
    AlvaroF1
    Guest

    Question SetWindowPos example...?

    I'd like a simple example of SetWindowPos to center the Printer Dialog (from the Windows Commom Dialog) on the screen.

    Thanks.

  2. #2
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    you got to find the print dialog window handle (hWnd) first and then just applied this handle in the SetWindowPos first parameter will do.

    regards,

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    The SetWindowPos function changes the size, position, and Z order of a child, pop-up, or top-level window.
    Child, pop-up, and top-level windows are ordered according to their appearance on the screen. The topmost
    window receives the highest rank and is the first window in the Z order.
    VB Code:
    1. Const HWND_TOPMOST = -1
    2. Const HWND_NOTOPMOST = -2
    3. Const SWP_NOSIZE = &H1
    4. Const SWP_NOMOVE = &H2
    5. Const SWP_NOACTIVATE = &H10
    6. Const SWP_SHOWWINDOW = &H40
    7. Private Declare Sub 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)
    8.  
    9. Private Sub Form_Activate()
    10.     'KPD-Team 1998
    11.     'URL: [url]http://www.allapi.net/[/url]
    12.     'E-Mail: [email][email protected][/email]
    13.     'Set the window position to topmost
    14.     SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    15. End Sub

  4. #4
    AlvaroF1
    Guest

    Thumbs down

    Thanks Hack, but it does not work.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I did a little web searching last night. See if this works for you.
    VB Code:
    1. 'In a Bas Module
    2. ' Centrdlg sample by Matt Hart - [email][email protected][/email]
    3. ' [url]http://matthart.com[/url]
    4.  
    5. Public Type CWPSTRUCT
    6.     lParam As Long
    7.     wParam As Long
    8.     message As Long
    9.     hwnd As Long
    10. End Type
    11.  
    12. Public Type RECT
    13.     Left As Long
    14.     Top As Long
    15.     Right As Long
    16.     Bottom As Long
    17. End Type
    18.  
    19. Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
    20. Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    21. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    22. Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    23. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    24. Public 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
    25. Public 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
    26. Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    27.  
    28. Public Const WH_CALLWNDPROC = 4
    29. Public Const WM_INITDIALOG = &H110
    30. Public Const GWL_WNDPROC = (-4)
    31. Public Const SWP_NOSIZE = &H1
    32. Public Const SWP_NOACTIVATE = &H10
    33.  
    34. Public lWndProc As Long
    35. Public hHook As Long, lHookWndProc As Long
    36.  
    37. Public Function AppHook(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    38.     Dim CWP As CWPSTRUCT
    39.     CopyMemory CWP, ByVal lParam, Len(CWP)
    40.     Select Case CWP.message
    41.         Case WM_INITDIALOG
    42.             lWndProc = SetWindowLong(CWP.hwnd, GWL_WNDPROC, AddressOf Dlg_WndProc)
    43.             AppHook = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
    44.             UnhookWindowsHookEx hHook
    45.             hHook = 0
    46.             Exit Function
    47.     End Select
    48.     AppHook = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
    49. End Function
    50.  
    51. Public Function Dlg_WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    52.     Select Case Msg
    53.         Case WM_INITDIALOG
    54.             Dim R As RECT, x As Long, y As Long
    55.             GetWindowRect hwnd, R
    56.             x = (Form1.Left \ Screen.TwipsPerPixelX + (Form1.Width \ Screen.TwipsPerPixelX - (R.Right - R.Left)) \ 2)
    57.             y = (Form1.Top \ Screen.TwipsPerPixelY + (Form1.Height \ Screen.TwipsPerPixelY - (R.Bottom - R.Top)) \ 2)
    58.             SetWindowPos hwnd, 0, x, y, 0, 0, SWP_NOSIZE Or SWP_NOACTIVATE
    59.             SetWindowLong hwnd, GWL_WNDPROC, lWndProc
    60.     End Select
    61.     Dlg_WndProc = CallWindowProc(lWndProc, hwnd, Msg, wParam, lParam)
    62. End Function
    63.  
    64. 'on the form with the common dialog control
    65. Private Sub Command1_Click()
    66. ' Centrdlg sample by Matt Hart - [email][email protected][/email]
    67. ' [url]http://matthart.com[/url]
    68. '
    69. ' This sample shows the how to use subclassing to process the
    70. ' dialog messages and move the dialog.
    71. '
    72. ' It's actually a bit easier if you use the API common dialog
    73. ' functions rather than the control. You can specify a callback
    74. ' procedure when you create the dialog rather than having to mess
    75. ' with hooking the application so that you can watch for the
    76. ' dialog to receive the WM_INITDIALOG message.
    77. '
    78. ' However, you can see that I immediately unhook and unsubclass
    79. ' the application as soon as I finish processing the needed
    80. ' message. That leaves nothing "messy" hanging around. I do double
    81. ' check that the hook is released in case there was some kind
    82. ' of error attempting to create the dialog.
    83. '
    84. ' There's also another way to do it - use the DWL_DLGPROC to
    85. ' set a pointer to a dialog box procedure. However, I found that
    86. ' using this method doesn't seem to work very well with VB's
    87. ' common dialog control.
    88. '
    89. ' Note that this sample [sort of] uses the method I created for
    90. ' my "setting windows styles" article in Visual Basic Programmer's
    91. ' Journal - an application hook to watch for messages as an object
    92. ' is being created.
    93.     hHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf AppHook, App.hInstance, App.ThreadID)
    94.     CommonDialog1.ShowPrinter
    95.     If hHook Then UnhookWindowsHookEx hHook
    96. End Sub

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