📄 fileeditctrl.cpp
字号:
// Message Map
BEGIN_MESSAGE_MAP(CFileEditCtrl, CEdit)
ON_CONTROL_REFLECT_EX(EN_CHANGE, OnChange)
ON_MESSAGE(EM_SETREADONLY, OnSetReadOnly)
ON_MESSAGE(WM_SETTEXT, OnSetText)
ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnTTNNeedText)
ON_WM_DROPFILES()
ON_WM_ENABLE()
ON_WM_KEYDOWN()
ON_WM_KILLFOCUS()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_NCCALCSIZE()
ON_WM_NCHITTEST()
ON_WM_NCLBUTTONDBLCLK()
ON_WM_NCLBUTTONDOWN()
ON_WM_NCMOUSEMOVE()
ON_WM_NCPAINT()
ON_WM_SETFOCUS()
ON_WM_SIZE()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileEditCtrl member functions
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::AddFile (protected member function)
// Adds the specified file to the m_Files list. Removes the path info
// if it is the same as the path in m_szFolder
//
// Parameters :
// FileName [in] - The file to add
//
// Returns :
// Nothing
//
/////////////////////////////////////////////////////////////////////////////
void CFileEditCtrl::AddFile(CString File)
{
DWORD Flags = GetFlags();
if (Flags & FEC_FILE && !(Flags & FEC_NODEREFERENCELINKS))
{ // resolve shortcuts (*.lnk or *.pif)
CString Ext = File.Right(4);
Ext.MakeLower();
if (_T(".lnk") == Ext || _T(".pif") == Ext)
DereferenceLink(File);
}
int FolderLength = m_szFolder.GetLength();
if (File.Left(FolderLength) == m_szFolder)
File.Delete(0, FolderLength);
m_Files.AddTail(File);
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::ButtonClicked (protected member function)
// Called when the user clicks on the browse button.
//
// Parameters :
// None
//
// Returns :
// Nothing
//
// Note :
// Sends a WM_NOTIFY message to the parent window both before and after the dialogs have run.
// The NMHDR parameter points to a FEC_NOTIFY structure.
//
// Before : Sends the FEC_NM_PREBROWSE notification. Returning non-zero aborts this function.
// After : Sends the FEC_NM_POSTBROWSE notification. (update Mar 28, 2002 - now sent by called functions)
//
/////////////////////////////////////////////////////////////////////////////
void CFileEditCtrl::ButtonClicked()
{
CWnd *pParent = GetParent();
if (IsWindow(pParent))
{
FEC_NOTIFY notify(this, FEC_NM_PREBROWSE);
if (pParent->SendMessage (WM_NOTIFY, (WPARAM)GetDlgCtrlID(), (LPARAM)¬ify) != 0)
return; // SendMessage returned nonzero, do not show dialog
}
DWORD Flags = GetFlags();
if (Flags & FEC_FOLDER)
FECBrowseForFolder();
else if (Flags & (FEC_FILEOPEN | FEC_FILESAVEAS))
FECOpenFile();
else
ASSERT (FALSE); // control flags not properly set (should never get here)
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::Create (public member function)
// Creates the CFileEditCtrl in any window.
//
// Parameters :
// dwFlags [in] - CFileEditCtrl flags ( FEC_* )
// dwExStyle [in] - Windows extended styles ( WS_EX_* )
// lpszWindowName [in] - The initial text in the control
// dwStyle [in] - Windows and Edit control styles ( WS_* and ES_* )
// rect [in] - The position and size of the control
// pParentWnd [in] - Pointer to the control's parent window
// nID [in] - the control's ID
//
// Returns :
// TRUE if the control was successfully created
// FALSE if not
//
// Note :
// See the FileEditCtrl.h file for descriptions of the flags used
//
/////////////////////////////////////////////////////////////////////////////
BOOL CFileEditCtrl::Create(DWORD dwFlags,
DWORD dwExStyle,
LPCTSTR lpszWindowName,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID)
{
BOOL bResult = CreateEx(dwExStyle,
_T("EDIT"),
lpszWindowName,
dwStyle,
rect,
pParentWnd,
nID);
if (bResult)
{ // call CFileEditCtrl::SetFlags() to initialize the internal data structures
bResult = SetFlags(dwFlags);
if (bResult)
{ // set the font to the font used by the parent window
if (IsWindow(pParentWnd))
SetFont(pParentWnd->GetFont());
}
else // SetFlags() failed - destroy the window
DestroyWindow();
}
return bResult;
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::DereferenceLinks (protected member function)
// Gets the file path name pointed to by shortcut (*.lnk or *.pif) file
//
// Parameters :
// FileName [in] : the shortcut file to be dereferenced
// [out] : if successful, the complete path name of the file the
// shortcut points to, or unchanged if not successful
//
// Returns :
// TRUE if the link was dereferenced
// FALSE if not
//
// Note :
// Thanks to Michael Dunn for his article "Introduction to COM - What It Is and How to Use It."
// found at http://www.codeproject.com/com/comintro.asp
//
/////////////////////////////////////////////////////////////////////////////
BOOL CFileEditCtrl::DereferenceLink(CString &FileName)
{
BOOL ret = FALSE; // assume failure
IShellLink *pIShellLink;
IPersistFile *pIPersistFile;
// initialize the COM libraries
CoInitialize (NULL);
// create an IShellLink object
HRESULT hr = CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(void **) &pIShellLink);
if (SUCCEEDED (hr))
{
// get the IPersistFile interface for this IShellLink object
hr = pIShellLink->QueryInterface(IID_IPersistFile, (void **)&pIPersistFile);
if (SUCCEEDED (hr))
{
int len = FileName.GetLength();
WCHAR *pWFile = new WCHAR[len + 1];
USES_CONVERSION;
wcscpy(pWFile, T2COLE(FileName));
// open and read the .lnk or .pif file
hr = pIPersistFile->Load(pWFile, 0);
if (SUCCEEDED(hr))
{
TCHAR buffer[_MAX_PATH];
// get the file path name
hr = pIShellLink->GetPath(buffer, _MAX_PATH, NULL, 0 /*SLGP_UNCPRIORITY*/);
if (SUCCEEDED(hr))
{
FileName = buffer;
ret = TRUE;
}
}
delete[] pWFile;
}
// release the IShellLink interface
pIShellLink->Release();
}
// close the COM libraries
CoUninitialize();
return ret;
}
/////////////////////////////////////////////////////////////////////////////
//
// CFileEditCtrl::DrawButton (protected member function)
// Draws the button on the control
//
// Parameters :
// nButtonState [in] - the state of the button ( Up, Down, or Disabled )
//
// Returns :
// Nothing
//
/////////////////////////////////////////////////////////////////////////////
void CFileEditCtrl::DrawButton(int nButtonState)
{ // if the button is too small, do not draw it
if (m_rcButtonRect.Width() < 3 || m_rcButtonRect.Height() < 3)
return;
ASSERT(IsWindow(this));
// if the control is disabled, ensure the button is drawn disabled
if (GetStyle() & WS_DISABLED)
nButtonState = BTN_DISABLED;
nButtonState |= m_nButtonState & BTN_FLAT;
CRect DrawRect(m_rcButtonRect);
DrawRect.OffsetRect(0 - m_rcButtonRect.left, 0 - m_rcButtonRect.top);
CWindowDC WndDC(this); // get the DC for drawing
// Create a memory DC to prevent flicker
CDC MemDC;
MemDC.CreateCompatibleDC(&WndDC);
int savedDC = MemDC.SaveDC();
CBitmap Bitmap;
Bitmap.CreateCompatibleBitmap(&WndDC, m_rcButtonRect.Width(), m_rcButtonRect.Height());
MemDC.SelectObject(&Bitmap);
// use HS_DIAGCROSS pattern brush to test brush alignment
CBrush theBrush(/*HS_DIAGCROSS,*/ GetSysColor(COLOR_3DFACE)); // the colour of the button background
CPoint BrushOrg;
CPoint Origin = WndDC.GetBrushOrg();
BrushOrg.x = (BRUSHWIDTH - (m_rcButtonRect.left - Origin.x) % BRUSHWIDTH);
BrushOrg.y = (BRUSHHEIGHT - (m_rcButtonRect.top - Origin.y) % BRUSHHEIGHT);
MemDC.SetBrushOrg(BrushOrg);
MemDC.SelectObject(&theBrush);
MemDC.FillRect(&DrawRect, &theBrush);
if (nButtonState & BTN_DOWN)
{
// draw the border
CPen thePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW));
CPen *pOldPen = MemDC.SelectObject(&thePen);
MemDC.Rectangle(DrawRect);
MemDC.SelectObject(pOldPen);
thePen.DeleteObject();
#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,
3,
3,
DrawRect.Width() - 5,
DrawRect.Height() - 5,
m_dwImageDrawFlags);
}
else // draw the dots
#endif
DrawDots(&MemDC, GetSysColor(COLOR_BTNTEXT), 1);
}
else // draw button as up
{
// draw the border
if (nButtonState & BTN_FLAT)
{
CPen thePen(PS_SOLID, 1, GetSysColor(COLOR_WINDOW));
CPen *pOldPen = MemDC.SelectObject(&thePen);
if (m_rcButtonRect.left < 4) // button on left side of control
MemDC.Rectangle(DrawRect.left - 1, DrawRect.top - 1, DrawRect.right, DrawRect.bottom + 1);
else
MemDC.Rectangle(DrawRect.left, DrawRect.top - 1, DrawRect.right + 1, DrawRect.bottom + 1);
MemDC.SelectObject(pOldPen);
thePen.DeleteObject();
}
else
MemDC.DrawEdge(DrawRect, EDGE_RAISED, BF_RECT);
if (nButtonState & BTN_DISABLED)
{
#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,
PJAI_DISABLED | m_dwImageDrawFlags);
}
else
#endif
{ // draw the dots
DrawDots(&MemDC, GetSysColor(COLOR_3DHILIGHT), 1);
DrawDots(&MemDC, GetSysColor(COLOR_3DSHADOW));
}
}
else if (nButtonState & BTN_UP)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -