Close Common Dialog from hookproc

  • Thread starter david epsom dot com dot au
  • Start date
D

david epsom dot com dot au

I am trying to send a message to a FileOpen dialog, to tell it to close, as
suggested here:
http://msdn.microsoft.com/library/d...ence/CommonDialogBoxFunctions/OFNHookProc.asp

I am using :
Call PostMessage(GetParent(hwnd), WM_COMMAND, IDABORT, 0)
this didn't work either:
Call PostMessage(GetParent(hwnd), WM_SYSCOMMAND, SC_CLOSE, 0)

hwnd is the handle to the child dialog (using the default template),
and GetParent(hwnd) is the handle to the dialog.

When I post the message, nothing happens.

FWIW, using (hwnd) closes the parent application : using
GetParent(GetParent(hwnd), (which correctly returns the hwnd of the parent
application) locks up the dialog and the parent application.

(I want this to work like the File|Get External Data|Import dialog in
MSAccess: I want to close the dialog when the ODBC file type is selected. I
am trapping the CFileDialog::OnTypeChange event)

Any suggestions?

(david)
 
R

Randy Birch

Try something along the line of ...

Case WM_NOTIFY

Dim notify As OFNOTIFY
Dim hWndTargetBtn As Long

Call CopyMemory(notify, ByVal lParam, Len(notify))

Select Case notify.hdr.code

Case CDN_SELCHANGE

hwndParent = GetParent(hwnd)
hWndTargetBtn = GetDlgItem(hwndParent, IDOK)

If <WHATEVER> Then

If hWndTargetBtn <> 0 Then

'simulate a mouse click on the button
Call PutFocus(hWndTargetBtn)
Call PostMessage(hWndTargetBtn, WM_LBUTTONDOWN,
0, ByVal 0&)
Call PostMessage(hWndTargetBtn, WM_LBUTTONUP, 0,
ByVal 0&)
Call PostMessage(hWndTargetBtn, WM_LBUTTONDOWN,
0, ByVal 0&)
Call PostMessage(hWndTargetBtn, WM_LBUTTONUP, 0,
ByVal 0&)
Debug.Print "should have closed", hWndTargetBtn

End If

End If

.... where the " If <WHATEVER> " test is your code to ensure an actual file
name has been selected.


'------------------------
Public Const IDOK = 1
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202

Public Const CDN_FIRST As Long = -601
Public Const CDN_SELCHANGE As Long = (CDN_FIRST - &H1)
Public Const CDN_TYPECHANGE As Long = (CDN_FIRST - &H6)

Public Const WM_NOTIFY As Long = &H4E&

Public Type NMHDR
hWndFrom As Long
idfrom As Long
code As Long
End Type

Public Type OFNOTIFY
hdr As NMHDR
ofnPtr As Long ' OPENFILENAMEPTR
pszFile As Long
End Type


Public Declare Function GetDlgItem Lib "user32" _
(ByVal hDlg As Long, _
ByVal nIDDlgItem As Long) As Long

Public Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Long) As Long

Public Declare Function PutFocus Lib "user32" _
Alias "SetFocus" _
(ByVal hwnd As Long) As Long

Public Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)


--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


: I am trying to send a message to a FileOpen dialog, to tell it to close,
as
: suggested here:
:
http://msdn.microsoft.com/library/d...ence/CommonDialogBoxFunctions/OFNHookProc.asp
:
: I am using :
: Call PostMessage(GetParent(hwnd), WM_COMMAND, IDABORT, 0)
: this didn't work either:
: Call PostMessage(GetParent(hwnd), WM_SYSCOMMAND, SC_CLOSE, 0)
:
: hwnd is the handle to the child dialog (using the default template),
: and GetParent(hwnd) is the handle to the dialog.
:
: When I post the message, nothing happens.
:
: FWIW, using (hwnd) closes the parent application : using
: GetParent(GetParent(hwnd), (which correctly returns the hwnd of the parent
: application) locks up the dialog and the parent application.
:
: (I want this to work like the File|Get External Data|Import dialog in
: MSAccess: I want to close the dialog when the ODBC file type is selected.
I
: am trapping the CFileDialog::OnTypeChange event)
:
: Any suggestions?
:
: (david)
:
:
 
D

david epsom dot com dot au

Cool!

(david)

Randy Birch said:
Try something along the line of ...

Case WM_NOTIFY

Dim notify As OFNOTIFY
Dim hWndTargetBtn As Long

Call CopyMemory(notify, ByVal lParam, Len(notify))

Select Case notify.hdr.code

Case CDN_SELCHANGE

hwndParent = GetParent(hwnd)
hWndTargetBtn = GetDlgItem(hwndParent, IDOK)

If <WHATEVER> Then

If hWndTargetBtn <> 0 Then

'simulate a mouse click on the button
Call PutFocus(hWndTargetBtn)
Call PostMessage(hWndTargetBtn, WM_LBUTTONDOWN,
0, ByVal 0&)
Call PostMessage(hWndTargetBtn, WM_LBUTTONUP, 0,
ByVal 0&)
Call PostMessage(hWndTargetBtn, WM_LBUTTONDOWN,
0, ByVal 0&)
Call PostMessage(hWndTargetBtn, WM_LBUTTONUP, 0,
ByVal 0&)
Debug.Print "should have closed", hWndTargetBtn

End If

End If

... where the " If <WHATEVER> " test is your code to ensure an actual file
name has been selected.


'------------------------
Public Const IDOK = 1
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202

Public Const CDN_FIRST As Long = -601
Public Const CDN_SELCHANGE As Long = (CDN_FIRST - &H1)
Public Const CDN_TYPECHANGE As Long = (CDN_FIRST - &H6)

Public Const WM_NOTIFY As Long = &H4E&

Public Type NMHDR
hWndFrom As Long
idfrom As Long
code As Long
End Type

Public Type OFNOTIFY
hdr As NMHDR
ofnPtr As Long ' OPENFILENAMEPTR
pszFile As Long
End Type


Public Declare Function GetDlgItem Lib "user32" _
(ByVal hDlg As Long, _
ByVal nIDDlgItem As Long) As Long

Public Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Long) As Long

Public Declare Function PutFocus Lib "user32" _
Alias "SetFocus" _
(ByVal hwnd As Long) As Long

Public Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)


--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


: I am trying to send a message to a FileOpen dialog, to tell it to close,
as
: suggested here:
:
http://msdn.microsoft.com/library/d...ence/CommonDialogBoxFunctions/OFNHookProc.asp
:
: I am using :
: Call PostMessage(GetParent(hwnd), WM_COMMAND, IDABORT, 0)
: this didn't work either:
: Call PostMessage(GetParent(hwnd), WM_SYSCOMMAND, SC_CLOSE, 0)
:
: hwnd is the handle to the child dialog (using the default template),
: and GetParent(hwnd) is the handle to the dialog.
:
: When I post the message, nothing happens.
:
: FWIW, using (hwnd) closes the parent application : using
: GetParent(GetParent(hwnd), (which correctly returns the hwnd of the parent
: application) locks up the dialog and the parent application.
:
: (I want this to work like the File|Get External Data|Import dialog in
: MSAccess: I want to close the dialog when the ODBC file type is selected.
I
: am trapping the CFileDialog::OnTypeChange event)
:
: Any suggestions?
:
: (david)
:
:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top