Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

ATL/AUX Library

, 17 Mar 2000
A set of VC++ helpers and patterns to help automate some routine coding tasks.
atlaux.zip
Shared
ax_id.zip
ax_idgen.zip
ieclient.zip
IEClient
IEClient.dsw
IEClient.dsp
Release
IEClient.exe
////////////////////////////////////////////////////////////////////////////////
// Generic Log Window class
// Copyright (c) by Andrew Nosenko (andien@geocities.com), 1998
// http://www.geocities.com/~andien/atlaux.htm
// Version: 1.03

#include <Richedit.h>
#include <stdlib.h>

class CLogWindow: 
  public CWindowImpl<CLogWindow>
{
public:
  enum { edit = 100 };
  enum STYLE { plain = 0, topmost = 1, noclose = 2 };

  CLogWindow() {
    m_hwndEdit = 0;
    m_style = plain;
  }

  ~CLogWindow() {
    if ( ::IsWindow(m_hWnd) ) 
      DestroyWindow();
  }

  // override this in derived class if needed.
  virtual void OnDestoryLogWindow() {
  }

// operations
  HRESULT CreateLogWindow(LPCTSTR pszCaption = NULL, UINT style = plain) {
    TCHAR szName[64];
    TCHAR szModule[MAX_PATH];
    GetModuleFileName(_Module.GetModuleInstance(), szModule, MAX_PATH);
    _tsplitpath(szModule, NULL, NULL, szName, NULL);
    TCHAR szCaption[128];
    wsprintf(szCaption, _T("%s:%08X - %s"), 
      szName, GetCurrentThreadId(), pszCaption? pszCaption: _T("Log Window"));

    RECT rcPos = { CW_USEDEFAULT, 0, 0, 0 };
    m_style = style;
    DWORD dwExStyle = 0;
    if ( m_style & topmost ) dwExStyle |= WS_EX_TOPMOST;

    if ( !Create(NULL, rcPos, szCaption, WS_OVERLAPPEDWINDOW, dwExStyle) )
    {
      TRACE(_T("Error creating log window\n"));
      _R_FAIL;
    }
    ShowWindow(SW_SHOW);
    UpdateWindow();
    _R_OK;
  }

  void Log(LPCTSTR pszFormat, ...) {
    TCHAR szBuff[1024];
    va_list arglist;
    va_start(arglist, pszFormat);
    int c = wvsprintf(szBuff, pszFormat, arglist);
    va_end(arglist);

    DWORD dwTextLen = ::Edit_GetTextLength(m_hwndEdit);
    Edit_SetSel(m_hwndEdit, dwTextLen, dwTextLen);
    Edit_ReplaceSel(m_hwndEdit, szBuff);
  }

// implementation
protected:
  HWND m_hwndEdit;
  UINT m_style;

  // msg map
  DECLARE_DEFWNDPROC()
  BEGIN_MSG_MAP(CHost)
    MESSAGE_CRACKER(WM_CREATE, Cls_OnCreate)
    MESSAGE_CRACKER(WM_SIZE, Cls_OnSize)
    MESSAGE_CRACKER(WM_ACTIVATE, Cls_OnActivate)
    MESSAGE_CRACKER(WM_CLOSE, Cls_OnClose)
    MESSAGE_CRACKER(WM_ERASEBKGND, Cls_OnEraseBkgnd)
    MESSAGE_HANDLER(WM_NCDESTROY, OnNCDestroy)
  END_MSG_MAP()

  LRESULT OnNCDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 
  {
    bHandled = FALSE;
    OnDestoryLogWindow();
    LoadRichEdit(FALSE);
    return 0L;
  }

  BOOL Cls_OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct) {
    if ( FAILED(LoadRichEdit()) ) return FALSE;
    m_hwndEdit = ::CreateWindowEx(
      WS_EX_CLIENTEDGE,
      RICHEDIT_CLASS, NULL,
      WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL |
      ES_NOHIDESEL | ES_LEFT | ES_MULTILINE,
      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
      hwnd,
      HMENU(edit), (HMODULE)GetWindowLong(GWL_HINSTANCE),
      NULL
    );
    ASSERT(m_hwndEdit);
    ::SendMessage(m_hwndEdit, EM_SETTEXTMODE, TM_PLAINTEXT, 0);
    ::SendMessage(m_hwndEdit, EM_SETOPTIONS, ECOOP_SET, ECO_READONLY | ECO_AUTOVSCROLL);
    SetWindowFont(m_hwndEdit, GetStockFont(SYSTEM_FIXED_FONT), FALSE);
    return (BOOL)m_hwndEdit;
  }

  void Cls_OnClose(HWND hwnd)
  {
    if ( m_style & noclose ) return;
    FORWARD_WM_CLOSE(hwnd, DefWndProc);
  }

  BOOL Cls_OnEraseBkgnd(HWND hwnd, HDC hdc) {
    return TRUE;
  }

  void Cls_OnSize(HWND hwnd, UINT state, int cx, int cy) {
    ::SetWindowPos(m_hwndEdit, NULL, 0, 0, cx, cy, SWP_NOZORDER | SWP_NOMOVE);
  }

  void Cls_OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized) {
    if ( state ) ::SetFocus(m_hwndEdit);
  }

  static HRESULT LoadRichEdit(BOOL fLoad = TRUE) {
    static const TCHAR szMod[] = _T("riched20.dll");
    if ( fLoad ) {
      if ( !LoadLibrary(szMod) )
        _R( AuxGetLastResult() );
    }
    else 
      FreeLibrary(GetModuleHandle(szMod));
    _R_OK;
  }

};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Andrew Nosenko
Engineer
Australia Australia
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 18 Mar 2000
Article Copyright 2000 by Andrew Nosenko
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid