📄 logeditctrl.cpp
字号:
if (iTextLen + iLineLen > m_iMaxLogBuff)
{
// delete the 1st 10 lines; freeing up only 1 or 2 lines is still not enough (peformance problem)
int iLine0Len = 0;
for (int i = 0; i < 10; i++){
int iLineLen = LineLength(iLine0Len);
if (iLineLen == 0)
break;
iLine0Len += iLineLen + 2;
}
bOldNoPaint = m_bNoPaint;
m_bNoPaint = true;
bRestorePaintFlag = true;
bIsVisible = IsWindowVisible();
if (bIsVisible)
SetRedraw(FALSE);
SetSel(0, iLine0Len, TRUE);
ReplaceSel(_T(""));
// update any possible available selection
iStartChar -= iLine0Len;
if (iStartChar < 0)
iStartChar = 0;
iEndChar -= iLine0Len;
if (iEndChar < 0)
iEndChar = 0;
nPos -= iLine0Len;
if (nPos < 0)
nPos = 0;
}
m_bEnErrSpace = false;
SetSel(nPos, nPos, TRUE);
ReplaceSel(pszLine);
if (bRestorePaintFlag)
{
m_bNoPaint = bOldNoPaint;
if (bIsVisible && !m_bNoPaint){
SetRedraw();
if (m_bRichEdit)
Invalidate();
}
}
if (m_bEnErrSpace)
{
// following code works properly, but there is a performance problem. if the control
// starts to rotate the text and if there is much to log, CPU usage hits the roof.
// to get around this, we try to free the needed space for the current line (and more)
// before adding the line (see code above). actually, the following code should not be
// executed any longer, but is kept for fail safe handling.
bool bOldNoPaint = m_bNoPaint;
m_bNoPaint = true;
BOOL bIsVisible = IsWindowVisible();
if (bIsVisible)
SetRedraw(FALSE);
// remove the first line as long as we are capable of adding the new line
int iSafetyCounter = 0;
while (m_bEnErrSpace && iSafetyCounter < 10)
{
// delete the previous partially added line
SetSel(nPos, -1, TRUE);
ReplaceSel(_T(""));
// delete 1st line
int iLine0Len = LineLength(0) + 2; // add NL character
SetSel(0, iLine0Len, TRUE);
ReplaceSel(_T(""));
// update any possible available selection
iStartChar -= iLine0Len;
if (iStartChar < 0)
iStartChar = 0;
iEndChar -= iLine0Len;
if (iEndChar < 0)
iEndChar = 0;
// add the new line again
nPos = GetWindowTextLength();
SetSel(nPos, nPos, TRUE);
m_bEnErrSpace = false;
ReplaceSel(pszLine);
if (m_bEnErrSpace && nPos == 0){
// should never happen: if we tried to add the line another time in the 1st line, there
// will be no chance to add the line at all -> avoid endless loop!
break;
}
iSafetyCounter++; // never ever create an endless loop!
}
m_bNoPaint = bOldNoPaint;
if (bIsVisible && !m_bNoPaint){
SetRedraw();
if (m_bRichEdit)
Invalidate();
}
}
}
void CLogEditCtrl::Reset(){
m_astrBuff.RemoveAll();
SetRedraw(FALSE);
SetWindowText(_T(""));
SetRedraw();
if (m_bRichEdit)
Invalidate();
}
void CLogEditCtrl::OnContextMenu(CWnd* pWnd, CPoint point){
int iSelStart, iSelEnd;
GetSel(iSelStart, iSelEnd);
int iTextLen = GetWindowTextLength();
m_LogMenu.EnableMenuItem(MP_COPYSELECTED, iSelEnd > iSelStart ? MF_ENABLED : MF_GRAYED);
m_LogMenu.EnableMenuItem(MP_REMOVEALL, iTextLen > 0 ? MF_ENABLED : MF_GRAYED);
m_LogMenu.EnableMenuItem(MP_SELECTALL, iTextLen > 0 ? MF_ENABLED : MF_GRAYED);
m_LogMenu.EnableMenuItem(MP_SAVELOG, iTextLen > 0 ? MF_ENABLED : MF_GRAYED);
m_LogMenu.CheckMenuItem(MP_AUTOSCROLL, m_bAutoScroll ? MF_CHECKED : MF_UNCHECKED);
if (point.x == -1 && point.y == -1){
point.x = 16;
point.y = 32;
ClientToScreen(&point);
}
m_LogMenu.TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, point.x, point.y, this);
}
BOOL CLogEditCtrl::OnCommand(WPARAM wParam, LPARAM lParam){
switch (wParam) {
case MP_COPYSELECTED:
CopySelectedItems();
break;
case MP_SELECTALL:
SelectAllItems();
break;
case MP_REMOVEALL:
Reset();
break;
case MP_SAVELOG:
SaveLog();
break;
case MP_AUTOSCROLL:
m_bAutoScroll = !m_bAutoScroll;
break;
}
return TRUE;
}
bool CLogEditCtrl::SaveLog(LPCTSTR pszDefName)
{
bool bResult = false;
CFileDialog dlg(FALSE, _T("log"), pszDefName ? pszDefName : (LPCTSTR)m_strTitle, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Log Files (*.log)|*.log||"), this, 0);
if (dlg.DoModal() == IDOK)
{
FILE* fp = _tfsopen(dlg.GetPathName(), _T("wb"), _SH_DENYWR);
if (fp)
{
#ifdef _UNICODE
// write Unicode byte-order mark 0xFEFF
fputwc(0xFEFF, fp);
#endif
CString strText;
GetWindowText(strText);
fwrite(strText, sizeof(TCHAR), strText.GetLength(), fp);
if (ferror(fp)){
CString strError;
strError.Format(_T("Failed to write log file \"%s\" - %s"), dlg.GetPathName(), strerror(errno));
AfxMessageBox(strError, MB_ICONERROR);
}
else
bResult = true;
fclose(fp);
}
else{
CString strError;
strError.Format(_T("Failed to create log file \"%s\" - %s"), dlg.GetPathName(), strerror(errno));
AfxMessageBox(strError, MB_ICONERROR);
}
}
return bResult;
}
CString CLogEditCtrl::GetLastLogEntry(){
CString strLog;
int iLastLine = GetLineCount() - 2;
if (iLastLine >= 0){
GetLine(iLastLine, strLog.GetBuffer(1024), 1024);
strLog.ReleaseBuffer();
}
return strLog;
}
CString CLogEditCtrl::GetAllLogEntries(){
CString strLog;
GetWindowText(strLog);
return strLog;
}
void CLogEditCtrl::SelectAllItems()
{
SetSel(0, -1, TRUE);
}
void CLogEditCtrl::CopySelectedItems()
{
Copy();
}
void CLogEditCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == 'A' && (GetKeyState(VK_CONTROL) & 0x8000))
{
//////////////////////////////////////////////////////////////////
// Ctrl+A: Select all items
SelectAllItems();
}
else if (nChar == 'C' && (GetKeyState(VK_CONTROL) & 0x8000))
{
//////////////////////////////////////////////////////////////////
// Ctrl+C: Copy listview items to clipboard
CopySelectedItems();
}
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
HBRUSH CLogEditCtrl::CtlColor(CDC* pDC, UINT nCtlColor)
{
if (m_crForeground != CLR_DEFAULT)
pDC->SetTextColor(m_crForeground);
else
{
// explicitly set the (default) text color -- needed for some contrast window color schemes
pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
}
if (m_crBackground != CLR_DEFAULT && m_brBackground.m_hObject != NULL)
{
pDC->SetBkColor(m_crBackground);
return m_brBackground;
}
else
{
pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
return GetSysColorBrush(COLOR_WINDOW);
}
}
void CLogEditCtrl::OnSysColorChange()
{
CEdit::OnSysColorChange();
ApplySkin();
}
void CLogEditCtrl::ApplySkin()
{
if (!m_strSkinKey.IsEmpty())
{
COLORREF cr;
if (theApp.LoadSkinColor(m_strSkinKey + _T("Fg"), cr))
m_crForeground = cr;
else
m_crForeground = CLR_DEFAULT;
if (theApp.LoadSkinColor(m_strSkinKey + _T("Bk"), cr))
{
m_crBackground = cr;
m_brBackground.DeleteObject();
m_brBackground.CreateSolidBrush(m_crBackground);
}
else
{
m_crBackground = CLR_DEFAULT;
m_brBackground.DeleteObject();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -