⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmaloglistbox.cpp

📁 日志表控件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// ==========================================================================
// MMALogListBox.cpp
// 
// Author : Mike Marquet
//          mike.marquet@altavista.net
//
// Last Modified : 27/01/2000
// by            : Mike marquet
// ==========================================================================

// ==========================================================================
// Les Includes
// ==========================================================================

#include "stdafx.h"
#include <crtdbg.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <commdlg.h>
#include <math.h>
#include "MMALogListBox.h"
#include "API\Debug.h"

// ==========================================================================
// Les Defines
// ==========================================================================

#define LOGLBSTRING_INFO            "INFO"
#define LOGLBSTRING_WARNING         "WARNING"
#define LOGLBSTRING_ERROR           "ERROR"
#define LOGLBSTRING_FATAL           "FATAL"
#define LOGLBSTRING_SYSTEM          "SYSTEM"
#define LOGLBSTRING_DEBUG1          "DEBUG1"
#define LOGLBSTRING_DEBUG2          "DEBUG2"
#define LOGLBSTRING_DEBUG3          "DEBUG3"
#define LOGLBSTRING_DEBUG4          "DEBUG4"
#define LOGLBSTRING_TRACE           "TRACE"

#define MMALOGLISTBOX_VERSION       0x00000001

#define MMALOGBOOKDLG_LEFTMARGIN    10
#define MMALOGBOOKDLG_RIGHTMARGIN   10
#define MMALOGBOOKDLG_TOPMARGIN     10
#define MMALOGBOOKDLG_BOTTOMMARGIN  10

// ==========================================================================
// Les Variables Globales
// ==========================================================================

CMMALogListBox *g_pcMMALogListBox = NULL;

// ==========================================================================
// Les Fonctions HOOK
// ==========================================================================
#ifdef _DEBUG

int MMALogListBoxReportingHook(int iReportType, char *szUserMsg, int *piRetValue)
 {
  switch(iReportType)
   {
    case _CRT_WARN :
                     if (g_pcMMALogListBox)
                      {
                       if (szUserMsg[strlen(szUserMsg) - 1] == '\n') szUserMsg[strlen(szUserMsg) - 1] = 0;
                       if (szUserMsg[strlen(szUserMsg) - 1] == '\r') szUserMsg[strlen(szUserMsg) - 1] = 0;
                       if (szUserMsg[strlen(szUserMsg) - 1] == '\n') szUserMsg[strlen(szUserMsg) - 1] = 0;

                       g_pcMMALogListBox->AddLogLine(szUserMsg, LOGLB_TRACE);
                      }
                     break;
   }

  *piRetValue = 0;

  return 0;
 }

#endif

// ==========================================================================
// Les Fonctions
// ==========================================================================

void MMALogListBoxWndProc_DrawItemLine(HDC hDC, LPDRAWITEMSTRUCT lpdis)
 {
  if (lpdis->itemID == (UINT)-1) return;

  MMALOGLISTBOX_USERDATA *pstUserData = CMMALogListBox::GetUserData(lpdis->hwndItem);

  if (!pstUserData) return;

  LINEDATA_MEMORY *pstAllocatedLDM = (LINEDATA_MEMORY *)lpdis->itemData;
  int              iItemHeight     = lpdis->rcItem.bottom - lpdis->rcItem.top;
  HFONT            hOldFont        = NULL;
  int              iOldMode;
  COLORREF         clrOldText;
  HBRUSH           hBrush;
  RECT             stTimeRect;
  RECT             stTypeRect;
  RECT             stTextRect;
  char             szTime[255];
  char             szType[MMALOGLISTBOX_LINEDATA_TYPELEN + 1];
  char             szText[MMALOGLISTBOX_LINEDATA_TEXTLEN + 1];
  int              iTimeWidth;
  int              iTypeWidth;
  int              iTextWidth;
  BOOL             bWidthChange = FALSE;

  if (pstAllocatedLDM == NULL) return;

  // ----- LINE NOT USED -----
  if (pstAllocatedLDM->stLDF.lType == 0)
   {
    if (lpdis->itemState & ODS_SELECTED) hBrush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
    else hBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));

    FillRect(hDC, &lpdis->rcItem, hBrush);

    DeleteObject(hBrush);

    return;
   }

  // ----- APPLY FONT IF NECESSARY -----
  if (pstAllocatedLDM->hFont) hOldFont = (HFONT)SelectObject(hDC, pstAllocatedLDM->hFont);

  // ----- GET ALL STRINGS -----
  strcpy(szTime, CMMALogListBox::GetStringTime(pstAllocatedLDM->stLDF.stST));
  strcpy(szType, CMMALogListBox::GetStringType(lpdis->hwndItem,pstAllocatedLDM->stLDF.lType));
  strcpy(szText, pstAllocatedLDM->stLDF.szText);

  iTimeWidth = CMMALogListBox::GetExtentForString(lpdis->hwndItem,pstAllocatedLDM->hFont,szTime);
  iTypeWidth = CMMALogListBox::GetExtentForString(lpdis->hwndItem,pstAllocatedLDM->hFont,CMMALogListBox::GetLengthlyType(lpdis->hwndItem));
  iTextWidth = CMMALogListBox::GetExtentForString(lpdis->hwndItem,pstAllocatedLDM->hFont,szText);

  // ----- CHECK WIDTH -----
  if (pstUserData->iTimeColumnSize < iTimeWidth)
   {
    pstUserData->iTimeColumnSize = iTimeWidth;
    bWidthChange                     = TRUE;
   }

  if (pstUserData->iTypeColumnSize < iTypeWidth)
   {
    pstUserData->iTypeColumnSize = iTypeWidth;
    bWidthChange                     = TRUE;
   }

  if (bWidthChange == TRUE)
   {
    InvalidateRect(lpdis->hwndItem, NULL, NULL);
    return;
   }

  // ----- TIME RECTANGLE -----
  stTimeRect.left   = lpdis->rcItem.left;
  stTimeRect.top    = lpdis->rcItem.top;
  stTimeRect.right  = lpdis->rcItem.left + pstUserData->iTimeColumnSize;
  stTimeRect.bottom = lpdis->rcItem.bottom;

  // ----- TYPE RECTANGLE -----
  stTypeRect.left   = stTimeRect.right;
  stTypeRect.top    = stTimeRect.top;
  stTypeRect.right  = stTimeRect.right + pstUserData->iTypeColumnSize;
  stTypeRect.bottom = stTimeRect.bottom;

  // ----- TEXT RECTANGLE -----
  stTextRect.left   = stTypeRect.right;
  stTextRect.top    = stTypeRect.top;
  stTextRect.right  = lpdis->rcItem.right;
  stTextRect.bottom = stTypeRect.bottom;

  // ----- BACKGROUND MODE -----
  iOldMode = SetBkMode(hDC, TRANSPARENT);

  // ----- TIME BACKGROUND-----
  if (lpdis->itemState & ODS_SELECTED) hBrush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
  else if (pstAllocatedLDM->stLDF.bNewLineData) hBrush = CreateSolidBrush(GetNearestColor(hDC,RGB(210,210,210)));
       else if (pstAllocatedLDM->stLDF.stTimeColors.bUseBkColor) hBrush = CreateSolidBrush(pstAllocatedLDM->stLDF.stTimeColors.clrBk);
            else hBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));

  FillRect(hDC, &stTimeRect, hBrush);

  DeleteObject(hBrush);

  // ----- TYPE BACKGROUND -----
  if (lpdis->itemState & ODS_SELECTED) hBrush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
  else if (pstAllocatedLDM->stLDF.lType == LOGLB_DEBUG1) hBrush = CreateSolidBrush(pstUserData->clrBkDebug[0]);
       else if (pstAllocatedLDM->stLDF.lType == LOGLB_DEBUG2) hBrush = CreateSolidBrush(pstUserData->clrBkDebug[1]);
            else if (pstAllocatedLDM->stLDF.lType == LOGLB_DEBUG3) hBrush = CreateSolidBrush(pstUserData->clrBkDebug[2]);
                 else if (pstAllocatedLDM->stLDF.lType == LOGLB_DEBUG4) hBrush = CreateSolidBrush(pstUserData->clrBkDebug[3]);
                      else if (pstAllocatedLDM->stLDF.stTypeColors.bUseBkColor) hBrush = CreateSolidBrush(pstAllocatedLDM->stLDF.stTypeColors.clrBk);
                           else hBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));

  FillRect(hDC, &stTypeRect, hBrush);

  DeleteObject(hBrush);

  // ----- TEXT BACKGROUND -----
  if (lpdis->itemState & ODS_SELECTED) hBrush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
  else if (pstAllocatedLDM->stLDF.stTextColors.bUseBkColor) hBrush = CreateSolidBrush(pstAllocatedLDM->stLDF.stTextColors.clrBk);
       else hBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));

  FillRect(hDC, &stTextRect, hBrush);

  DeleteObject(hBrush);

  // ----- DRAW COLUMNS -----
  MoveToEx(hDC, stTimeRect.right, stTimeRect.top, NULL);
  LineTo(hDC, stTimeRect.right, stTimeRect.bottom);

  MoveToEx(hDC, stTypeRect.right, stTypeRect.top, NULL);
  LineTo(hDC, stTypeRect.right, stTypeRect.bottom);

  // ----- SEPARATOR -----
  if (CMMALogListBox::IsSeparatorLine(pstAllocatedLDM->stLDF.lType))
   {
    HPEN hPen;

    switch(CMMALogListBox::IsSeparatorLine(pstAllocatedLDM->stLDF.lType))
     {
      case LOGLB_DBLSEPARATOR        : hPen = CreatePen(PS_SOLID     , 3, RGB(0,0,0)); break;
      case LOGLB_DASHSEPARATOR       : hPen = CreatePen(PS_DASH      , 1, RGB(0,0,0)); break;
      case LOGLB_DOTSEPARATOR        : hPen = CreatePen(PS_DOT       , 1, RGB(0,0,0)); break;
      case LOGLB_DASHDOTSEPARATOR    : hPen = CreatePen(PS_DASHDOT   , 1, RGB(0,0,0)); break;
      case LOGLB_DASHDOTDOTSEPARATOR : hPen = CreatePen(PS_DASHDOTDOT, 1, RGB(0,0,0)); break;

      default                        : hPen = CreatePen(PS_SOLID     , 1, RGB(0,0,0)); break;
     }

    HPEN hOldPen = (HPEN)SelectObject(hDC, hPen);

    MoveToEx(hDC, lpdis->rcItem.left, lpdis->rcItem.top + iItemHeight / 2, NULL);
    LineTo(hDC, lpdis->rcItem.right, lpdis->rcItem.top + iItemHeight / 2);

    SelectObject(hDC, hOldPen);

    DeleteObject(hPen);
   }
  else {
        // ----- TIME FOREGROUND + TEXT
        clrOldText = (lpdis->itemState & ODS_SELECTED) ? SetTextColor(hDC, RGB(255,255,255)) : SetTextColor(hDC, RGB(0,0,0));

        if (pstAllocatedLDM->stLDF.stTimeColors.bUseTextColor) clrOldText = SetTextColor(hDC, pstAllocatedLDM->stLDF.stTimeColors.clrText);

        stTimeRect.left += 2;

        if (!(pstAllocatedLDM->stLDF.lType & LOGLB_NOTIME))
         DrawText(hDC, szTime, strlen(szTime), &stTimeRect, DT_LEFT);

        // ----- TYPE FOREGROUND + TEXT -----
        clrOldText = (lpdis->itemState & ODS_SELECTED) ? SetTextColor(hDC, RGB(255,255,255)) : SetTextColor(hDC, RGB(0,0,0));

        if (pstAllocatedLDM->stLDF.stTypeColors.bUseTextColor) clrOldText = SetTextColor(hDC, pstAllocatedLDM->stLDF.stTypeColors.clrText);

        if (pstAllocatedLDM->stLDF.lType == LOGLB_DEBUG1) clrOldText = SetTextColor(hDC, pstUserData->clrTextDebug[0]);
        else if (pstAllocatedLDM->stLDF.lType == LOGLB_DEBUG2) clrOldText = SetTextColor(hDC, pstUserData->clrTextDebug[1]);
             else if (pstAllocatedLDM->stLDF.lType == LOGLB_DEBUG3) clrOldText = SetTextColor(hDC, pstUserData->clrTextDebug[2]);
                  else if (pstAllocatedLDM->stLDF.lType == LOGLB_DEBUG4) clrOldText = SetTextColor(hDC, pstUserData->clrTextDebug[3]);

        stTypeRect.left += 2;

        if (!(pstAllocatedLDM->stLDF.lType & LOGLB_NOTYPE))
         DrawText(hDC, szType, strlen(szType), &stTypeRect, DT_LEFT);

        // ----- TEXT FOREGROUND + TEXT -----
        clrOldText = (lpdis->itemState & ODS_SELECTED) ? SetTextColor(hDC, RGB(255,255,255)) : SetTextColor(hDC, RGB(0,0,0));

        if (pstAllocatedLDM->stLDF.stTextColors.bUseTextColor) clrOldText = SetTextColor(hDC, pstAllocatedLDM->stLDF.stTextColors.clrText);

        stTextRect.left += 2;

        DrawText(hDC, szText, strlen(szText), &stTextRect, DT_LEFT);
       }

  // ----- RESTORE OLD FONT -----
  if (pstAllocatedLDM->hFont) SelectObject(hDC, hOldFont);

  // ---- RESTORE OLD BACKGROUND MODE -----
  SetTextColor(hDC, clrOldText);

  SetBkMode(hDC, iOldMode);
 }

