Industrial Control Software graphical interface-control implementation (Thermometer Control) (zz)

Source: Internet
Author: User
Tags drawtext

URL: http://dev.firnow.com/course/3_program/c++/cppsl/2008227/101906.html

Instrument controls, thermometer controls, and led controls are the most common interpersonal interaction controls in Industrial configuration software. They provide a more friendly interface display method. The thermometer control is actually a process of drawing a silver column and a scale. Considering the impact of refreshing on the interface, we adopt the dual-buffer technology.
Create a thermometer display control class through classwizard of VC and inherit cstatic. We can map the wm_paint message to complete the plotting of the thermometer here. This is consistent with the drawing principle of the preceding instrument control. The only difference is that different effects are displayed on the interface.
Void cthermometer: onpaint ()
{
Cpaintdc DC (this );
// Obtain the control area
Getclientrect (& m_rectctrl );
Cmemdc memdc (& DC, & m_rectctrl );
// Draw a dashboard
If (m_dcmeterplate.getsafehdc () = NULL | (m_bitmapmeterplate.m_hobject = NULL ))
{
M_dcmeterplate.createcompatibledc (& DC );
M_bitmapmeterplate.createcompatiblebitmap (& DC, m_rectctrl.width (), m_rectctrl.height ());
M_pbitmapoldmeterplate = m_dcmeterplate.selectobject (& m_bitmapmeterplate );
Drawmeterbackground (& m_dcmeterplate, m_rectctrl );
}
Memdc. bitblt (0, 0, m_rectctrl.width (), m_rectctrl.height (),
& M_dcmeterplate, 0, 0, srccopy );
Drawscale (& memdc );
M_ctrlunit.draw (& memdc );
}
Because our background can be considered unchanged during the double buffer rendering process Program During the running data refresh process, we can complete this part of the painting in the middle of the memory during initialization, and then call it to bring its bitbtn to the interface. Each time you call the previously drawn result bitbtn (post) to refresh the data to the interface, you can save the flickering feeling when refreshing the data. Then draw the silver column and the real-time value. The interface is drawn as the core part of the control.
// Draw the gauge background
Void cthermometer: drawmeterbackground (CDC * PDC, crect & rect)
{
Cstring strscale;
Cfont fscalefont, * poldfont;
Cpen penthick, penthin, penshadow, penscale, * poldpen;
Cbrush m_brushback, pbackbrush, * poldbrush;
PDC-> setbkcolor (m_backcolor );
M_brushback.createsolidbrush (m_backcolor );
Poldbrush = (cbrush *) PDC-> SelectObject (& m_brushback );
PDC-> fillrect (rect, & m_brushback); // draw the background
PDC-> SelectObject (poldbrush );
M_brushback.deleteobject ();
// Draw the three-dimensional effect of the border
Penthick. createpen (ps_solid, 1, RGB (172,168,153 ));
Penthin. createpen (ps_solid, 1, RGB (113,111,110 ));
Penshadow. createpen (ps_solid, 1, RGB (255,255,255 ));
Poldpen = (cpen *) PDC-> SelectObject (& penshadow );
PDC-> moveTo (rect. Left, rect. Top );
PDC-> lineto (rect. Right-1, rect. Top );
PDC-> moveTo (rect. Left + 1, rect. Top + 1 );
PDC-> lineto (rect. Right-2, rect. Top + 1 );
PDC-> moveTo (rect. Left, rect. Top );
PDC-> lineto (rect. Left, rect. Bottom-1 );
PDC-> moveTo (rect. Left + 1, rect. Top + 1 );
PDC-> lineto (rect. Left + 1, rect. Bottom-2 );
PDC-> SelectObject (& penthick );
PDC-> moveTo (rect. Left + 1, rect. Bottom-1 );
PDC-> lineto (rect. Right-1, rect. Bottom-1 );
PDC-> lineto (rect. Right-1, rect. Top + 1 );
PDC-> SelectObject (& penthin );
PDC-> moveTo (rect. Left, rect. Bottom );
PDC-> lineto (rect. Right, rect. Bottom );
PDC-> lineto (rect. Right, rect. Top );
PDC-> SelectObject (poldpen );
// Draw the pointer display area
Crect rectscale;
Rectscale. setrect (rect. Left + rect. Width ()/4,
Rect. Top + rect. Height ()/12,
Rect. Left + rect. Width ()/4 + rect. Width ()/10,
Rect. Bottom-rect. Height ()/12 );
Pbackbrush. createsolidbrush (RGB (128, 0, 0 ));
Poldbrush = (cbrush *) PDC-> SelectObject (& pbackbrush );
PDC-> fillrect (& rectscale, & pbackbrush );
PDC-> SelectObject (poldbrush );
Pbackbrush. deleteobject ();
// Draw the three-dimensional effect of the pointer Area
PDC-> SelectObject (& penthick );
PDC-> moveTo (rectscale. Left, rectscale. Bottom );
PDC-> lineto (rectscale. Left, rectscale. Top );
PDC-> lineto (rectscale. Right, rectscale. Top );
PDC-> SelectObject (& penthin );
PDC-> moveTo (rectscale. Left + 1, rectscale. Bottom );
PDC-> lineto (rectscale. Left + 1, rectscale. Top + 1 );
PDC-> lineto (rectscale. Right, rectscale. Top + 1 );
PDC-> SelectObject (& penshadow );
PDC-> moveTo (rectscale. Right-1, rectscale. Top + 1 );
PDC-> lineto (rectscale. Right-1, rectscale. Bottom-1 );
PDC-> lineto (rectscale. Left + 1, rectscale. Bottom-1 );
PDC-> moveTo (rectscale. Right, rectscale. Top );
PDC-> lineto (rectscale. Right, rectscale. Bottom );
PDC-> lineto (rectscale. Left, rectscale. Bottom );
M_rectscale = rectscale;
M_rectscale.deflaterect (2, 2, 2, 2 );
// Draw the scale
Fscalefont. createfont (rect. Height ()/8, 0,
0,
0,
Fw_normal,
False,
False,
False,
Default_charset,
Out_default_precis,
Clip_default_precis,
Default_quality,
Default_pitch | ff_dontcare,
"System ");
Poldfont = (cfont *) PDC-> SelectObject (& fscalefont );
Penscale. createpen (ps_solid, 1, RGB (0, 0, 0 ));
For (INT I = 0; I <m_nticks + 1; I ++)
{
Cpoint ptstarttick, ptendtick;
Int ntickdisc = rect. Height () * 5 * I/(6 * m_nticks );
Ptstarttick. x = rect. Left + rect. Width () * 3/7;
Ptstarttick. Y = rect. Bottom-rect. Height ()/12-ntickdisc;
Ptendtick. x = rect. Left + rect. Width () * 3/5;
Ptendtick. Y = rect. Bottom-rect. Height ()/12-ntickdisc;
PDC-> SelectObject (penscale );
PDC-> moveTo (ptstarttick );
PDC-> lineto (ptendtick );
// Draw the stereoscopic feeling
Cpoint ptshadowstarttick, ptshadowendtick;
Ptshadowstarttick = ptstarttick;
Ptshadowendtick = ptendtick;
Ptshadowstarttick. y --;
Ptshadowendtick. y --;
PDC-> SelectObject (penshadow );
PDC-> moveTo (ptshadowstarttick );
PDC-> lineto (ptshadowendtick );
// Draw a subscale
For (Int J = 0; j <m_nsubticks + 1; j ++)
{
If (I <m_nticks)
{
Cpoint ptsubstarttick, ptsubendtick;
Int nsubtickdisc = (rect. Height () * 5/(6 * m_nticks) * j/m_nsubticks;
Ptsubstarttick. x = ptstarttick. X;
Ptsubstarttick. Y = ptstarttick. Y-nsubtickdisc;
Ptsubendtick. x = ptsubstarttick. x + (ptendtick. X-ptsubstarttick. X)/2;
Ptsubendtick. Y = ptsubstarttick. Y;
PDC-> SelectObject (penscale );
PDC-> moveTo (ptsubstarttick );
PDC-> lineto (ptsubendtick );
Cpoint ptshadowsubstarttick, ptshadowsubendtick;
Ptshadowsubstarttick = ptsubstarttick;
Ptshadowsubendtick = ptsubendtick;
Ptshadowsubstarttick. y --;
Ptshadowsubendtick. y --;
PDC-> SelectObject (penshadow );
PDC-> moveTo (ptshadowsubstarttick );
PDC-> lineto (ptshadowsubendtick );
}
}
// Draw the scale
Crect ptscale;
PDC-> setbkmode (transparent );
Strscale. Format ("%. 0f", (m_dmaxvalue-m_dminvalue) * I/m_nticks );
Csize size = PDC-> gettextextent (strscale );
PDC-> settextcolor (RGB (255,255,255 ));
Ptscale. setrect (rect. Left + rect. Width () * 2/3,
Ptstarttick. Y-size. cy/2,
Rect. Right,
Ptstarttick. Y + size. cy/2 );
PDC-> drawtext (strscale, ptscale, dt_left | dt_vcenter | dt_singleline );
PDC-> settextcolor (RGB (0, 0, 0 ));
Ptscale. deflaterect (-1,-1, 1, 1 );
PDC-> drawtext (strscale, ptscale, dt_left | dt_vcenter | dt_singleline );
}
PDC-> SelectObject (poldpen );
PDC-> SelectObject (poldbrush );
PDC-> SelectObject (poldfont );
Penthick. deleteobject ();
Penthin. deleteobject ();
Penshadow. deleteobject ();
M_brushback.deleteobject ();
Pbackbrush. deleteobject ();
Fscalefont. deleteobject ();
Penscale. deleteobject ();
}
Below is the drawing of the thermometer silver column.
Void cthermometer: drawscale (CDC * PDC)
{
If (m_dcurrentvalue> m_dmaxvalue)
{
M_dcurrentvalue = m_dmaxvalue;
}
Else if (m_dcurrentvalue <m_dminvalue)
{
M_dcurrentvalue = m_dminvalue;
}
Crect rectscale;
Double yscale = (m_dcurrentvalue-m_dminvalue)/(m_dmaxvalue-m_dminvalue );
Rectscale. setrect (m_rectscale.left,
M_rectscale.bottom-int (yscale * (m_rectscale.height ())),
M_rectscale.right,
M_rectscale.bottom );
Cbrush pbackbrush, * poldbrush;
Pbackbrush. createsolidbrush (RGB (255, 0, 0 ));
Poldbrush = (cbrush *) PDC-> SelectObject (& pbackbrush );
PDC-> fillrect (& rectscale, & pbackbrush );
PDC-> SelectObject (poldbrush );
Pbackbrush. deleteobject ();
M_ctrlunit.setrect (getunitrect ());
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.