默认情况下,打开对话框的文件列表的样式是列表样式,要实现一打开放大框就自动是大图标风格,需要在打开对话框显示在屏幕上以前,找到该文件列表控件(SysListView32)的句柄,然后改变其风格就可以了。
标准ListView控件有四种样式,分别如下:
LVS_ICON 0x0000 大图标样式
LVS_REPORT 0x0001 具体资料样式
LVS_SMALLICON 0x0002 小图标样式
LVS_LIST 0x0003 列表样式
以前ccrun曾在csdn回答过一位网友的问题,正好有一些代码,比较简单,ccrun就没有加注释。嘿嘿。
在Form上放置一个TButton,一个TOpenDialog。然后添加以下代码:
.h 文件中:
public: // User declarations
void __fastcall OpenDialogShow(TObject *Sender);
void __fastcall OpenDialogClose(TObject *Sender);
.cpp 文件中:
//---------------------------------------------------------------------------
FARPROC OldDlgOpenProc = NULL;
static LRESULT CALLBACK NewDlgOpenProc(
HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
static LRESULT CALLBACK NewDlgOpenProc(
HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(uMsg == WM_UPDATEUISTATE)
{
HWND hShell = FindWindowEx(GetParent(hWnd), NULL, "SHELLDLL_DefView", NULL);
HWND hListView = FindWindowEx(hShell, NULL, "SysListView32", NULL);
if(hListView)
{
SetWindowLong(hListView, GWL_STYLE,
GetWindowLong(hListView, GWL_STYLE) & 0xFFFFFFF0 LVS_ICON);
}
}
return CallWindowProc((FARPROC)OldDlgOpenProc, hWnd, uMsg, wParam, lParam);
}
//---------------------------------------------------------------------------
// 本文来自 http://www.ccrun.com
// 欢迎光临 C++ Builder 研究
// 作者: ccrun(老妖)
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
OpenDialog1->OnClose = OpenDialogClose;
OpenDialog1->OnShow = OpenDialogShow;
Button1->OnClick = Button1Click;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::Button1Click(TObject *Sender)
{
OpenDialog1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::OpenDialogShow(TObject *Sender)
{
OldDlgOpenProc = (FARPROC)SetWindowLong(
OpenDialog1->Handle, GWL_WNDPROC, (long)NewDlgOpenProc);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::OpenDialogClose(TObject *Sender)
{
SetWindowLong(OpenDialog1->Handle, GWL_WNDPROC, (long)OldDlgOpenProc);
}
//---------------------------------------------------------------------------
后来ccrun偶然发现,还有更简单的方法:
//---------------------------------------------------------------------------
// 本文来自 http://www.ccrun.com
// 欢迎光临 C++ Builder 研究
// 作者: ccrun(老妖)
const DWord FCIDM_SHVIEW_LARGEICON = 28713; // 大图标模式
const DWORD FCIDM_SHVIEW_SMALLICON = 28714; // 小图标模式
const DWORD FCIDM_SHVIEW_LIST = 28715; // 列表模式
const DWORD FCIDM_SHVIEW_REPORT = 28716; //
const DWORD FCIDM_SHVIEW_THUMBNAIL = 28717; // XP only
const DWORD FCIDM_SHVIEW_TILE = 28718; // XP
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
// 指定打开对话框在显示出来时执行的事件
OpenDialog1->OnShow = dlgOpenOnShow;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::WndProc(TMessage &Msg)
{
// 重载WndProc实现消息的自定义处理
if(Msg.Msg == WM_USER + 1)
{
// 文件列表是一个ListView控件,其父控件关系如下:
// TOpenDialog-->SHELLDLL_DefView-->SysListView32
HWND hDlg = (HWND)Msg.WParam;
HWND hCtrl = FindWindowEx(hDlg, NULL, "SHELLDLL_DefView", NULL);