Display your words in a regular vertical bar in the control

Source: Internet
Author: User
Tags drawtext

To meet the development requirements, I would like to write a question about the vertical display of words here. In your control, make it display regularly.

It allows him to display a letter, a letter, or a word. I have vc6 now. We also want to change the words displayed on it at any time.

First of all, I made the button text so that it is displayed in the vertical bar, multiple words so that it can be displayed anywhere in your button.

Let's first create a class called ctextmuilt. Inherit from the cbutton class.

To make the button re-painting take effect, the most important thing is that its attribute must be set to bs_ownerdraw. You can add this item to this function.

Presubclasswindow (), which should be a virtual function. Right-click the ctextmuilt class and add the virtual function to it. You can also add it (create) during creation so that we can do things.

The project I am working on is so abnormal that I have to write everything myself. There is no way to do it. I had no foundation and struggled for three months. I still felt that I could not do anything.

First, double-click the ctextmuilt class and add a create function to it.

Public:
Void create (maid, cwnd * PR, crect RC, uint ID)

Let's take a look at the original create of the cbutton class.

Lpszcaption

Specifies the text of the button control.

Dwstyle

Specifies the style of the button control. Applies to any combination of Button styles.

Rect

Specifies the size and position of the button control. It can be a crect object or rect structure.

Pparentwnd

Specifies the parent window of the button control, usually cdialog. Cannot be null.

Nid

ID of the button control.

Well, the most important thing is coming.

Next, we will add a drawitem function.

This does not need to be added.

Void textmiult: drawitem (lpdrawitemstruct LP)
{
// Obtain the button handle
CDC * PDC = CDC: fromhandle (LP-> HDC );
Crect RC;
// Obtain the current customer Region
Getclientrect (& rc );
Cbrush brushs (RGB (100,100,255 ));
PDC-> SelectObject (& brushs );
PDC-> rectangle (& rc );
// Create a font
Cfont font;

Font. createpointfont (90, _ T ("Arial Unicode ms "));
// Select the font in the device.
PDC-> SelectObject (font );
Cstring STR;
// Get the font
Getwindowtext (STR );
// Set the text background to transparent output, that is, the text background remains unchanged.
PDC-> setbkmode (transparent );
// Set the text to red
PDC-> settextcolor (RGB (255, 0, 0 ));
// Set the rectangular area for text display
Crect boundary (30, 29, 80, 70 );
// Set the blue area
Cbrush brush (RGB (0, 0, 255 ));
PDC-> SelectObject (& brush );
PDC-> rectangle (& boundary );
// Set the purple border
Cpen pen (ps_dot, 2, RGB (128,0, 128 ));
PDC-> SelectObject (& pen );
PDC-> rectangle (& boundary );
// Set the temporary area for text display
Crect rect (0, 0, boundary. Width (), boundary. Height ());
// Int Height = PDC-> drawtext (STR, & rc, dt_calcrect | dt_center | dt_editcontrol | dt_wordbreak );
// Use the dt_calcrect, dt_center, dt_editcontrol, and dt_wordbreak parameters to calculate the required text height.
Int Height = PDC-> drawtext (STR, rect, dt_wordbreak | dt_noclip | dt_wordbreak | dt_calcrect );
// Assign the original region to the rect after the calculation is complete.
Rect = boundary;
// Prevent the height from exceeding the range to avoid negative numbers.
If (boundary. Height ()> height)
{
RC. Top + = (boundary. Height ()-height)/2; // calculate the half of the blank height. + = is used here.
}
// Output the text result of vertical center + left-right center + automatic line feed
PDC-> drawtext (STR, rect, dt_center | dt_wordbreak | dt_editcontrol );
}

Crect boundary (,); ----------------------- you can display your words anywhere in your control.

Now let's take a look at the most important landlord. How should I write in the CREATE FUNCTION.

There are two writing methods. First. Add the self-painting to bs_multiline -- this is multi-line display.

Void ctextmiult: Create (lpctstr lpszcaption, cwnd * PR, crect RC, uint ID)
{
Cbutton: Create (lpszcaption, ws_child | ws_visible | bs_multiline | bs_ownerdraw | bs_pushbutton, RC, PR, ID );
}

The second type is

Void ctextmiult: Create (lpctstr lpszcaption, cwnd * PR, crect RC, uint ID)
{
Cbutton: Create (lpszcaption,
Ws_child | ws_visible | bs_multiline, RC, PR, ID );
}

Add presubclasswindow () to this function ()

Modifystyle (0, bs_ownerdraw | bs_pushbutton );

Add him. Haha. After writing, add some images to everyone. Tomorrow is the Lantern Festival. Wish you a happy Lantern Festival first. Haha.

For him to make it easier for everyone to understand, I will add a border for him and color it for everyone to distinguish.

As shown in the figure, although the color is not very nice, let's take a look at the effect.

Once again, change the color. Change the location.

Okay. Displayed.

Because my buttons are set to be large, this effect is achieved.

Well, I learned it from the basics, so I wrote it in detail.

Okay. Complete.

Here is a picture

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.