📄 fileeditctrl.cpp
字号:
#ifdef AFX_PJAIMAGE_H__F15965B0_B05A_11D4_B625_A1459D96AB20__INCLUDED_
if (m_pButtonImage)
{ // draw the image
if (DrawRect.Width() > 5 && DrawRect.Height() > 5)
m_pButtonImage->DrawImage(&MemDC,
2,
2,
DrawRect.Width() - 5,
DrawRect.Height() - 5,
(((m_nButtonState & BTN_FLAT) && (GetFlags() & FEC_GRAYSCALE)) ? PJAI_GRAYSCALE : 0) | m_dwImageDrawFlags);
}
else // draw the dots
#endif
{
COLORREF dotcolour = GetSysColor(COLOR_BTNTEXT);
if ((m_nButtonState & BTN_FLAT) && (GetFlags() & FEC_GRAYSCALE))
{
int grayscale = (((GetBValue(dotcolour) * 11) + (GetGValue(dotcolour) * 59) + (GetRValue(dotcolour) * 30)) / 100);
dotcolour = RGB(grayscale, grayscale, grayscale);
}
DrawDots(&MemDC, dotcolour);
}
}
else
{
ASSERT(FALSE); // Invalid nButtonState
}
}
// ShowDC(MemDC);
// Blit the button image onto the screen
WndDC.BitBlt(m_rcButtonRect.left,
m_rcButtonRect.top,
m_rcButtonRect.Width(),
m_rcButtonRect.Height(),
&MemDC,
0,
0,
SRCCOPY);
// clean up the memory DC
MemDC.RestoreDC(savedDC);
Bitmap.DeleteObject();
// update m_nButtonState
m_nButtonState = nButtonState;
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::DrawDots (protected member function)
// Draws the dots on the button
//
// Parameters :
// pDC [in] - Pointer to the device context needed for drawing
// CR [in] - The colour of the dots
// nOffset [in] - How far down and to the right to shift the dots
//
// Returns :
// Nothing
//
/////////////////////////////////////////////////////////////////////////////
void CFileEditCtrl::DrawDots(CDC *pDC, COLORREF CR, int nOffset /* = 0 */)
{
int width = m_rcButtonRect.Width(); // width of the button
div_t divt = div(width, 4);
int delta = divt.quot; // space between dots
int left = width / 2 - delta - (divt.rem ? 0 : 1); // left side of first dot
width = width / 10; // width and height of one dot
int top = m_rcButtonRect.Height() / 2 - width / 2; // top of dots
left += nOffset; // draw dots shifted? (for button pressed)
top += nOffset;
// draw the dots
if (width < 2)
{
pDC->SetPixelV(left, top, CR);
left += delta;
pDC->SetPixelV(left, top, CR);
left += delta;
pDC->SetPixelV(left, top, CR);
}
else
{
CPen thePen(PS_SOLID, 1, CR); // set the dot colour
CPen *pOldPen = pDC->SelectObject(&thePen);
CBrush theBrush(CR);
CBrush *pOldBrush = pDC->SelectObject(&theBrush);
pDC->Ellipse(left, top, left + width, top + width);
left += delta;
pDC->Ellipse(left, top, left + width, top + width);
left += delta;
pDC->Ellipse(left, top, left + width, top + width);
pDC->SelectObject(pOldBrush); // reset the DC
theBrush.DeleteObject();
pDC->SelectObject(pOldPen);
thePen.DeleteObject();
}
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::ExpandWildCards (protected member function)
// resolves any wildcards ('*' and/or '?') found in the file name
// calls AddFile() to add the files to the m_Files list
//
// Parameters :
// FileName [in] - the file name
//
// Returns :
// Nothing
//
/////////////////////////////////////////////////////////////////////////////
void CFileEditCtrl::ExpandWildCards(const CString &FileName)
{
DWORD Flags = GetFlags();
if (!(Flags & FEC_WILDCARDS) || -1 == FileName.FindOneOf(_T("*?")))
{ // wildcards not permitted or not found
AddFile(FileName);
return;
}
CString Temp;
CString Path;
if (_T('\\') == FileName[0] || (FileName.GetLength() > 1 && _T(':') == FileName[1]))
Temp = FileName;
else
Temp = m_szFolder + FileName;
_tfullpath(Path.GetBuffer(_MAX_PATH), Temp, _MAX_PATH);
Path.ReleaseBuffer();
CFileFind cff;
BOOL Finding = cff.FindFile(Path);
while (Finding)
{
Finding = cff.FindNextFile();
Path = cff.GetFilePath();
if (!cff.IsDirectory())
{
AddFile(Path);
if (!(Flags & FEC_MULTIPLE))
break;
}
}
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::FECBrowseForFolder (protected member function)
// Set up and call SHBrowseForFolder().
// Update the control to the users selection
//
// Parameters :
// None
//
// Returns :
// TRUE if user clicks OK in SHBrowseForFolder dialog
// FALSE otherwise
//
/////////////////////////////////////////////////////////////////////////////
BOOL CFileEditCtrl::FECBrowseForFolder()
{
BOOL bReturnValue = FALSE;
#if defined _DEBUG
if (m_pBROWSEINFO->lpfn == FECFolderProc)
ASSERT((CWnd *)m_pBROWSEINFO->lParam == this);
#endif
ITEMIDLIST *idl = SHBrowseForFolder(m_pBROWSEINFO);
if (idl) // SHBrowseForFolder returned successfully
{
CString Buffer = _T("");
BOOL result = SHGetPathFromIDList(idl, Buffer.GetBuffer(_MAX_PATH)); // get path string from ITEMIDLIST
Buffer.ReleaseBuffer();
if (result)
{
if (GetFlags() & FEC_TRAILINGSLASH) // add a trailing slash if it is not already there
{
if (_T('\\') != Buffer[Buffer.GetLength() - 1])
Buffer+= _T("\\");
}
LRESULT retval = 0;
CWnd *pParent = GetParent();
if (IsWindow(pParent))
{ // notify parent window that the control text is about to be updated
FEC_NOTIFY notify(this, FEC_NM_POSTBROWSE);
notify.pNewText = &Buffer;
retval = pParent->SendMessage (WM_NOTIFY, (WPARAM)GetDlgCtrlID(), (LPARAM)¬ify);
}
if (0 == retval)
{
SetWindowText(Buffer); // update edit control text
bReturnValue = TRUE;
}
}
LPMALLOC lpm;
if (SHGetMalloc(&lpm) == NOERROR)
lpm->Free(idl); // free memory returned by SHBrowseForFolder
}
else
{ // notify parent window that ShBrowseForFolder was cancelled
CWnd *pParent = GetParent();
if (IsWindow(pParent))
{
FEC_NOTIFY notify(this, FEC_NM_POSTBROWSE);
pParent->SendMessage (WM_NOTIFY, (WPARAM)GetDlgCtrlID(), (LPARAM)¬ify);
}
}
SetFocus(); // ensure focus returns to this control
return bReturnValue;
}
/////////////////////////////////////////////////////////////////////////////
//
// FECFolderProc (Global CALLBACK function)
// This is the default callback procedure for the SHBrowseForFolder function.
// It sets the initial selection to the directory specified in the edit control
//
// Parameters :
// hwnd [in] - Handle of the SHBrowseForFolder dialog
// nMsg [in] - Message to be handled
// lpData [in] - the lparam member of the BROWSEINFO structure, must be a pointer
// to the CFileEditCtrl
//
// Returns :
// zero
//
// Note :
// See 'SHBrowseForFolder' in MSDN for more info on the callback function for SHBrowseForFolder()
//
/////////////////////////////////////////////////////////////////////////////
int CALLBACK FECFolderProc(HWND hWnd, UINT nMsg, LPARAM, LPARAM lpData)
{
if (BFFM_INITIALIZED == nMsg)
{ // ensure we are passed a pointer to the CFileEditCtrl
CWnd *pWnd = (CWnd *)lpData;
ASSERT (pWnd->IsKindOf(RUNTIME_CLASS(CFileEditCtrl)));
CFileEditCtrl* pCFEC = (CFileEditCtrl *)pWnd;
CString szPath;
POSITION pos = pCFEC->GetStartPosition();
if (pos)
{
szPath = pCFEC->GetNextPathName(pos);
if (_T("\\\\") != szPath.Left(2)) // SHBrowseForFolder does not like UNC path
{
int len = szPath.GetLength() - 1;
if (2 != len && _T('\\') == szPath[len])
szPath.Delete(len); // remove trailing slash (SHBrowseForFolder does not like it)
::SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)(LPCTSTR)szPath);
}
}
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::FECOpenFile (protected member function)
// Set up the CFECFileDialog and call CFECFileDialog::DoModal().
// Update the control to the users selection
//
// Parameters :
// None
//
// Returns :
// TRUE if the user clicked the OK button in the CFileDialog
// FALSE otherwise
//
/////////////////////////////////////////////////////////////////////////////
BOOL CFileEditCtrl::FECOpenFile()
{
CFileDialog dlgOpen(true,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
m_strFilter,NULL);
dlgOpen.m_ofn.lpstrTitle = m_szCaption;//标题条
/*if (bOpen)
m_pCFileDialog->m_ofn.Flags |= OFN_FILEMUSTEXIST;
else
m_pCFileDialog->m_ofn.Flags |= OFN_OVERWRITEPROMPT;*/
if (m_dwFlags & FEC_MULTIPLE)
dlgOpen.m_ofn.Flags |= OFN_ALLOWMULTISELECT;
else
dlgOpen.m_ofn.Flags &= ~OFN_ALLOWMULTISELECT;
if (m_dwFlags & FEC_NODEREFERENCELINKS)
dlgOpen.m_ofn.Flags |= OFN_NODEREFERENCELINKS;
else
dlgOpen.m_ofn.Flags &= ~OFN_NODEREFERENCELINKS;
BOOL bReturnValue = FALSE;
BOOL bDirectory = TRUE; // assume user of this class has set the initial directory
TCHAR lpstrDirectory[_MAX_PATH] = _T("");
if (NULL == dlgOpen.m_ofn.lpstrInitialDir) // user has not set the initial directory
{ // flag it, and set the initial directory
bDirectory = FALSE; // to the directory in the edit control
POSITION pos = GetStartPosition();
if (pos)
{
_tcscpy(lpstrDirectory, GetNextPathName(pos));
DWORD attrib = GetFileAttributes(lpstrDirectory);
if (((0xFFFFFFFF != attrib) && (!(attrib & FILE_ATTRIBUTE_DIRECTORY)))
|| ((0xFFFFFFFF == attrib) && (!(dlgOpen.m_ofn.Flags & OFN_FILEMUSTEXIST))))
// if ((file exists && is not a folder) || (does not exist && does not have to exist))
{
// set the filename editbox in CFileDialog to the name of the
// first file in the control
TCHAR Name[_MAX_FNAME];
TCHAR Ext[_MAX_EXT];
_tsplitpath(lpstrDirectory, NULL, NULL, Name, Ext);
_tcscat(Name, Ext);
_tcscpy(dlgOpen.m_ofn.lpstrFile, Name);
}
else
// empty the filename edit box
_tcscpy(dlgOpen.m_ofn.lpstrFile, _T(""));
// Start browsing in the correct folder
GetValidFolder(lpstrDirectory);
dlgOpen.m_ofn.lpstrInitialDir = lpstrDirectory;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -