默認情況下,打開對話框的文件列表的樣式是列表樣式,要實現一打開放大框就自動是大圖標風格,需要在打開對話框顯示在屏幕上以前,找到該文件列表控件(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);
if(hCtrl != NULL)
// 向SHELLDLL_DefView控件發送 WM_COMMAND 消息
// 關鍵是wParam參數,指定ListView的樣式
SendMessage(hCtrl, WM_COMMAND, FCIDM_SHVIEW_LARGEICON, 0); // 大圖標
}
TForm::WndProc(Msg);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::dlgOpenOnShow(TObject *Sender)
{
// 當打開對話框顯示出來時發送自定義消息到窗體
HWND hDlg = GetParent(((TOpenDialog *)Sender)->Handle);
PostMessage(Handle, WM_USER + 1, (WPARAM)hDlg, 0);
}
//---------------------------------------------------------------------------
// 測試代碼:
void __fastcall TfrmMain::Button1Click(TObject *Sender)
{
// Show出打開對話框
OpenDialog1->Execute();
}