MFC 列表控件的列宽根据窗口的大小自动调节
求助大神
在初始化的时候
窗口里的列表控件分成了4等份
当窗口扩大时希望列表的列宽还是4等份
应该怎么做
把所有的代码都沾过来了 希望大家帮帮忙
// SearchExploreDlg.cpp : 実装ファイル
//
#include "pch.h"
#include "framework.h"
#include "SearchExplore.h"
#include "SearchExploreDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// アプリケーションのバージョン情報に使われる CAboutDlg ダイアログ
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// ダイアログ データ
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV サポート
// 実装
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CSearchExploreDlg ダイアログ
CSearchExploreDlg::CSearchExploreDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_SEARCHEXPLORE_DIALOG, pParent)
{
}
void CSearchExploreDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_MFCSHELLLIST1, m_wndShellList);
DDX_Control(pDX, IDC_MFCSHELLTREE1, m_wbdShellTree);
DDX_Control(pDX, IDC_LIST1, m_aNswerList);
}
BEGIN_MESSAGE_MAP(CSearchExploreDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_GETMINMAXINFO()
ON_WM_SIZE()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CSearchExploreDlg::OnClickedButton1)
ON_NOTIFY(NM_CLICK, IDC_MFCSHELLTREE1, &CSearchExploreDlg::OnClickMfcshelltree1)
ON_WM_CONTEXTMENU()
ON_COMMAND(ID__FILEOPEN, &CSearchExploreDlg::OnOpenfile)
ON_COMMAND(ID__DIRECTORYOPEN, &CSearchExploreDlg::OnOpenDirectory)
ON_COMMAND(ID__DELETE, &CSearchExploreDlg::OnDelete)
END_MESSAGE_MAP()
// CSearchExploreDlg メッセージ ハンドラー
BOOL CSearchExploreDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
GetClientRect(&rectDlgA);
GetWindowRect(m_minimalRect);
// "バージョン情報..." メニューをシステム メニューに追加します。
// IDM_ABOUTBOX は、システム コマンドの範囲内になければなりません。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// このダイアログのアイコンを設定します。アプリケーションのメイン ウィンドウがダイアログでない場合、
// Framework は、この設定を自動的に行います。
SetIcon(m_hIcon, TRUE); // 大きいアイコンの設定
SetIcon(m_hIcon, FALSE); // 小さいアイコンの設定
// TODO: 初期化をここに追加します。
m_wbdShellTree.Expand(m_wbdShellTree.GetRootItem(), TVE_EXPAND);
m_wbdShellTree.SetRelatedList(&m_wndShellList);
GetDlgItem(IDC_MFCSHELLLIST1)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_LIST1)->ShowWindow(SW_HIDE);
CRect rect;
m_aNswerList.GetClientRect(&rect);
m_aNswerList.SetExtendedStyle(m_aNswerList.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES); //listCtrlスタイル設定
m_aNswerList.InsertColumn(0, _T("対象"), LVCFMT_LEFT, rect.Width() / 4, 0);
m_aNswerList.InsertColumn(1, _T("場所"), LVCFMT_LEFT, rect.Width() / 4, 0);
m_aNswerList.InsertColumn(2, _T("種類"), LVCFMT_LEFT, rect.Width() / 4, 0);
m_aNswerList.InsertColumn(3, _T("サイズ"), LVCFMT_RIGHT, rect.Width() / 4, 0);
pImageList = new CImageList();
pImageList->Create(32, 32, ILC_COLOR32 | ILC_MASK, 10, 10);
CImageList* intptr = m_aNswerList.SetImageList(pImageList, LVSIL_SMALL);
if (intptr != NULL)
{
delete intptr;
}
mhIcon1 = AfxGetApp()->LoadIcon(IDI_ICON8);
mhIcon2 = AfxGetApp()->LoadIcon(IDI_ICON5);
mhIcon3 = AfxGetApp()->LoadIcon(IDI_ICON7);
return TRUE; // フォーカスをコントロールに設定した場合を除き、TRUE を返します。
}
void CSearchExploreDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
void CSearchExploreDlg::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
CWnd* pLIST;
CWnd* pSHELLLIST;
CWnd* pSHELLTREE;
CWnd* pBUTTON;
CWnd* pEDIT1;
CWnd* pEDIT2;
CRect rectLIST;
CRect rectSHELLLIST;
CRect rectSHELLTREE;
CRect rectBUTTON;
CRect rectEDIT1;
CRect rectEDIT2;
CRect rectDlg;
GetClientRect(rectDlg);
pLIST = GetDlgItem(IDC_LIST1);
if (NULL == pLIST)
{
return;
}
pLIST->GetWindowRect(&rectLIST);
ScreenToClient(rectLIST);
pLIST->MoveWindow(rectLIST.left, rectLIST.top, rectLIST.Width() + (cx - rectDlgA.Width()), rectLIST.Height() + (cy - rectDlgA.Height()));
pSHELLLIST = GetDlgItem(IDC_MFCSHELLLIST1);
if (NULL == pSHELLLIST)
{
return;
}
pSHELLLIST->GetWindowRect(&rectSHELLLIST);
ScreenToClient(rectSHELLLIST);
pSHELLLIST->MoveWindow(rectSHELLLIST.left, rectSHELLLIST.top, rectSHELLLIST.Width() + (cx - rectDlgA.Width()), rectSHELLLIST.Height() + (cy - rectDlgA.Height()));
pSHELLTREE = GetDlgItem(IDC_MFCSHELLTREE1);
if (NULL == pSHELLTREE)
{
return;
}
pSHELLTREE->GetWindowRect(&rectSHELLTREE);
ScreenToClient(rectSHELLTREE);
pSHELLTREE->MoveWindow(rectSHELLTREE.left, rectSHELLTREE.top, rectSHELLTREE.Width(), rectSHELLTREE.Height() + (cy - rectDlgA.Height()));
pBUTTON = GetDlgItem(IDC_BUTTON1);
if (NULL == pBUTTON)
{
return;
}
pBUTTON->GetWindowRect(&rectBUTTON);
ScreenToClient(rectBUTTON);
pBUTTON->MoveWindow(rectBUTTON.left + (cx - rectDlgA.Width()), rectBUTTON.top, rectBUTTON.Width() , rectBUTTON.Height());
pEDIT1 = GetDlgItem(IDC_EDIT1);
if (NULL == rectEDIT1)
{
return;
}
pEDIT1->GetWindowRect(&rectEDIT1);
ScreenToClient(rectEDIT1);
pEDIT1->MoveWindow(rectEDIT1.left, rectEDIT1.top, rectEDIT1.Width() + (cx - rectDlgA.Width()), rectEDIT1.Height());
pEDIT2 = GetDlgItem(IDC_EDIT2);
if (NULL == rectEDIT2)
{
return;
}
pEDIT2->GetWindowRect(&rectEDIT2);
ScreenToClient(rectEDIT2);
pEDIT2->MoveWindow(rectEDIT2.left + (cx - rectDlgA.Width()), rectEDIT2.top, rectEDIT2.Width(), rectEDIT2.Height());
rectDlgA = rectDlg;
Invalidate(); //OnPaintを呼ぶ
}
void CSearchExploreDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 描画のデバイス コンテキスト
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// クライアントの四角形領域内の中央
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
ScreenToClient(rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// アイコンの描画
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// ユーザーが最小化したウィンドウをドラッグしているときに表示するカーソルを取得するために、
// システムがこの関数を呼び出します。
HCURSOR CSearchExploreDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CSearchExploreDlg::SearchKey()
{
GetDlgItemText(IDC_EDIT2, m_strKey); //キーワード取得
if (m_strKey == "") //キーワードがない場合
{
m_aNswerList.DeleteAllItems();
GetDlgItem(IDC_MFCSHELLLIST1)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_LIST1)->ShowWindow(SW_HIDE);
return;
}
GetDlgItem(IDC_MFCSHELLLIST1)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_LIST1)->ShowWindow(SW_SHOW);
m_wndShellList.GetCurrentFolder(m_strPath); //検索パスはListコントロールから取得
//CString strSearchPath = m_strPath + _T("\\") + "*" + m_strKey + "*";
CString strSearchPath = m_strPath +_T("\\")+ _T("*.*");
SearchProcess(strSearchPath);
}
void CSearchExploreDlg::SearchProcess(CString strSearchPath)
{
CStringArray strFileItemALL;
CStringArray strDirItemALL;
CFileFind fFinder; //インスタンス
BOOL bFind = fFinder.FindFile(strSearchPath); //ファイル検索を開く
ULONGLONG filesize = 0;
CString dst;
CString sizeAnswer;
CFileStatus fileStatus;
#if 0
while (bFind)
{
HICON mhIcon = NULL;
bFind = fFinder.FindNextFile();
if (fFinder.IsDots())
{
continue;
}
if (fFinder.IsDirectory())
{
CString cstrDirName = fFinder.GetFileName(); //directory name
CString cstrDirPath = fFinder.GetFilePath(); //directory path
strDirItemALL.Add(cstrDirName);
if (SHGetFileInfo(cstrDirPath, NULL, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_TYPENAME))
{
//sfi.hIcon にアイコンハンドルが戻ってくる
mhIcon = sfi.hIcon;
}
int nImage = -1;
if (mhIcon)
{
// アイコンをイメージリストに追加
nImage = pImageList->Add(mhIcon);
ExploreMap.SetAt(, );
// 使い終わったアイコンリソースを破棄
DeleteObject(mhIcon);
}
m_aNswerList.InsertItem(0, cstrDirName, nImage);
SearchProcess(cstrDirPath + _T("\\") + _T("*.*"));
continue;
}
CString cstrFileName = fFinder.GetFileName(); //file name
CString cstrFilePath = fFinder.GetFilePath(); //file path
strFileItemALL.Add(cstrFileName);
if (SHGetFileInfo(cstrFilePath, NULL, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_TYPENAME))
{
//sfi.hIcon にアイコンハンドルが戻ってくる
mhIcon = sfi.hIcon;
}
int nImage = -1;
if (mhIcon)
{
// アイコンをイメージリストに追加
nImage = pImageList->Add(mhIcon);
// 使い終わったアイコンリソースを破棄
DeleteObject(mhIcon);
}
m_aNswerList.InsertItem(0, cstrFileName, nImage);
}
fFinder.Close();
#else
while (bFind)
{
int nImage1 = pImageList->Add(mhIcon1);
int nImage2 = pImageList->Add(mhIcon2);
int nImage3 = pImageList->Add(mhIcon3);
bFind = fFinder.FindNextFile();
if (fFinder.IsDots())
{
continue;
}
if (fFinder.IsDirectory())
{
CString cstrDirName = fFinder.GetFileName(); //directory name
CString cstrDirPath = fFinder.GetFilePath(); //directory path
int a = cstrDirName.Find(