Home > Software engineering >  MFC serial program written real-time drawing slow problem
MFC serial program written real-time drawing slow problem

Time:10-31

Me in the picture control controls the inside construction, through the serial port in x, y coordinates of the point of real-time picture in picture control controls inside, I started drawing found minimize figure painting before has not, so just by watching video Sun Xin teachers, by using the ptrArray , the coordinates increase in real-time, and then the inside of the each drawing traversal point for drawing, found that drawing delay too much , have to wait for will display a little while, and the CPU utilization from 6% to 30%, please ask what is this? Thank you all!

All of my drawing are carried out in on paint program
Void CComElvesDlg: : OnPaint ()
{
//extern int x_lc;
If (IsIconic ())
{
CPaintDC dc (this);//device context for painting

SendMessage (WM_ICONERASEBKGND, (WPARAM) dc. GetSafeHdc (), 0).

//Center icon in the client rectangle
Int cxIcon=GetSystemMetrics (SM_CXICON);
Int cyIcon=GetSystemMetrics (SM_CYICON);
CRect rect;
GetClientRect (& amp; The rect);
Int x=(the rect. Width () - cxIcon + 1)/2;
Int y=(the rect. Height () - cyIcon + 1)/2;

//Draw the icon
Dc. DrawIcon (x, y, m_hIcon);
}
The else
{
Int rgb1, rgb2 rgb3;
CDialog: : OnPaint ();
CRect rect;
The CWnd * pWin=GetDlgItem (IDC_PIC);//get the control of a pointer, can directly to the control operation
PWin - & gt; GetClientRect (the rect);//the control aspect, the coordinate information stored in the rect in
The CDC * pDc=pWin - & gt; GetDC ();//get the control of the canvas
//a canvas, below free drawing, think how painting was how words,
//set the font
PDc - & gt; SetBkMode (TRANSPARENT);//set the background transparent
CFont * pFont=GetFont ();

CPen cu;
Cu. CreatePen (PS_SOLID, 5, RGB (0, 0, 0));//red, 5 pixels wide
CPen * oldPen=pDc - & gt; SelectObject (& amp; Cu);
PDc - & gt; SelectObject (oldPen);
Cu. DeleteObject ();

for (int i=0; i {
If (1==(. (CGraph *) m_ptrArray GetAt (I)) - & gt; M_color)
{
Rgb1=0;
Rgb2=0;
Rgb3=255;
}
If (0==(. (CGraph *) m_ptrArray GetAt (I)) - & gt; M_color)
{
Rgb1=255;
Rgb2=0;
Rgb3=0;
}
CPen huatu;
Huatu. CreatePen (PS_SOLID, 5, RGB (rgb1 rgb2, rgb3));//red, 5 pixels wide
PDc - & gt; SelectObject (& amp; Huatu);
PDc - & gt; MoveTo (((CGraph *) m_ptrArray GetAt (I)) - & gt; M_xLocation, ((CGraph *) m_ptrArray GetAt (I)) - & gt; M_yLocation + 1);
PDc - & gt; LineTo (((CGraph *) m_ptrArray GetAt (I)) - & gt; M_xLocation, ((CGraph *) m_ptrArray GetAt (I)) - & gt; M_yLocation);
PDc - & gt; SelectObject (oldPen);
Huatu. DeleteObject ();
}


//draw the X axis
CPen x_zuobiao;
X_zuobiao. CreatePen (PS_SOLID, 2, RGB (0, 0, 255));//blue, 2 pixels wide
PDc - & gt; SelectObject (& amp; X_zuobiao);
PDc - & gt; MoveTo (0, the rect. Bottom);
PDc - & gt; + 20 LineTo (the rect. Right, the rect. Bottom);
PDc - & gt; SelectObject (oldPen);
X_zuobiao. DeleteObject ();

//draw Y axis
CPen y_zuobiao;
Y_zuobiao. CreatePen (PS_SOLID, 2, RGB (0, 0, 255));//blue, 2 pixels wide
PDc - & gt; SelectObject (& amp; Y_zuobiao);
PDc - & gt; MoveTo (0, the rect. Bottom);
PDc - & gt; LineTo (0-20);
PDc - & gt; SelectObject (oldPen);
Y_zuobiao. DeleteObject ();
//
//X arrows painted
CPen x_jiantou;
X_jiantou. CreatePen (PS_SOLID, 2, RGB (0, 0, 255));//blue, 2 pixels wide
PDc - & gt; SelectObject (& amp; X_jiantou);
PDc - & gt; MoveTo (the rect. Right + 20, the rect. Bottom);
PDc - & gt; LineTo (the rect. Right + 10, the rect. Bottom - 5).
PDc - & gt; MoveTo (the rect. Right + 20, the rect. Bottom);
PDc - & gt; LineTo (the rect. Right + 10, the rect. Bottom + 5);
PDc - & gt; SelectObject (oldPen);
X_jiantou. DeleteObject ();
Kedumingcheng_font (pDc, pFont);//the selected font
PDc - & gt; TextOut ((the rect. Right)/2, the rect. Bottom + 30, _T (" level "));
PDc - & gt; SelectObject (pFont);//restore the original font

//Y arrows painted
CPen y_jiantou;
Y_jiantou. CreatePen (PS_SOLID, 2, RGB (0, 0, 255));//blue, 2 pixels wide
PDc - & gt; SelectObject (& amp; Y_jiantou);
PDc - & gt; MoveTo (0-20);
PDc - & gt; LineTo (- 5-10);
PDc - & gt; MoveTo (0-20);
PDc - & gt; The LineTo (5-10);
PDc - & gt; SelectObject (oldPen);
Y_jiantou. DeleteObject ();

//draw X scale
CPen x_kedu;
X_kedu. CreatePen (PS_SOLID, 2, RGB (0, 0, 255));//blue, 2 pixels wide
PDc - & gt; SelectObject (& amp; X_kedu);
for (int i=0; i <=the rect. Right; I=I + 30)
{
Cstrings str1;
Str1. The Format (_T (" % d "), I);
Kedu_font (pDc, pFont);//the specified font
PDc - & gt; TextOut (I - 5, the rect. Bottom + 10, str1);
PDc - & gt; SelectObject (pFont);//restore the original font
PDc - & gt; MoveTo (I, the rect. Bottom);
PDc - & gt; LineTo (I, the rect. Bottom + 5);
}
PDc - & gt; SelectObject (oldPen);
X_kedu. DeleteObject ();

//draw the Y-axis calibration
CPen y_kedu;
Y_kedu. CreatePen (PS_SOLID, 2, RGB (0, 0, 255));//blue, 2 pixels wide
PDc - & gt; SelectObject (& amp; Y_kedu);
For (int I=the rect. Bottom; I & gt;=0; I=I - 30)
{
Cstrings str1;
Str1. The Format (_T (" % d "), the rect. The bottom - I);
Kedu_font (pDc, pFont);//the specified font
PDc - & gt; TextOut (20, I, str1);
PDc - & gt; SelectObject (pFont);//restore the original font
PDc - & gt; MoveTo (0, I);
PDc - & gt; LineTo (5, I);
}
PDc - & gt; SelectObject (oldPen);
Y_kedu. DeleteObject ();
The ReleaseDC (pDc);
}
}

Here is the serial port receiving part

LRESULT CComElvesDlg: : OnReceive (WPARAM ch, LPARAM port)
{
If (port & lt;=0 | | port & gt; 4)
return -1;
M_RCount + +;//receiving byte count
Cstrings strTemp;
StrTemp. The Format (" % ld, "m_RCount); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related