A Memory Viewer — with a powerful class for any kind of text editor





Click here for larger image

Environment: VC6, NT4, Win2000, Win9x/ME

Abstract

The Memory Viewer displays memory contents starting at a specified address (0x00400000 by default), it shows memory contents as the HEX format in a binary editor.

With this viewer, programmer can watch where Windows runs an application, where is Windows data page, and even where is CMOS information (0x000FF5C0).

It shows the memory contents by bytes and explains bytes to ASCII as much as possible. If a memory page is not readable, it shows ‘?’ instead.

A toolbar at the top of the window displays the starting address for the memory display. Edit the value in the toolbar and press the button ‘Go’ beside to change the starting address. Use the scrollbar at the side of the window to view other memory locations in the system’s address space without changing the starting address of the display. Scrollbar can go to anywhere in any address without problem.

The treasure in this program is the class CSW_EditorBase. It is called ‘EditorBase’ because it can be a basic class for any kind of editor, such as an Information Window or a Binary Viewer. The class has all the necessary frame members for an editor control.

Editors are easily built by deriving just a few members from CSW_EditorBase, and they don’t have to take care of flashing, selections, scrollbars, drawing part, and other boring works has to be done on each kind of editors job over and over.

If you are looking for a tiny text window, it would be a very nice startup.

In fact, the purpose of the Memory Viewer is just to test this idea and implement the class CSW_EditorBase. It proves well.

How a common text editor works

To draw texts line by line, you have to decide :

  1. The number of lines, GetTotalLines() in CSW_EditorBase;

  2. The height of a line, which is the maximum height of all characters in a line. GetLineH() in CSW_EditorBase;

  3. A line index from a client Y position on pixels. GetLineFromY() in CSW_EditorBase;

  4. The number of columns, it should be the maximum columns in all lines. It is used to calculate scrollbars range in most situations. GetTotalCols() in CSW_EditorBase;

  5. A width of a column. It is very important when the font is varied. GetColW() in CSW_EditorBase;

  6. A column index from a client X position on pixels. GetColFromX() in CSW_EditorBase;

  7. Get the string in a line. GetLineStr() in CSW_EditorBase;

  8. A method to draw a line. OnDrawItem() in CSW_EditorBase;

  9. Calculate scrollbar ranges. OnUpdateBars() in CSW_EditorBase;

  10. Override OnPaint(), MouseMove() and other CWnd methods.

  11. To remove some refresh problems, only draws the visible lines and moves the whole client area when scrolling.

  12. It may be other works such as how to save all text or draw an image inside.

Class CSW_EditorBase members

Abstract class CSW_EditorBase derives from CWnd and has the following member functions:

Line functions, a line can be a line of text and its alignment

   // Get the number of total lines
   virtual UINT    GetTotalLines   (void) = 0;
   // Get the height of a line
   virtual UINT    GetLineH        (int nLine);
   // Get a line number from a position( in pixel)
   virtual int     GetLineFromY    (int &Y);

Column functions, a column can be a char and its alignment, or an image

   // Get the maximum number of columns in all lines
   virtual UINT    GetTotalCols    (void) = 0;
   // Get the with of a column
   virtual UINT    GetColW         (int nCol);
   virtual int     GetColFromX     (int &X);

   // Cell functions
   // Get the line and column index from a point
   virtual BOOL    GetLineColFromPt(CPoint ↦Pt,
                                    int &iLine,
                                    int &iCol);

   // String functions
   // Get the string for a line
   virtual void    GetLineStr      (int nLine, CString& str) = 0;

   // Paint
   virtual BOOL    OnDrawItem      ( CDC *pDC,
                                     UINT nLine,
                                     LPCTSTR lpStr,
                                     int FirstSelectedChar = -1,
                                     int LastSelectedChar = -1);

   // Scroll bars, update scrollbars range
           void    OnUpdateBars    (void);

   // Don't derive again in subclass, normally.

   afx_msg BOOL    OnEraseBkgnd    (CDC *pDC);
   afx_msg void    OnPaint         (void);
   afx_msg void    OnSize          (UINT nType, int cx, int cy);

   afx_msg void    OnHScroll       ( UINT nSBCode,
                                    UINT nPos,
                                    CScrollBar* pScrollBar);
   afx_msg void    OnVScroll       ( UINT nSBCode,
                                     UINT nPos,
                                     CScrollBar* pScrollBar);

   afx_msg void    OnLButtonDown   (UINT nFlags, CPoint point);
   afx_msg void    OnLButtonDblClk (UINT nFlags, CPoint point);
   afx_msg void    OnMouseMove     (UINT nFlags, CPoint point);
   afx_msg void    OnLButtonUp     (UINT nFlags, CPoint point);
   afx_msg BOOL    OnMouseWheel    (UINT nFlags,
                                    short zDelta,
                                    CPoint pt);
   ...

How to use class CSW_EditorBase

Derive a new class from CSW_EditorBase, overrides some members as necessary. GetTotalLines(),GetTotalCols() and GetLineStr() are essential.
Creates an instance of the new class and puts it inside a view window.

See CSW_TextInfo as a simple example.

class CSW_TextInfo : public CSW_EditorBase
{
private:

   // Save all lines here
   CStringArray    m_StrData;
   // Keep the maximun number of columns
   int             m_MaxColNBR;
   // public members
public:
   CSW_TextInfo    ();
   // Add a line of string
   void    AddLine (LPCTSTR lpStr);
   // Clean all information
   void    Clean   (void);
   // Members overrides from its parent class
protected:
   virtual UINT    GetTotalLines   (void);
   virtual UINT    GetTotalCols    (void);
   virtual void    GetLineStr      (int nLine, CString& str);
};

See CSW_BDisplay in the source for a complicated example.

MemViewer History

v2.0 is on process

v1.0 (01/December/2001)
First public release

Downloads

Download source and demo project – 35KB

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read