Similar to the "CPU usage" Control in the Task Manager

Source: Internet
Author: User
Tags drawtext

This class is generated from cstatic.

Add a static text box in the dialog box editor before you directly use classwizard to add a member variable member clevelbar in the dialog box, or use create to generate a static text box dynamically.

This control also provides a threshold. When the threshold is exceeded, the parameter is displayed in another color.

The Code is as follows:

Class Definition

//////////////////////////////////////// /////////////////////////////////////
// Clevelbar. h parameter monitoring window
// Coder: daijun scut-ee-425
// Version: 1.0.0
// Created: 2007-07-12
//////////////////////////////////////// /////////////////////////////////////

Class clevelbar: Public cstatic
{
// Construction
Public:
Clevelbar ();

// Attributes
Public:
 
Colorref m_bkcolor; // background color
Colorref m_barcolor; // foreground color
Colorref m_barbkcolor; // Security remaining area color
Colorref m_txcolor; // text color
Colorref m_warncolor; // hazardous area foreground
Colorref m_warnbkcolor; // hazardous residual area color
 
Int npercent; // percentage of the current parameter (0 ~ 100)
 
Protected:
Int m_nslice; // Number of cut parts
Char szvalue [32]; // annotation text
Int nwarn; // dangerous line percentage (20 ~ 100)
Crect m_dwrect; // rect of the drawing Area
// Operations
Public:
Void setpercent (int p );
Void settext (char * sztext );
Void setSlice (int n );
Void setwarnlevel (INT eV );
Void setcolorbk (colorref color );
Void setcolors (colorref fore, colorref barbk, colorref warn, colorref warnbk );
Protected:
Void drawbackground ();
Void drawbar ();
Void drawtext ();

// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (clevelbar)
//} Afx_virtual

// Implementation
Public:
Virtual ~ Clevelbar ();

// Generated message map Functions
Protected:
// {Afx_msg (clevelbar)
Afx_msg void onpaint ();
// Note-The classwizard will add and remove member functions here.
//} Afx_msg

Declare_message_map ()
};

 

//////////////////////////////////////// //////////////////////////////////////// //////////

//////////////////////////////////////// //////////////////////////////////////// /////////////////////////////////

Implementation file:

Clevelbar: clevelbar (): m_nslice (20)
{
M_bkcolor = RGB (0, 0, 0 );
M_barcolor = RGB (30,250, 30 );
M_barbkcolor = RGB (0, 50, 0 );
M_txcolor = m_barcolor;
M_warnbkcolor = RGB (50, 0, 0 );
M_warncolor = RGB (255, 20, 20 );
Npercent = 0;
Nwarn= 100;
M_txcolor = (npercent> nwarn )? M_warncolor: m_barcolor;
 
Strcpy (szvalue ,"");
}

Clevelbar ::~ Clevelbar ()
{
 
}

Begin_message_map (clevelbar, cstatic)
// {Afx_msg_map (clevelbar)
On_wm_paint ()
//} Afx_msg_map
End_message_map ()

//////////////////////////////////////// /////////////////////////////////////
// Clevelbar message handlers

Void clevelbar: onpaint ()
{
Cpaintdc DC (this); // device context for painting
Getclientrect (& m_dwrect );
 
// Draw the background
Drawbackground ();
Drawbar ();
Drawtext ();

DC. SelectObject (poldpen );
}

Void clevelbar: drawbackground ()
{
Cclientdc DC (this );
Cbrush BR;
BR. createsolidbrush (m_bkcolor );
DC. fillrect (& m_dwrect, & Br );
BR. deleteobject ();
}

// Draw a cursor
Void clevelbar: drawbar ()
{
Cclientdc DC (this );
Int barwidth, barlen;
Crect rectbar;
Crect CTS;
Rectbar. Left = m_dwrect.left + 15; // bar area size
Rectbar. Right = m_dwrect.right-15;
Rectbar. Top = m_dwrect.top + 5;
Rectbar. Bottom = m_dwrect.bottom-25;
Barwidth = rectbar. Height ()/m_nslice-1;
Barlen = rectbar. Width ()/2-2;

// Specify a foreground entry
Cbrush BR;
BR. createsolidbrush (m_barcolor );
Int I;
Int foreslice = npercent * m_nslice/100;
Int warnslice = nwarn * m_nslice/100;
If (npercent> nwarn) & (foreslice = warnslice ))
{
-- Warnslice;
}
For (I = 0; I <foreslice; I ++)
{
RDBMS. setrect (rectbar. Left + 1, rectbar. Bottom-barwidth-(barwidth + 1) * I,
Rectbar. Left + barlen, rectbar. Bottom-(barwidth + 1) * I );
DC. fillrect (& RDBMS, & Br );
RDBMS. setrect (rectbar. Left + barlen + 2, rectbar. Bottom-barwidth-(barwidth + 1) * I,
Rectbar. Right-1, rectbar. Bottom-(barwidth + 1) * I );
DC. fillrect (& RDBMS, & Br );
If (I + 1) = warnslice)
{
++ I;
Break; // exit the loop
}
}
If (foreslice> I) // area of the alarm
{
BR. deleteobject ();
BR. createsolidbrush (m_warncolor );
For (; I <foreslice; I ++)
{
RDBMS. setrect (rectbar. Left + 2, rectbar. Bottom-barwidth-(barwidth + 1) * I,
Rectbar. Left + barlen, rectbar. Bottom-(barwidth + 1) * I );
DC. fillrect (& RDBMS, & Br );
RDBMS. setrect (rectbar. Left + barlen + 2, rectbar. Bottom-barwidth-(barwidth + 1) * I,
Rectbar. Right-1, rectbar. Bottom-(barwidth + 1) * I );
DC. fillrect (& RDBMS, & Br );
}
}

BR. deleteobject ();
BR. createsolidbrush (m_barbkcolor );
// Background
For (; I <m_nslice & I <warnslice; I ++)
{
RDBMS. setrect (rectbar. Left + 1, rectbar. Bottom-barwidth-(barwidth + 1) * I,
Rectbar. Left + barlen, rectbar. Bottom-(barwidth + 1) * I );
DC. fillrect (& RDBMS, & Br );
RDBMS. setrect (rectbar. Left + barlen + 2, rectbar. Bottom-barwidth-(barwidth + 1) * I,
Rectbar. Right-1, rectbar. Bottom-(barwidth + 1) * I );
DC. fillrect (& RDBMS, & Br );
}
BR. deleteobject ();
BR. createsolidbrush (m_warnbkcolor );
// Alarm background
For (; I <m_nslice; I ++)
{
RDBMS. setrect (rectbar. Left + 2, rectbar. Bottom-barwidth-(barwidth + 1) * I,
Rectbar. Left + barlen, rectbar. Bottom-(barwidth + 1) * I );
DC. fillrect (& RDBMS, & Br );
RDBMS. setrect (rectbar. Left + barlen + 2, rectbar. Bottom-barwidth-(barwidth + 1) * I,
Rectbar. Right-1, rectbar. Bottom-(barwidth + 1) * I );
DC. fillrect (& RDBMS, & Br );
}

BR. deleteobject ();

}

Void clevelbar: drawtext ()
{
Cclientdc DC (this );
Crect CTS;
RDBMS. Top = m_dwrect.bottom-20;
RDBMS. Left = m_dwrect.left;
RDBMS. Right = m_dwrect.right;
RDBMS. Bottom = m_dwrect.bottom-5;

DC. setbkcolor (m_bkcolor );
DC. settextcolor (m_txcolor );
 
DC. drawtext (cstring (szvalue), & RDBMS, dt_center );

}

Void clevelbar: settext (char * sztext)
{
Strcpy (szvalue, sztext );
}

Void clevelbar: setSlice (int n)
{
M_nslice = (n> 50 )? 50: N;
}

Void clevelbar: setpercent (int p)
{
Npercent = (P> 100 | P <0 )? 0: P;
M_txcolor = (npercent> nwarn )? M_warncolor: m_barcolor;
}

Void clevelbar: setwarnlevel (INT eV)
{
Nwarn = (lev> 100 | lev_number <20 )? 100: lev.aliyun.com;
}

Void clevelbar: setcolorbk (colorref color)
{
M_bkcolor = color;
}

Void clevelbar: setcolors (colorref fore, colorref barbk, colorref warn, colorref warnbk)
{
M_barcolor = fore;
M_barbkcolor = barbk;
M_warncolor = warn;
M_warnbkcolor = warnbk;
M_txcolor = (npercent> nwarn )? M_warncolor: m_barcolor;
}

 

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.