Control font setting in VC

Source: Internet
Author: User
Like all beginners, new things are new at the beginning, and they always want to make the screen more beautiful, but it is difficult to do it in VC, such as setting the font of controls such as buttons in the dialog box, it took me some time.
I. To change the font size, use the following method:
1. Declare a global font pointer cfont * my_font = new cfont () at the beginning; // note that initialization cannot be blank
2. Call the function where you need to change the font:
Set_font (60, my_font, ""); // font size, pointer, name
Getdlgitem (idc_anniu)-> setfont (my_font); // change the font
3. For different fonts, you need to set different font pointers.
4. Delete the font when exiting. Otherwise, multiple calls may fail.
Bool cmydlg: destroywindow ()
{
If (my_font) my_font-> deleteobject ();
Return cdialog: destroywindow ();
}
5. The following is a detailed description of the font setting function:
Void set_font (INT height, cfont * font, char * name)
{
// Font = new cfont (); // if this parameter is not set, it must be initialized externally; otherwise, the pointer cannot be found.
Logfont lf;
Lf. lfheight = 20; LF. lfwidth = 0;
Lf. lfescapement = 0; LF. lforientation = 0;
Lf. lfweight = 760; LF. lfitalic = 0;
Lf. lfunderline = 0; LF. lfstrikeout = 0;
Lf. lfcharset = 134; LF. lfoutprecision = 3;
Lf. lfclipprecision = 2; LF. lfquality = 1;
Lf. lfpitchandfamily = 2; lstrcpy (LF. lffacename, "");
Lf. lfoutprecision = out_tt_only_precis; // out_tt_precis;

Lf. lfheight = height; // font size
Lstrcpy (LF. lffacename, name); // name
If (font! = NULL)
{
Font-> deleteobject ();
Font-> createfontindirect (& lf );
}
}

# Define say (CH) afxmessagebox (CH) // self-used prompt Function
# Define Bt (CH) setwindowtext (CH) // self-used prompt Function

II. To change the font color, add the system function.
Hbrush cmydlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
// Return HBr;
Switch (nctlcolor)
{
Case ctlcolor_static: // static text
Tchar lpszclassname [255];
Getclassname (pwnd-> m_hwnd, lpszclassname, 255 );
If (_ tcscmp (lpszclassname, trackbar_class) = 0) // The class name is a pull entry
Return cdialog: onctlcolor (PDC, pwnd, nctlcolor );
Else if (idc_tishi = pwnd-> getdlgctrlid () // a prompt is displayed for the specified control ID.
{
PDC-> settextcolor (RGB (255,100,100); // RGB (100,255,100)
Return HBr;
}
Else
{
Pwnd-> getwindowtext (CH, 40 );
If (strcmp (CH, "Test Data") = 0) // The specified title
{
PDC-> settextcolor (RGB (200 ));
Return HBr;
}
PDC-> setbkcolor (RGB (255,255,255 ));
Return HBr; // (hbrush) getstockobject (hollow_brush );
}
}
Break;
Case ctlcolor_btn: // button, as if not
// PDC-> setbkmode (opaque); // The background is not transparent.
// PDC-> setbkmode (transparent); // transparent background
// Say ("button ");
Cstring STR;
// Owen draw // note that the right-click button attribute is changed to self-painted
Pwnd-> getwindowtext (STR); // get the title content
Rect;
Pwnd-> getclientrect (& rect); // obtain the size of the rectangle range.
PDC-> selectstockobject (black_pen );
PDC-> rectangle (& rect); // specifies the outer rectangle of a black stroke.
Rect. Left + = 2; rect. Top + = 2;
Rect. Right-= 2; rect. Bottom-= 2;
PDC-> selectstockobject (white_pen );
PDC-> rectangle (& rect); // a rectangle in a white stroke
PDC-> SelectObject (font); // select the font and size
PDC-> settextcolor (RGB (0,255, 0); // foreground color of the font
PDC-> setbkcolor (RGB (255,); // the background color of the font.
PDC-> drawtext (STR, & rect, dt_center | dt_vcenter | dt_singleline );
Return (hbrush) getstockobject (hollow_brush );
Break;
Case ctlcolor_edit: // edit box
PDC-> settextcolor (RGB (255, 0, 0); // text color
PDC-> setbkcolor (RGB (255,255,255); // text background
Break;
Case ctlcolor_listbox: // list box
// PDC-> settextcolor (RGB (255,255, 0 ));
// PDC-> setbkcolor (RGB (160,60, 0 ));
Break;
}
// Todo: return a different brush if the default is not desired
Return HBr;
}

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.