// ==========================================================================
// Les Fonctions Relatives aux messages de la ListBox
// ==========================================================================

BOOL MMALogListBoxWndProc_Destroy(HWND hwnd)
 {
  //TRACE("(...) MMALogListBoxWndProc_Destroy(hwnd = 0x%X)\n", hwnd);

  CMMALogListBox::ResetListBoxContent(hwnd);

  MMALOGLISTBOX_USERDATA *pstUserData = CMMALogListBox::GetUserData(hwnd);

  if (pstUserData)
   {
    delete pstUserData;
    pstUserData = NULL;
   }

  CMMALogListBox::SetUserData(hwnd, NULL);

  return FALSE;
 }

// --------------------------------------------------------------------------

void MMALogListBoxWndProc_DrawItem(LPDRAWITEMSTRUCT lpdis)
 {
  if (lpdis->CtlType != ODT_LISTBOX) return;

  HDC hDC     = lpdis->hDC;
  int iSaveDC = SaveDC(hDC);

  if (lpdis->itemAction == ODA_DRAWENTIRE)
   {
    MMALogListBoxWndProc_DrawItemLine(hDC, lpdis);
   }

  if (lpdis->itemAction == ODA_SELECT)
   {
    MMALogListBoxWndProc_DrawItemLine(hDC, lpdis);
   }

  if (lpdis->itemAction == ODA_FOCUS)
   {
    DrawFocusRect(hDC, &lpdis->rcItem);
   }

  // Restore DC
  RestoreDC(hDC, iSaveDC);
 }

