Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Comment position l'objet CommonDialog au centre de l' écran ??

5 views
Skip to first unread message

François Picalausa

unread,
Feb 20, 2004, 10:47:26 AM2/20/04
to
Bonjour/soir,

Tu peux le faire avec deux méthodes:
1°) un timer et l'API FindWindow.
Tu active le timer (qui doit avoir un interval très faible)
Dans le code déclanché par le timer:
Tu retrouve la fenêtre à l'aide du FindWindow.
A l'aide de l'API SetWindowPos, tu déplace la fenêtre à partir du hndle
récupéré.
Cette méthode comporte l'inconvénient de ne pas être sûr de la fenêtre que
tu retrouve (et il pourrait y avoir un décallage entre l'apparition et le
centrage).

2°) Tu crée la boite de dialogue commune par API (cfr la faq avec par
exemple http://faq.vb.free.fr/index.php?question=90). Dans la structure
OPENFILENAME (dans cet exemple), tu rempli lpfnHook par l'adresse d'une
fonction et tu peux travailler dessus sans problème.

J'envoie un exemple de cette seconde méthode dès que j'ai finit de le coder.

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


<ano...@anaymous.com> a écrit dans le message de
news:c156km$an6$1...@s1.read.news.oleane.net
> tout est dans le titre


François Picalausa

unread,
Feb 20, 2004, 11:03:31 AM2/20/04
to
Bonjour/soir,

Voici l'exemple promis:

'Dans une form, Form1:
Option Explicit

' Déclaration de l'API
Private Declare Function GetOpenFileName _
Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" _
( _
pOpenfilename As OPENFILENAME _
) _
As Long

' GetOpenFileName flags
Public Enum OFN_Constants
OFN_ALLOWMULTISELECT = &H200
OFN_CREATEPROMPT = &H2000
OFN_DONTADDTORECENT = &H2000000
OFN_ENABLEHOOK = &H20
OFN_ENABLEINCLUDENOTIFY = &H400000
OFN_ENABLESIZING = &H800000
OFN_ENABLETEMPLATE = &H40
OFN_ENABLETEMPLATEHANDLE = &H80
OFN_EX_NOPLACESBAR = &H1
OFN_EXPLORER = &H80000
OFN_EXTENSIONDIFFERENT = &H400
OFN_FILEMUSTEXIST = &H1000
OFN_FORCESHOWHIDDEN = &H10000000
OFN_HIDEREADONLY = &H4
OFN_LONGNAMES = &H200000
OFN_NOCHANGEDIR = &H8
OFN_NODEREFERENCELINKS = &H100000
OFN_NOLONGNAMES = &H40000
OFN_NONETWORKBUTTON = &H20000
OFN_NOREADONLYRETURN = &H8000
OFN_NOTESTFILECREATE = &H10000
OFN_NOVALIDATE = &H100
OFN_OVERWRITEPROMPT = &H2
OFN_PATHMUSTEXIST = &H800
OFN_READONLY = &H1
OFN_SHAREAWARE = &H4000
OFN_SHAREFALLTHROUGH = 2
OFN_SHARENOWARN = 1
OFN_SHAREWARN = 0
OFN_SHOWHELP = &H10
OFN_USEMONIKERS = &H1000000
End Enum

Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Public Function OpenFileDialog(Optional DialogTitle As String, _
Optional sFilter As String, _
Optional Flags As OFN_Constants, _
Optional InitialDir As String, _
Optional hInstance As Long _
) _
As String

Dim OFName As OPENFILENAME

With OFName
.lStructSize = Len(OFName)
.hInstance = hInstance
.lpstrFilter = sFilter
.lpstrFile = String$(260, vbNullChar)
.nMaxFile = 255
.lpstrFileTitle = String$(260, vbNullChar)
.nMaxFileTitle = 255
.lpstrInitialDir = InitialDir
.lpstrTitle = DialogTitle
.Flags = Flags
.lpfnHook = GetProcAddress(AddressOf OFNHookProc)
End With

If GetOpenFileName(OFName) Then
OpenFileDialog = Trim$(OFName.lpstrFile)
End If
End Function

Private Sub Form_Load()
OpenFileDialog "Choississez un fichier...", "Sons wav (*.wav)" + Chr$(0)
+ "*.wav" & Chr$(0), OFN_HIDEREADONLY Or OFN_EXPLORER Or OFN_ENABLEHOOK,
CurDir, App.hInstance
End Sub

Private Function GetProcAddress(ProcAddress As Long) As Long
GetProcAddress = ProcAddress
End Function

'Dans un module standard, module1:
Option Explicit

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 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 Declare Function GetParent _
Lib "user32" _
( _
ByVal hwnd As Long _
) _
As Long

Private Const WM_INITDIALOG = &H110

Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4

Function OFNHookProc(ByVal hdlg As Long, ByVal uiMsg As Long, ByVal wParam
As Long, ByVal lParam As Long) As Long
Select Case uiMsg
Case WM_INITDIALOG 'On peut redimensionner
Dim WndRect As RECT

'hdlg
'[in] Handle to the child dialog box of the Open or Save As
dialog box.
'Use the GetParent function to get the handle to the Open or
Save As dialog box.
Dim WndHandle As Long
WndHandle = GetParent(hdlg)

If GetWindowRect(WndHandle, WndRect) Then
'On repositionne la boite de dialogue

'Attention, le code suivant peut ne pas fonctionner:
'http://support.microsoft.com/default.aspx?scid=253940
SetWindowPos WndHandle, 0, _
(Screen.Width / Screen.TwipsPerPixelX - (WndRect.Right -
WndRect.Left)) / 2, _
(Screen.Height / Screen.TwipsPerPixelY -
(WndRect.Bottom - WndRect.Top)) / 2, _
0, 0, SWP_NOSIZE Or SWP_NOZORDER
End If
Case Else
OFNHookProc = 0 'on ignore le message
End Select
End Function

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"François Picalausa" <fpica...@chez.com> a écrit dans le message de
news:%23tfwYj8...@TK2MSFTNGP12.phx.gbl


> J'envoie un exemple de cette seconde méthode dès que j'ai finit de le
> coder.
>
>

Zoury

unread,
Feb 20, 2004, 11:12:16 AM2/20/04
to
Salut! :O)

