Realization of VC class of imitation Scroll.ocx control

Source: Internet
Author: User
Tags drawtext

A few days ago to do a project, in the interface design of the time to carry out a number of skin-changing operations. But because the interface uses the graphical interface development, the menu bar and the toolbar all have carried on the graphical processing, the Status column appears monotonous many, therefore replaces the Windows program to generate the status bar, uses the network above Scroll.ocx control. However, because the program belongs to the applet if loading more controls may not be convenient, plus in the process of using this control found that if the input display font length is longer than when the display of the refresh problem, so you copy the display method for this control to write.

The program inherits the static control class, which can be used in place of some controls to display some information when the program is created. Here I use instead of the status bar to show some welcome logins, as well as login names, and so on. The program uses the double buffering way to carry on the RECT output in different position. The program uses text to run from right to left. Because in the CDC class DrawText call to develop the output text area, we can make a fuss here, dynamic change this area. The right to left method is that all programs from the control to the right as the starting point as the left edge of the output text, using the timer on the left to gradually decrement operation. Then the area of our output text increments from the rightmost area of the control to the left area. With our dt_left way of drawing text, after the constant control refresh will appear text from the right to the left of the running scam.

Some of the main rendering methods of the control: (All are simple call drawing, ^_^)

To draw a control background:

void CScrollCtrl::DrawCtrlBackground(CDC *pDC, CRect &rect)
{
   CBrush pNewBrush, *pOldBrush;
   pNewBrush.CreateSolidBrush(m_clBkColor);
   pOldBrush = (CBrush *)pDC->SelectObject(pNewBrush);
   pDC->FillRect(&rect, &pNewBrush);
   pDC->SelectObject(&pOldBrush);
   pNewBrush.DeleteObject();
   //重新设置文字位置,默认为最右边
   CSize sSize;
   pDC->GetTextExtent(m_strText);
   m_nXPos = sSize.cx;
}

Draw text

void CScrollCtrl::DrawCtrlText(CDC *pDC, CRect rect)
{
   CRect rectText;
   CFont *pOldFont;
   pDC->SetBkMode(TRANSPARENT);
   pDC->SetTextColor(m_clTextColor);
   pOldFont = (CFont *)pDC->SelectObject(m_pFnt);
   //选择了字体要重新获得字体宽度
   CSize sSize;
   sSize = pDC->GetTextExtent(m_strText);
   m_nTextWidth = sSize.cx;
   rectText.left = m_nXPos;
   rectText.top = rect.top;
   rectText.bottom = rect.bottom;
   rectText.right = rect.right;
  
   pDC->DrawText(m_strText, rectText, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
  
   pDC->SelectObject(&pOldFont);
}

Call Method:

Create a new project, based on the direct call of the dialog box, based on the need for SDI and dynamic creation. Copy ScrollCtrl.h and ScrollCtrl.cpp to the program directory and load it into the project through the "Engineering"-"Add Project"-"Files". The font color and display font are available by defining the control object and setting the display background color.

Program Demo situation See below:

Because the time is hurried the control's encapsulation and the function are not very perfect, you are interested can carry on the function expansion, if has the good modification please sends the code to me! such as background gradients or providing more interfaces. If you have any questions, please contact me: successq_g@163.com

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.