How to read listbox part of OpenFileDialog

  • Thread starter Thread starter Bill Rogers
  • Start date Start date
B

Bill Rogers

Hi All,
I am trying to find out how many files are currently listed in the listbox
part of an OpenFileDialog. I can get to the OpenFileDialog with
system.reflection, but when I drill down I run into a wall. I know it can
be done because I can see it in various object browsers I use.
Many thanks,
Bill
 
Hi Bill,

I am researching the issue and will update you with new information as soon
as possible.
As a quick view, since the OpenFileDialog is a sealed class, you can not
inherit a new custom dialog.
Have you tried to use the FindWindow and FindWindowEx APIs to get the
window handle of the list view in the OpenFileDialog,
and then get the item in the listview window?


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
 
Hi Peter,
Thank you for your suggestion. I have not tried using FindWindow or
FindWindowEx to get to it but I will do that presently and get back to you.
Regards,
Bill Rogers
 
Thanks Peter,
Your suggestion on how to approach finding the OpenFileDialog's count of
items displayed in the listbox was most helpful.
Here is the solution I came up with just in case someone else wants to know
how to do this.
There is also an article in MSDN Magazine, January 2002, "C++ Q&A", by Paul
DiLascia, which shows how to work with the innards of the OpenFileDialog
using MFC.
Additional research indicates I could acces the count via the Accessibility
interface too.
Regards,
Bill Rogers

Explanation for my solution:
The goal is to get the count of items in the listbox shown in the
OpenFileDialog
The listbox of interest is actually a SyslistView32 contianed in a
SHELLDLL_DefView, contained in the OpenFileDialog
You can see this by opening the OpenFileDialog from a program and then
examining it with Spy++

First, using FindWindowEx, find the OpenFileDialog's main window (it must be
on the screen to do this)
The first argument to FindWindowEx is null, so it starts at the desktop
level - this is what you want because the
OpenFileDialog comes up as a top level window, not a child window of your
application
The ATOM specified here was found with Spy++

Next, find the SHELLDLL_DefView within this dialog, again using FindWindowEx
This time the first argument to FindWindowEx is the hWnd of the
OpenFileDialog so we limit the search to within just this dialog
The thing we are looking for has no caption so the only way we can find it
is by its ATOM - which was found via Spy++

The next step is to again use FindWindowEx to get the window handle of the
listbox (actually a SysListView32)
Again, the ATOM and caption were found using Spy++

Finally you get the count from the SyslistView32 by sending it a message via
SendMessage
I used the ListView_GetItemCount macro defined in Commctrl.h to do this
Error checking in this example is minimal...
#include <Commctrl.h>
hWnd hwnd1, hwnd2, hwnd3;
int count;

//...

//find the listbox in the file open dialog
//captions and atoms found with spy++
if (!(hwnd1 = FindWindowEx(0, 0, MAKEINTATOM(0x8002), L"Open"))) return
false; \\the file open dialog
if (!(hwnd2 = FindWindowEx(hwnd1, 0, MAKEINTATOM(0xC0CB), 0))) return false;
\\SHELLDLL_DefView
if (!(hwnd3 = FindWindowEx(hwnd2, 0, MAKEINTATOM(0xC043), L"FolderView")))
return false; \\SysListView32
count = ListView_GetItemCount(hwnd3);
 
Back
Top