// Linepic. cpp: Implementation of the clinepic class.
// This module is the drawing module. After obtaining the context of the device, you can draw the image. However, when the form module is required
// Call invalidaterect (rect * rect) to invalidate the drawing area and trigger the onpaint event. At the same time,
// You need to add a function to the onpaint function of the form and copy the image drawn in the memory DC to the DC of the form.
// Created by Huang. haihong
//////////////////////////////////////// //////////////////////////////
# Include "stdafx. H"
# Include "GDB tool. H"
# Include "linepic. H"
# Ifdef _ debug
# UNDEF this_file
Static char this_file [] =__ file __;
# Define new debug_new
# Endif
//////////////////////////////////////// //////////////////////////////
// Construction/destruction
//////////////////////////////////////// //////////////////////////////
Clinepic: clinepic ()
{
}
Clinepic: clinepic (CDC * DC)
{
Int I;
M_dcline = Dc;
M_dcmeml.createcompatibledc (m_dcline );
M_dememold.createcompatibledc (m_dcline );
M_dcline-> getboundsrect (& m_rcclient, 0 );
Xunit = 10;
Yunit = 10;
XScale = 1;
Yscale = 1;
For (I = 0; I <5; I ++)
{
Pointlast [I]. x = 0;
Pointlast [I]. Y = 0;
}
Xovalue = 0;
Topempty = 30;
Leftempty = 30;
M_legend [0] = "";
M_legend [1] = "";
M_legend [2] = "";
M_legend [3] = "";
M_legend [4] = "";
M_picname = "Table1 ";
}
Clinepic ::~ Clinepic ()
{
M_dcmeml.deletedc ();
M_dememold.deletedc ();
}
Bool clinepic: moverange (INT xoff)
{
Int oldw;
Oldw = m_rcclient.right-xoff;
Xovalue + = xoff;
// Copy the source image to the copy
// Copy only the part that is not repainted
M_bmpold.createcompatiblebitmap (m_dcline, oldw, m_rcclient.bottom-topempty );
M_dememold.selectobject (& m_bmpold );
M_dememold.bitblt (, oldw, m_rcclient.bottom-topempty,
& M_dcmeml, xoff, topempty, srccopy );
// Copy the copy to the source Image
M_dcmeml.selectobject (& m_bmp );
M_dcmeml.bitblt (0, topempty, oldw, m_rcclient.bottom-topempty,
& M_dememold, 0, 0, srccopy );
Drawrect (oldw, topempty, m_rcclient.right, m_rcclient.bottom );
// Deleteobject;
Drawgrid (oldw, 0 );
Drawaxistitle ();
M_bmpold.deleteobject ();
M_bmp .deleteobject ();
Return true;
}
/* Expand the image canvas */
Bool clinepic: setrange (INT width, int height)
{
Int oldw, oldh;
Oldw = m_rcclient.right;
Oldh = m_rcclient.bottom;
M_rcclient.bottom = height;
M_rcclient.right = width;
// Copy the source image to the copy
// Copy only the part that is not repainted
M_bmpold.createcompatiblebitmap (m_dcline, oldw, (oldh-m_rcclient.bottom % yunit ));
M_dememold.selectobject (& m_bmpold );
M_dememold.bitblt (0, 0, oldw, oldh,
& M_dcmeml, 0, 0, srccopy );
// Regenerate the source Image
M_bmp .createcompatiblebitmap (m_dcline, width, height );
Erasebkgrd ();
// Copy the copy to the source Image
M_dcmeml.selectobject (& m_bmp );
M_dcmeml.bitblt (0, 0, oldw, oldh,
& M_dememold, 0, 0, srccopy );
// Deleteobject;
M_bmpold.deleteobject ();
M_bmp .deleteobject ();
Return true;
}
/* Reset the image */
Bool clinepic: resetrange (INT width, int height)
{
M_bmp .createcompatiblebitmap (m_dcline, width, height );
M_rcclient.bottom = height;
M_rcclient.right = width;
Xovalue = 0;
Erasebkgrd ();
Return true;
}
Bool clinepic: drawrect (INT X1, int Y1, int X2, int Y2, colorref color)
{
M_dcmeml.selectstockobject (null_pen );
M_hbrushnew.createsolidbrush (color );
Verify (m_hbrushold = m_dcmeml.selectobject (& m_hbrushnew ));
M_dcmeml.rectangle (x1, Y1, X2, Y2 );
M_dcmeml.selectobject (m_hbrushold );
M_hbrushnew.deleteobject ();
Return true;
}
CDC * clinepic: getmemdc ()
{
Return (& m_dcmeml );
}
Bool clinepic: erasebkgrd ()
{
// M_hpennew.createpen (ps_solid, 1, RGB (0, 0 ));
M_dcmeml.selectobject (& m_bmp );
// Verify (m_hpenold = m_dcmeml.selectobject (& m_hpennew ));
M_dcmeml.selectstockobject (null_pen );
M_hbrushnew.createsolidbrush (RGB (0, 0, 0 ));
Verify (m_hbrushold = m_dcmeml.selectobject (& m_hbrushnew ));
M_dcmeml.rectangle (0, 0, m_rcclient.right + 1, m_rcclient.bottom );
// M_dcmeml.textout (0, 0, "ABC ");
// M_dcmeml.selectobject (m_hpenold );
M_dcmeml.selectobject (m_hbrushold );
// M_hpennew.deleteobject ();
M_hbrushnew.deleteobject ();
M_bmp .deleteobject ();
Drawgrid (0, 0 );
// Drawaxis ();
// Drawaxistitle ();
Drawtitle ();
Drawlegend ();
Return true;
}
Bool clinepic: drawcircle (INT topx, int topy, int radius)
{
// Create a graphic object
M_hpennew.createpen (ps_solid, 1, RGB (255 ));
M_hbrushnew.createsolidbrush (RGB (255,255, 0 ));
// Reselect a graphic object
M_dcmeml.selectobject (& m_bmp );
Verify (m_hpenold = m_dcmeml.selectobject (& m_hpennew ));
Verify (m_hbrushold = m_dcmeml.selectobject (& m_hbrushnew ));
// Circle
M_dcmeml.ellipse (topx, topy, topx + radius, topy + radius );
// Delete object
M_dcmeml.selectobject (m_hpenold );
M_dcmeml.selectobject (m_hbrushold );
M_hpennew.deleteobject ();
M_hbrushnew.deleteobject ();
M_bmp .deleteobject ();
Return true;
}
Bool clinepic: drawgrid (int x, int y)
{
Int I, J;
M_hpennew.createpen (ps_solid, 1, RGB (0,100, 0 ));
Verify (m_hpenold = m_dcmeml.selectobject (& m_hpennew ));
/*
For (I = (X-leftempty)/xunit + 1; I <= (m_rcclient.right/xunit); I ++)
{
M_dcmeml.moveto (I * xunit + (xunit-(xovalue-leftempty) % xunit), topempty );
M_dcmeml.lineto (I * xunit + (xunit-(xovalue-leftempty) % xunit), m_rcclient.bottom );
}
*/
For (I = x/xunit; I <= (m_rcclient.right/xunit); I ++)
{
M_dcmeml.moveto (I * xunit + (xunit-xovalue % xunit), topempty );
M_dcmeml.lineto (I * xunit + (xunit-xovalue % xunit), m_rcclient.bottom );
}
For (j = 0; j <= (m_rcclient.bottom/yunit); j ++)
{
M_dcmeml.moveto (x, J * yunit + topempty );
M_dcmeml.lineto (m_rcclient.right, J * yunit + topempty );
}
M_dcmeml.selectobject (m_hpenold );
M_hpennew.deleteobject ();
Return true;
}
Bool clinepic: drawaxis ()
{
Int I;
Int y = (m_rcclient.bottom)/yUnit-1) * yunit;
Int x = (m_rcclient.right/xUnit-1) * xunit;
Point Points [3];
Points [0]. x = 30;
Points [0]. Y = 0;
Points [1]. x = 25;
Points [1]. Y = 20;
Points [2]. x = 35;
Points [2]. Y = 20;
M_hpennew.createpen (ps_solid, 2, RGB (255 ));
Verify (m_hpenold = m_dcmeml.selectobject (& m_hpennew ));
M_dcmeml.selectstockobject (null_brush );
For (I = 0; I <= y; I ++)
{
M_dcmeml.moveto (30, Y-I * yunit );
M_dcmeml.lineto (25, Y-I * yunit );
}
For (I = 0; I <= x; I ++)
{
M_dcmeml.moveto (xunit * I + (xunit-(XOvalue-30) % xunit), y );
M_dcmeml.lineto (xunit * I + (xunit-(XOvalue-30) % xunit), Y + 5 );
}
// M_dcmeml.rectangle (30,0, m_rcclient.right-2, Y + 1 );
M_dcmeml.moveto (30,0 );
M_dcmeml.lineto (30, y );
M_dcmeml.moveto (30, y );
M_dcmeml.lineto (m_rcclient.right-2, y );
M_hbrushnew.createsolidbrush (RGB (0, 0, 255 ));
Verify (m_hbrushold = m_dcmeml.selectobject (& m_hbrushnew ));
M_dcmeml.polygon (points, 3 );
/* X = m_rcclient.right;
Y = m_rcclient.bottom-(m_rcclient.bottom % yunit)-30;
Points [0]. x = X;
Points [0]. Y = y;
Points [1]. x = x-20;
Points [1]. Y = y-5;
Points [2]. x = x-20;
Points [2]. Y = Y + 5;
M_dcmeml.polygon (points, 3 );*/
M_dcmeml.selectobject (m_hpenold );
M_hpennew.deleteobject ();
M_dcmeml.selectobject (m_hbrushold );
M_hbrushnew.deleteobject ();
Return true;
}
Bool clinepic: drawmarker (int x, int y, int index, bool bmark)
{
Int XC, YC, xold, yold, xover, yover;
Colorref color;
Switch (INDEX)
{
Case 0:
{
Color = RGB (0,255, 0 );
Break;
}
Case 1:
{
Color = RGB (255,255, 0 );
Break;
}
Case 2:
{
Color = RGB (255, 0, 0 );
Break;
}
Case 3:
{
Color = RGB (255, 0, 255 );
Break;
}
Case 4:
{
Color = RGB (1, 0,255,255 );
Break;
}
Default:
{
Color = RGB (1, 255,255,255 );
Break;
}
}
/*
XC = x * xunit/XScale + 30-xovalue;
YC = m_rcclient.bottom-y * yunit/yscale;
Xold = pointlast [Index]. x * xunit/XScale + 30;
Yold = m_rcclient.bottom-pointlast [Index]. y * yunit/yscale ;*/
XC = x * xunit/XScale-xovalue + (m_rcclient.right/xunit) * xUnit-1; //-1 avoids repainting Area
YC = m_rcclient.bottom-y * yunit/yscale;
Xold = pointlast [Index]. x * xunit/XScale-xovalue + (m_rcclient.right/xunit) * xUnit-1;
Yold = m_rcclient.bottom-pointlast [Index]. y * yunit/yscale;
If (YC> = m_rcclient.bottom)
YC = m_rcclient.bottom-1;
If (yold> = m_rcclient.bottom)
Yold = m_rcclient.bottom-1;
/* XC = x * xunit/XScale + 30-xovalue;
Xold = pointlast [Index]. x * xunit/XScale + 30-xovalue ;*/
If (XC> m_rcclient.right-5)
{
Moverange (xc-m_rcclient.right + 5 );
XC = x * xunit/XScale-xovalue + (m_rcclient.right/xunit) * xUnit-1;
Xold = pointlast [Index]. x * xunit/XScale-xovalue + (m_rcclient.right/xunit) * xUnit-1;
}
If (bmark)
{
M_hpennew.createpen (ps_solid, 2, color );
Verify (m_hpenold = m_dcmeml.selectobject (& m_hpennew ));
Point P [4];
P [0]. x = XC-4;
P [0]. Y = YC;
P [1]. x = XC;
P [1]. Y = YC + 4;
P [2]. x = XC + 4;
P [2]. Y = YC;
P [3]. x = XC;
P [3]. Y = YC-4;
M_dcmeml.polygon (p, 4 );
M_dcmeml.selectobject (m_hpenold );
M_hpennew.deleteobject ();
}
M_hpennew.createpen (ps_solid, 1, color );
Verify (m_hpenold = m_dcmeml.selectobject (& m_hpennew ));
// M_dcmeml.selectstockobject (black_pen );
/* If (pointlast [Index]. X! = 0)
{*/
If (YC <topempty & yold <topempty)
{
M_dcmeml.moveto (xold-1, topempty );
M_dcmeml.lineto (XC, topempty );
}
Else if (YC <topempty)
{
Xover = XC-(topempty-YC) * (XC-xold)/(yold-YC );
Yover = topempty;
M_dcmeml.moveto (xold, yold );
M_dcmeml.lineto (xover, yover );
M_dcmeml.moveto (xover, topempty );
M_dcmeml.lineto (XC, topempty );
}
Else if (yold <topempty)
{
Xover = (topempty-yold) * (XC-xold)/(YC-yold) + xold;
Yover = topempty;
M_dcmeml.moveto (xover, yover );
M_dcmeml.lineto (XC, YC );
M_dcmeml.moveto (xover, topempty );
M_dcmeml.lineto (xold-1, topempty );
}
Else
{
M_dcmeml.moveto (xold, yold );
M_dcmeml.lineto (XC, YC );
}
/*}
Else
{
M_dcmeml.moveto (XC, m_rcclient.bottom );
M_dcmeml.lineto (XC, YC );
}*/
/* If (0 = pointlast [Index]. X)
{
; // M_dcmeml.moveto (xold, yold );
// M_dcmeml.lineto (XC, YC );
}
Else
{
M_dcmeml.moveto (xold, yold );
M_dcmeml.lineto (xover, yover );
}*/
M_dcmeml.selectobject (m_hpenold );
M_hpennew.deleteobject ();
Pointlast [Index]. x = X;
Pointlast [Index]. Y = y;
Return true;
}
Crect * clinepic: getrect ()
{
Return (& m_rcclient );
}
Bool clinepic: setscale (int xs, int ys)
{
XScale = Xs;
Yscale = ys;
Drawaxistitle ();
Erasebkgrd ();
Return true;
}
Bool clinepic: drawtitle ()
{
M_dcmeml.setbkcolor (RGB (0, 0, 0 ));
M_dcmeml.settextcolor (RGB (0,255, 0 ));
M_hpennew.createpen (ps_solid, 1, RGB (255,255,150 ));
Verify (m_hpenold = m_dcmeml.selectobject (& m_hpennew ));
M_dcmeml.selectstockobject (null_brush );
M_dcmeml.moveto (0, topempty-1 );
M_dcmeml.lineto (m_rcclient.right, topempty-1 );
/*
M_dcmeml.selectobject (m_hpenold );
M_hpennew.deleteobject ();
M_hpennew.createpen (ps_solid, 1, RGB (200,200,255 ));
Verify (m_hpenold = m_dcmeml.selectobject (& m_hpennew ));
M_dcmeml.moveto (0, topempty-2 );
M_dcmeml.lineto (m_rcclient.right, topempty-2 );
/**/
Verify (m_pnewfont.createpointfont (110, "", & m_dcmeml ));
M_poldfont = m_dcmeml.selectobject (& m_pnewfont );
M_dcmeml.textout (280,0, m_picname );
M_dcmeml.selectobject (m_poldfont );
M_pnewfont.deleteobject ();
M_dcmeml.selectobject (m_hpenold );
M_hpennew.deleteobject ();
Return true;
}
Int clinepic: getlastwidth ()
{
Return (pointlast [0]. X/XScale * xunit-xovalue );
}
Void clinepic: settitle (cstring sztitle)
{
M_picname = sztitle;
Drawtitle ();
}
Void clinepic: setlegend (cstring szlegend, int index)
{
If (index> 4)
{
// Afxmessagebox ("This table supports up to 5 tags", mb_ OK, 0 );
Index = 4;
}
M_legend [Index] = szlegend;
Drawlegend ();
}
Bool clinepic: drawlegend ()
{
Colorref color [5];
Color [0] = RGB (0,255, 0 );
Color [1] = RGB (255,255, 0 );
Color [2] = RGB (255, 0, 0 );
Color [3] = RGB (255 );
Color [4] = RGB (1, 0,255,255 );
M_dcmeml.setbkcolor (RGB (0, 0, 0 ));
Verify (m_pnewfont.createpointfont (110, "", & m_dcmeml ));
M_poldfont = m_dcmeml.selectobject (& m_pnewfont );
For (INT I = 0; I <5; I ++)
{
M_dcmeml.settextcolor (color [I]);
M_dcmeml.textout (150 * I, 15, m_legend [I]);
}
M_dcmeml.selectobject (m_poldfont );
M_pnewfont.deleteobject ();
Return true;
}
Bool clinepic: drawaxistitle ()
{
Cstring szaxis;
Int funit;
Funit = yscale;
M_dcmeml.settextcolor (RGB (0,255, 0 ));
M_dcmeml.setbkcolor (RGB (0, 0, 0 ));
Szaxis. Format ("% d", 0 );
M_dcmeml.textout (0, m_rcclient.bottom-15, szaxis );
Szaxis. Format ("% d", (yscale * (m_rcclient.bottom-topempty)/yunit)/4 );
M_dcmeml.textout (0, (m_rcclient.bottom-topempty)/4*3 + topempty, szaxis );
Szaxis. Format ("% d", (yscale * (m_rcclient.bottom-topempty)/yunit)/4*2 );
M_dcmeml.textout (0, (m_rcclient.bottom-topempty)/4*2 + topempty, szaxis );
Szaxis. Format ("% d", (yscale * (m_rcclient.bottom-topempty)/yunit)/4*3 );
M_dcmeml.textout (0, (m_rcclient.bottom-topempty)/4 + topempty, szaxis );
Szaxis. Format ("% d", (yscale * (m_rcclient.bottom-topempty)/yunit ));
M_dcmeml.textout (0, m_rcclient.top + topempty, szaxis );
// The value of the Y axis is drawn every 1/4 drawing areas.
Return true;
}
Int clinepic: getscale (INT type)
{
Switch (type)
{
Case 1:
{
Return XScale;
}
Case 2:
{
Return yscale;
}
Default:
{
Return 0;
}
}
}