et voici un exemple si tu utilises la composante et non les APIs..

'***
' Form1
' 1 CommandButton
' 1 CommonDialog
Option Explicit

Private Declare Function SetTimer _


Lib "user32" _
( _
ByVal hwnd As Long, _

ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long _
) As Long

Private Sub Command1_Click()
Call SetTimer(Me.hwnd, 0, 10, AddressOf TimerProc)
' Nous devons entré un titre afin de retrouver la fenêtre
CommonDialog1.DialogTitle = "Sélectionner un fichier"
Call CommonDialog1.ShowOpen
End Sub

Private Sub Form_Load()
Command1.Caption = "Ouvrir..."
End Sub
'***
'***
' Module1
Option Explicit

Private Declare Function KillTimer _


Lib "user32" _
( _
ByVal hwnd As Long, _

ByVal nIDEvent As Long _
) As Long

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function FindWindow _
Lib "user32" _
Alias "FindWindowA" _
( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long

Private Declare Function GetWindowRect _
Lib "user32" _
( _
ByVal hwnd As Long, _

ByRef lpRect As RECT _
) 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

Public Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal
uElapse As Long, ByVal lpTimerFunc As Long)

Dim hCDWnd As Long
Dim rc As RECT
Dim lScreenWidth As Long
Dim lScreenHeight As Long
Dim lCDWidth As Long
Dim lCDHeight As Long

hCDWnd = FindWindow(vbNullString, Form1.CommonDialog1.DialogTitle)
If (hCDWnd > 0) Then

' calcule les dimensions de l'écran (résolution)
lScreenWidth = Screen.Width \ Screen.TwipsPerPixelX
lScreenHeight = Screen.Height \ Screen.TwipsPerPixelY

' Obtient et calcule les dimensions de
' la boite de dialogue
Call GetWindowRect(hCDWnd, rc)
lCDWidth = rc.Right - rc.Left
lCDHeight = rc.Bottom - rc.Top

' repositionne la fenêtre
Call SetWindowPos(hCDWnd, 0, (lScreenWidth - lCDWidth) \ 2,
(lScreenHeight - lCDHeight) \ 2, lCDWidth, lCDHeight, 0)

' stop le timer
Call KillTimer(Form1.hwnd, 0)

End If

End Sub
'***

et passant, poste tes messages en format Texte Brut et non HTML... c'est
moins gourmant pour ceux qui ont une connection lente.. ;O)

--
Cordialement
Yanick Lefebvre - MVP pour Visual Basic
http://faq.vb.free.fr/?rubrique=0 - http://www.mvps.org/vbnet/
http://www.mentalis.org/agnet/apiguide.shtml - http://www.mztools.com/

Merci de poster les réponses au groupe afin d'en faire profiter à tous


François Picalausa

unread,
Feb 20, 2004, 11:24:12 AM2/20/04
to
Bonjour/soir,

A noter quand même que la deuxième solution EST débuggable en pas à pas,
sans aucun plantage fatal...
Donc, je conseille vivement la seconde solution à la première!

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"François Picalausa" <fpica...@chez.com> a écrit dans le message de
news:%23tfwYj8...@TK2MSFTNGP12.phx.gbl


> 2°) Tu crée la boite de dialogue commune par API (cfr la faq avec par
> exemple http://faq.vb.free.fr/index.php?question=90). Dans la
> structure OPENFILENAME (dans cet exemple), tu rempli lpfnHook par
> l'adresse d'une fonction et tu peux travailler dessus sans problème.
>
> J'envoie un exemple de cette seconde méthode dès que j'ai finit de le
> coder.

0 new messages