// --------------------------------------------------------------------------

void MMALogListBoxWndProc_MeasureItem(LPMEASUREITEMSTRUCT lpmis)
 {
  if (lpmis->CtlType != ODT_LISTBOX) return;

  if (lpmis->itemID == (UINT)-1) return;

  LINEDATA_MEMORY *pstAllocatedLDM = (LINEDATA_MEMORY *)lpmis->itemData;

  if (!pstAllocatedLDM) return;

  int iLineCount = CMMALogListBox::GetLineCount(pstAllocatedLDM->stLDF.szText);

  if (!pstAllocatedLDM->hFont)
   {
    lpmis->itemHeight = lpmis->itemHeight * iLineCount;    
    return;
   }

  LOGFONT stLF;

  GetObject(pstAllocatedLDM->hFont, sizeof(stLF), &stLF);

  unsigned int iHeight = (stLF.lfHeight >= 0) ? stLF.lfHeight : stLF.lfHeight * -1;

  if (iHeight < lpmis->itemHeight) lpmis->itemHeight = lpmis->itemHeight * iLineCount;
  else lpmis->itemHeight = iHeight * iLineCount;
 }

// --------------------------------------------------------------------------

BOOL MMALogListBoxWndProc_LButtonDblClk(HWND hwnd, WPARAM fwKeys, WORD xPos, WORD yPos)
 {
  int iCount = SendMessage(hwnd, LB_GETCOUNT, 0L, 0L);

  SendMessage(hwnd, LB_SETCURSEL, (WPARAM)(iCount - 1), 0L);

  return FALSE;
 }

// --------------------------------------------------------------------------

BOOL MMALogListBoxWndProc_RButtonDblClk(HWND hwnd, WPARAM fwKeys, WORD xPos, WORD yPos)
 {
  SendMessage(hwnd, LB_SETCURSEL, (WPARAM)0, 0L);

  return FALSE;
 }

// --------------------------------------------------------------------------

LRESULT CALLBACK MMALogListBoxWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
  WNDPROC pOldWndProc = CMMALogListBox::GetOldWndProc(hwnd);

  if (message == WM_DESTROY && MMALogListBoxWndProc_Destroy(hwnd)) return 0L;

  else if (message == WM_LBUTTONDBLCLK && MMALogListBoxWndProc_LButtonDblClk(hwnd, wParam, LOWORD(lParam), HIWORD(lParam))) return 0L;

  else if (message == WM_RBUTTONDBLCLK && MMALogListBoxWndProc_RButtonDblClk(hwnd, wParam, LOWORD(lParam), HIWORD(lParam))) return 0L;

  if (pOldWndProc) return CallWindowProc(pOldWndProc, hwnd, message, wParam, lParam);

  return DefWindowProc(hwnd, message, wParam, lParam);
 }


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -