[Implementation] When the mouse moves in the view area, the coordinate position where the mouse is located in synchronized the mouse near the mouse. This feature is not complicated to many netizens. A simple way is to draw a coordinate position in OnDraw. The implementation of this paper is to implement the tracking display of mouse coordinates on the basis of not invoking view refresh and the original content on the view.
【Implementation】
1. Before the mouse moves, the rectangular position of the displayed mouse coordinate string is calculated.
2. Back up the image of this location in the view,
3. Draw coordinates on the view
4. When the mouse moves next time, the saved image is restored at the last position.
[Realize code]
Several constant definitions:
#define Word_Height 18 // The height of the number, adjusts the width of #define Word_Width 7 // according to the system's font, according to the system's font adjustment #define off_x 15 // coordinate display with the mouse distance #define off_y 10 //// Generally in the lower right corner of the mouse #define text_color RGB (255, 0, 0) // Color of coordinate text
Define the member variables of the view class:
CBitMap m_storebmp; // Store bitmap cdc m_storedc; // Stores DC INT M_NCOORDSTRLEN; // Coordinate string length cpoint m_oldpt; // The last mouse bit BOOL m_BSTART; / / Whether the mouse starts to move
// Copy the heavy picture bitmap, copy the image of the rectangular position of the coordinate string in the view
// copypt - the current position of the mouse
void CDrawMouseView :: CopyBitmap (CDC * pDC, CPoint copyPt) {if (m_StoreBmp.GetSafeHandle ()) {// if the prior bitmap, then the first empty m_StoreDC.DeleteDC (); m_StoreBmp.DeleteObject (); m_StoreBmp.m_hObject = }
if (m_StoreDC.CreateCompatibleDC (pDC)) {if (m_StoreBmp.CreateCompatibleBitmap (pDC, m_nCoordStrLen, WORD_HEIGHT)) {m_StoreDC.SelectObject (& m_StoreBmp); m_StoreDC.BitBlt (0,0, m_nCoordStrLen, WORD_HEIGHT, pDC, copyPt.x OFF_X , copypt.y off_y, srccopy;} else {if (m_storebmp.getsafehandle ()) m_storebmp.deleteObject (); m_storedc.deletedc ();}}}
// Painting coordinate
Coordstring --- coordinate string
drawPt-- draw the starting point coordinate void CDrawMouseView :: DrawCoord (CDC * pDC, CString & coordString, CPoint drawPt) {if (m_StoreBmp.GetSafeHandle ()) // there is only bitmap picture {drawPt.Offset (OFF_X, OFF_Y); CRect rect (drawPt, CSize (m_nCoordStrLen, WORD_HEIGHT)); pDC-> SetBkMode (tRANSPARENT); // set transparent background COLORREF crf = pDC-> SetTextColor (tEXT_COLOR); pDC-> DrawText (coordString, rect, DT_CENTER); pDC-> SetTextColor (crf);}} // erase the last coordinate void CDrawMouseView :: DoRubberCoord (CDC * pDC, CPoint showPt) {if (m_StoreBmp.GetSafeHandle ()) {m_StoreDC.SelectObject (& m_StoreBmp); pDC-> BitBlt ( showPt.x OFF_X, showPt.y OFF_Y, m_nCoordStrLen, WORD_HEIGHT, & m_StoreDC, 0,0, SRCCOPY); m_StoreDC.DeleteDC (); m_StoreBmp.DeleteObject ();}} // mouse move events void CDrawMouseView :: OnMouseMove ( UINT NFLAGS, CPOINT POINT) {CDC * PDC = getdc (); if (! M_bstart) m_bstart = true; Else Dorubbercoord (PDC, M_OLDPT); // When not the first time you move your mouse, you need to erase the last time. Coordinate CString Str; str.format ("[% D,% D]", Point.x, Point.y); m_ncoordstrlen = str.getLength () * Word_Width; m_oldpt = point; copybitmap (PDC, POINT); Drawcoord PDC, STR, POINT; ReleaseDC (PDC);} [Editor Note] The program used herein is compiled correctly under VC6.0. If you have different insights and opinions, welcome to discuss.