49, how to achieve a rectangle of a rubber area
CRECTTRACKER is a very useful class that can create a rectangle of a rubber region by calling CRECTRACKER :: TrackRubberband to respond to WM_LBUTTONDOWN messages. The following example shows that the size of the blue ellipse in the window using CRECTRACKER is easy.
First, declare a CRECTRACKER data member in the file file:
Class CsampleView: Public CVIEW
{
...
PUBLIC:
CRECTTRACKER M_TRACKER;
...
}
Second, the CRECTRACKER object is initialized in the texture class constructor:
Csampledoc :: csampledoc ()
{
// Initialize Tracker Position, Size and Style.
m_tracker.m_rect.seTRECT (0, 0, 10, 10);
M_Tracker.m_nStyle = CRECTTRACKER :: ResizeInde |
CRECTTRACKER :: DottedLine;
}
Then, draw an ellipse and trace rectangle in the onDraw function:
Void CsampleView :: OnDraw (CDC * PDC)
{
CSAMPLEDOC * PDOC = getDocument ();
Ask_VALID (PDOC);
// Select Blue Brush INTO Device Context.
CBRUSH Brush (RGB (0, 0, 255);
CBRUSH * POLDBRUSH = PDC-> SelectObject (& brush);
// Draw Ellipse In TRACKING Rectangle.
CRECT RCELLIPSE;
PDOC-> M_Tracker.gettrueeRect (rCellipse);
PDC-> Ellipse (rCellipse);
// Draw Tracking Rectangle.
PDOC-> M_Tracker.draw (PDC);
// Select Blue Brush Out of Device Context.
PDC-> SELECTOBJECT (POLDBRUSH);
}
Finally, use the ClassWizard to process the WM_LBUTTONDOWN message and add the following code. This section can drag and drop, move, or reset the size of the ellipse according to the mouse button.
Void csampleview :: ONLBUTTONDOWN (UINT NFLAGS, CPOINT POINT)
{
// Get Pointer to Document.
CSAMPLEDOC * PDOC = getDocument ();
Ask_VALID (PDOC);
// if Clicked On Ellipse, Drag Or Resize It. OtherWise Create A
// Rubber-Band Rectangle Nd Create A New Ellipse.
Bool Bresult = pdoc-> m_tracker.hittest (Point)! =
CRECTTRACKER :: HITNOTHING;
// Tracker Rectangle Changed SO Update Views.
IF (BRESULT)
{
PDOC-> M_Tracker.track (this, Point, True);
PDOC-> setmodifiedflag ();
PDOC-> UpdateAllViews (NULL);
}
Else
PDOC-> M-Tracker.tckrubberband (this, point, true); cview :: ONLBUTTONDOWN (NFLAGS, POINT);
}
50, how to update the text of flip background color
Call CDC :: SetBkmode and transfer Opaque with current background color fill background, or call CDC :: setBkmode and transfer Transpaarent to keep the background unchanged, both methods can set background mode. The following example sets the background mode to transparent, you can update the string twice, with a black shadow to update the text with a flower color. The black string after the red string, but because the background mode is still visible.
Void CsampleView :: OnDraw (CDC * PDC)
{
// DETERMINT SIZE OF VIEW.
CRECT RCVIEW;
GetClientRect (rcvieew);
// Create Sample String to Display.
CSTRING STR (_t ("Awesome Shadow Text ...");
// set the background mode to transparent.
PDC-> setbkmode (transparent);
// Draw Black Shadow Text.
RcView.offsetRect (1, 1);
PDC-> SetTextColor (RGB (0, 0, 0));
PDC-> DrawText (Str, Str.GetLength (), RcView,
DT_SINGLINE | DT_CENTER | DT_VCENTER);
// Draw red text.
RcView.offsetRect (-1, -1);
PDC-> SetTextColor (RGB (255, 0, 0));
PDC-> DrawText (Str, Str.GetLength (), RcView,
DT_SINGLINE | DT_CENTER | DT_VCENTER);
}
51, how to create a font with a specific point size
You can specify the size of the font logic unit, but sometimes the size of the point specified by the font may be more convenient. You can convert the point of the font into the height of the font:
INT NHEIGTH = MULDIV (NpointSize, -dc.getDeviceCaps (Logpixelsy), 72);
The following example creates an 8-point APIAL font:
CclientDC DC (AqfxgetMainWnd ());
M_Font. CreateFont (Muldiv (8, -dc.getDeviceCaps (Logpixelsy),
72), 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET,
OUT_STROKE_PRECIS, CLIP_STROKE_PRECIS, Draft_Quality,
Variable_pitch | ff-swiss, _t ("arial));
52. How to calculate a sly size
The function cdc :: DET text extent calculates the height and width of a string according to the currently selected font. If not the system font but other font, it is important to select the font to select the device context before calling GetTexTextent, otherwise calculate the height and width, according to the system font, the result of this is of course incorrect. . The following sample program When changing the title of the lower pressure button, the size of the dynamic adjustment button is changed by the size of the button by the font and the size of the button. Turn ONSETTEXT when responding to WM_SETTEXT, which uses the user-defined message defined by the ON_MESSAE macro.
LResult CMyButton :: OnsetText (WPARAM WPARAM, LPARAM LPARAM) {
// Pass message to window procedure.
Lresult BRESULT = CallWindowProc (* getsuperWndProcAddr (),
M_hwnd, getCurrentMessage () -> Message, WParam, LPARAM;
// Get Title of Push Button.
CString startle;
GetWindowText (Strtertle);
// Select Current Font Into Device Context.
CDC * PDC = Getdc ();
Cfont * pfont = getFont ();
CFont * PoldFont = PDC-> SelectObject (PFont);
// Calculate size of title.
CSIZE SIZE = PDC-> GetTexTexent (STRTITE, STRTILE.GETLENGTH ());
// Adjust The Button's size based on its title.
// Add A 5-Pixel Border Around The Button.
SetWindowPos (NULL, 0, 0, SIZE.CX 10, Size.cy 10,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
// Clean Up.
PDC-> SELECTFONT (POLDFONT);
ReleaseDC (PDC);
Return BRESULT;
}
53, how to display rotary text
The rotation text can be displayed as long as the user uses TrueType or GDI pen or font (some hardware devices also support rotary raster fonts). The IFESCAPEMENT member in the logfont structure specifies the angle of the text line and the X-axis, the unit of the angle is one-tenth of the degree rather than the degree, for example, ifscapement is 450 indicates 45 degrees of the font. To ensure that all fonts rotate in the same direction of the coordinate system, be sure to set the clip_lh_angles bit of the IFESCAPEMENT member, otherwise some fonts may rotate in reverse. The following example uses a 14-point Arial font to draw a string per spacer.
Void CsampleView :: OnDraw (CDC * PDC)
{
// determine the size of the window.
CRECT RCCLIENT;
GetClientRect (rcclient);
// CREATE SAMPLE STRING.
CSTRING STR (_T ("Wheeee ... I am rotating!"));
// Draw Transparent, Red Text.
PDC-> setbkmode (transparent);
PDC-> SetTextColor (RGB (255, 0, 0));
CFONT font; // font object
Logfont stfont; // font definition
// set Font Attributes That Will Not Change.
MEMSET (& Stfont, 0, Sizeof (logfont));
StFont.ifHeight = MULDIV (14, -PDC-> GetDeviceCaps (Logpixelsy), 72)
StFont.ifweight = fw_normal;
Stfont.ifclipprecision = LCIP_LH_ANGLES
STRCPY (stfont.lffacename, "arial"); // Draw Text At 15degree Interval.
For (int Nangle = 0; Nangle <3600; Nangle = 150)
{
// Specify new Angle.
stfont.lfescapement = nangle;
// CREATE AND SELECT FONT INTO DC.
Font.createFontIndirect (& Stfont);
Cfont * PoldFont = PDC-> SelectObject (& font);
// Draw the text.
PDC-> SELECTOBJECT (POLDFONT);
Font.DelectObjext ();
}
}
54, how to correctly display the string containing the label character
When you call the GDI text Painting function, you need to expand the tag character, which can be done by calling CDC :: TabbedTextout or CDC :: DrawText and specifies the DT_ExpandTabs flag. The TabbedTextout function allows the specified array of labels, the following example specifies a label every 20 device unit:
Void CsampleView :: OnDraw (CDC * PDC)
{
Ctestdoc * pdoc = getDocument ();
Ask_VALID (PDOC);
CString Str;
Str.Format ("Cathy / Tnorman / Toliver");
INT NTABSTOP = 20; // Tabs Are Evey 20 Pixels
PDC-> TabbedTextout (10, 10, Str, 1, & NTABSTOP, 10);
}
55. How to display a omitted number at the end of the string too long?
Call CDC :: DRAWTEXT and specify the DT_END_ELLIPSIS flag so that the character of the string ends can be used to suck the specified boundary rectangle. If you want to display the path information, specify the DT_END_ELLIPSIS flag and submit the characters in the middle of the string.
Void CsampleView :: OnDraw (CDC * PDC)
{
Ctestdoc * pdoc = getDocument ();
Ask_VALID (PDOC);
// Add Ellpsis to end of string if it does not fit
PDC-> DRAWTEXT (CSTRING ("this is a long string"),
CRECT (10, 10, 80, 30), DT_LEFT | DT_END_ELLIPSIS);
// Add Ellpsis to Middle of String if it does not fit
PDC-> DRAWTEXT (AFXGetApp () -> m_pszhelpfilepath,
CRECT (10, 40, 200, 60), DT_LEFT | DT_PATH_ELLIPSIS);
}
56. How to quickly format a CString object
Call CString :: Format, the function and the printf function have the same parameters, the following example shows how to use the Format function:
// Get size of window.
CRECT RCWINDOW;
GetWindowRect (rcwindow);
// Format Message String.
Cstring strmessage;
StrMessage.Format (_T ("Window size (% D,% D)"),
RcWindow.Width (), rcwindow.height ()); // Display the message.
MessageBox (StrMessage);