1. How to display multi-line writing in the specified rectangle box
/// // Description: // In the rectangular box, display multi-line text horizontally or vertically, jingzhou Xu. // lmode: arrangement, 0: horizontal mode; 1: Vertical alignment // lhori: horizontally aligned, 0: Left alignment; 1: Central; 2: Right alignment; 3: Customized // Lvert: Vertical alignment, 0: Top alignment; 1: Central; 2: Bottom alignment; 3: Custom /// CRECT DRAWTILEINRECT (CDC * PDC, CSTRING SZSTRING, LPRECT LPRECT, LONG LMODE, Long Lhori, long Lvert) {TextMetric TM; PDC-> GetTextMetrics (& TM); int TmpWidth = Tm.tmavecharWidth, TMPHEIGHT = Tm.TmHeight;
CRECT RCINNER (LPRECT); if (LMODE == 0) {RCINNER.LEFT = TmpWidth; rcinner.right- = tmpWidth; rcinner.top- = tmpWidth; RCINNER.BOTTOM = TmpWidth;} if (LMODE == 1) {RCINNER left = tmpWidth; rcinner.right = RCINNER.LEFT TMPWIDTH; RCINNER.TOP- = TmpWidth; RCINNER.BOTTOM = TmpWidth;}
PDC-> DrawText (Szstring, RCINNER, DT_CALCRECT);
Switch (lhori) {casse 0: Break; case 1: {long xoutcent = (lprect-> right lprect-> left) / 2; long xinnenet = (RCINNER.right RCINNER.LEFT) / 2; RCINNER.LEFT = (xoutcent-xinnce); RCINNER.right = (xoutcent-xinnce);} Break; case 2: {long linwidth = rcinner.right-rcinner.LPRECT; RCINNER.right = lpRect-> Right-TmpWidth; Rcinner.LEFT = RCINNER .right-linwidth;} Break; default: Break;} switch (LVERT) {CASE 0: BREAK; Case 1: {long youtcent = (lprect-> bottom lprect-> TOP) / 2; long yinncent = (RCINNER. Bottom RCINNER.TOP) / 2; RCINNER.TOP - = (Yinncent-YouTCent); RCINNER.BOTTOM - = yInnCent-yOutCent);} break; case 2: {long lInHeigh = rcInner.top-rcInner.bottom; rcInner.bottom = lpRect-> bottom tmpWidth; rcInner.top = rcInner.bottom lInHeigh;} break; default: break }
/ / -------------------------------------------------------------------------------------------- ------------------------------------------- // Function: New, old rectangle, recalculate the number of lines, make text multi-line display, Jingzhou XU // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ------------- // Maximum number of characters in a row int nmaxlinechar = abs (lprect-> right - lprect-> left) / tmpWidth; // Record the width thelinelarth = 0 of the current row; // Record the number of Chinese characters in the current row to prevent half of the Chinese characters from divided into two lines unsigned short halfchinese = 0; for (int i = 0; i <= szString.getLength () - 1; i ) {IF ((( (unsigned char) szstring.getat (i) == 0x0D) && ((unsigned char) szstring.getat (i 1) == 0x0a)) ThelineLength = 0;
// Bythea greater than 0xA1 is Chinese character byte IF ((unsigned char) szstring.getat (i)> = 0xa1) HalfChinese ; thelineLength ;
/ / If the line width is greater than the maximum width per row, special processing if (theLineLength> nmaxlinechar) {// prevents one Chinese character from dividing into two rows, backtracking IF (HalfChinese% 2) {szstring.insert (i, (unsigned char) 0x0a); szstring.insert (i, (unsigned char) 0x0d);} else {szstring.insert (i-1, (unsigned char) 0x0a); szstring.insert (i-1, (unsigned char) 0x0d);} ThelineLength = 0;}}
// Recall the rectangular boundary range // int Tmpline = int (Szstring.getLength () * TmpWidth / ABS (LPRECT-> Right - LPRECT-> LEFT) -0.5)); // TMPLINE = (Szstring.getLength () * TmpWidth% ABS (LPRECT-> Right - LPRECT-> LEFT))? 1: 0; // i (tmpline == 0) // TMPLINE = 1; if (RCINNER.BOTTOM> LPRECT-> BOTTOM) RCINNER .bottom = lpRect-> bottom; if (Rcinner.top
IF (lhori == 0) PDC-> DRAWText (Szstring, Rcinner, DT_WordBreak | DT_LEFT); Else if (Lhori == 1) PDC-> DrawText (Szstring, RCINNER, DT_WORDBREAK | DT_CENTER); Else IF (lhori == 2 PDC-> DrawText (Szstring, RCINNER, DT_WORDBREAK | DT_RIGHT);
Return Rcinner;
2. How to rotate the text in the specified rectangle
///
// Description:
/ / Turn the text in the rectangular box, Jingzhou Xu
//parameter:
// PDC: DC pointer
// Str: Display text
// RECT: Display range
// Angle: Rotation angle
// NOPTIONS: EXTTEXTOUT () Set
///
Void DrawrotatedText (CDC * PDC, Const Cstring Str, CRECT RECT,
Double Angle, Uint NOPTIONS)
{
// Depending on the conversion angle value
Double pi = 3.141592654;
Double Radian = Pi * 2/360 * Angle;
// Get the display text center point
CSIZE TEXTSIZE = PDC-> GetTextExtent (STR);
Cpoint center;
Center.x = Textsize.cx / 2;
Center.y = Textsize.cy / 2;
// Calculate the new center point of the text
Cpoint rCenter;
RCenter.x = long (COS (Radian) * center.x - sin (radian) * center.y);
RCenter.y = long (sin (radian) * center.x cos (radian) * center.y);
// Draw a text
PDC-> setTextAlign (ta_baseline);
PDC-> setbkmode (transparent); PDC-> ExtTextout (Rect.Left Rect.width () / 2 - rCenter.x,
Rect.top Rect.Height () / 2 rCenter.y,
NOPTIONS, RECT, STR, NULL;
}
3. How to convert 32 x 32 pixel icons to 16 x 16 pixel values icon
Hicon Convert32x32iconto16x16 (Hicon H32x32icon)
{
HDC HMAINDC, HMEMDC1, HMEMDC2;
Hicon H16X16CI;
Bitmap BMP;
Hbitmap HoldBMP1, HoldBMP2;
Iconfo iconInfo32x32, iconinfo16x16;
GeticonInfo (H32X32ICON, & iconInfo32x32);
HmainDC = :: getDC (m_hwnd);
HMEMDC1 = CREATECOMPATIBLEDC (HMAINDC);
HMEMDC2 = CreateCompatibleDC (HMAINDC);
GetObject (iconInfo32x32.hbmcolor, sizeof (bitmap), & bmp);
IconInfo16x16.hbmcolor = CreateBitmap (16, 16,
Bmp.bmplanes,
BMP.BMBITSPIXEL,
NULL);
HoldBMP1 = (hbitmap) SelectObject (HMEMDC1,
IconInfo32x32.hbmcolor);
HoldBMP2 = (hbitmap) SelectObject (HMEMDC2,
IconInfo16x16.hbmcolor);
Stretchblt (HMEMDC2,
0, 0,
16, 16,
HMEMDC1,
0, 0,
32, 32,
SRCCopy
);
GetObject (iconInfo32x32.hbmmask, sizeof (bitmap), & bmp);
IconInfo16x16.hbmmask = CreateBitmap (16, 16,
Bmp.bmplanes,
BMP.BMBITSPIXEL,
NULL);
SelectObject (HMEMDC1, IconInfo32x32.hbmmask);
SelectObject (hmemdc2, iconinfo16x16.hbmmask);
Stretchblt (HMEMDC2,
0, 0,
16, 16,
HMEMDC1,
0, 0,
32, 32,
SRCCopy
);
SelectObject (HMEMDC1, HoldBMP1);
SelectObject (HMEMDC2, HoldBMP2);
Iconfo16x16.ficon = true;
H16X16CION = CREATEICONIIRECT (& iconInfo16x16);
DeleteObject (iconInfo32x32.hbmcolor);
DeleteObject (iconInfo16x16.hbmcolor);
DeleteObject (iconInfo32x32.hbmmask);
DeleteObject (iconInfo16x16.hbmmask);
Deletedc (HMEMDC1);
Deletedc (HMEMDC2);
:: ReleaseDC (M_HWND, HMAINDC);
Return h16x16icon;
}
4. How to build a grayscale icon
Hicon Creategrayscaleicon (HiCon Hicon)
{
Hicon hgrayicon = null;
HDC hmaindc = null,
hmemdc1 = null,
HMEMDC2 = NULL;
Bitmap BMP;
Hbitmap HoldBMP1 = NULL,
HoldBMP2 = NULL;
IconInfo CSII, CSGRAYII;
Bool BretValue = FALSE;
BretValue = :: GeticonInfo (Hicon, & CSII);
IF (BretValue == false) Return NULL;
HmainDC = :: getDC (m_hwnd);
HMEMDC1 = :: CreateCompatibleDC (Hmaindc);
HMEMDC2 = :: CreateCompatibleDC (Hmaindc);
IF (hmaindc == null ||
HMEMDC1 == Null ||
HMEMDC2 == NULL)
Return NULL;
IF (:: getObject (csii.hbmcolor,
SizeOf (Bitmap), &
AMP; BMP))
{
CSGRAYI.HBMCOLOR =
:: CreateBitmap (CSII.XHOTSPOT * 2,
CSII.YHOTSPOT * 2,
Bmp.bmplanes,
BMP.BMBITSPIXEL,
NULL);
IF (csgrayi.hbmcolor)
{
HoldBMP1 =
(Hbitmap) :: SelectObject (hmemdc1,
CSIi.hbmcolor);
HoldBMP2 =
(Hbitmap) :: SelectObject (hmemdc2,
CSGRAYI.HBMCOLOR);
:: Bitblt (Hmemdc2, 0, 0, CSII.XHOTSPOT * 2,
CSII.YHOTSPOT * 2, HMEMDC1, 0, 0,
Srccopy);
DWORD DWLOOPY = 0, dwloopx = 0;
ColorRef crpixel = 0;
BYTE BYNEWPIXEL = 0;
For (dwloopy = 0; dwloopy { For (dwloopx = 0; dwloopx { CRPixel = :: getpixel (hmemdc2, dwloopx, dwloopy); BYNewpixel = (BYTE) (GetRvalue (CRPixel) * 0.299) (GETGVALUE (CRPIXEL) * 0.587) GetBValue (CRPIXEL) * 0.114)); IF (crpixel) :: setPixel (HMEMDC2, Dwloopx, Dwloopy, RGB (Bynewpixel, Bynewpixel, BYNewpixel); } // for } // for :: SelectObject (HMEMDC1, HOLDBMP1); :: SelectObject (HMEMDC2, HoldBMP2); Csgrayii.hbmmask = csii.hbmmask; CSGRAYII.FICON = TRUE; Hgrayicon = :: Createicon Indirect (& CSGRAYI); } // if :: DeleteObject (csgrayii.hbmcolor); //:: deleteObject(CSGRAYI.HBMMASK); } // if :: DeleteObject (csii.hbmcolor); :: deleteObject (csii.hbmmask); :: Deletedc (HMEMDC1); :: Deletedc (HMEMDC2); :: ReleaseDC (M_HWND, HMAINDC); Return hgraphicon; } 5. How to rotate in the specified angle to display the memory bitmap (usage and Bitblt) Void Rotblt (HDC DESTDC, INT SRCX1, INT SRCY1, INT SRCX2, INT SRCY2, HDC SRCDC, Int Destx1, Int Desty1, Int ThetaIndegrees, DWORD MODE { Double Theta = THHINDEGREES * (3.14159 / 180); // Original image original size Int width = SRCX2 - SRCX1; INT Height = SRCY2 - SRCY1; // Original image center point INT CENTREX = INT (Float (SRCX2 SRCX1) / 2); INT CENTREY = INT (Float (SRCY2 SRCY1) / 2); / / Judgment the rectangular frame that can be rotated in any direction IF (width> height) height = width; Else Width = height; HDC MEMDC = CREATECOMPATIBLEDC (DESTDC); Hbitmap membmp = createcompatiblebitmap (destdc, width, height); Hbitmap obmp = (hbitmap) SelectObject (MEMDC, MEMBMP); // Memory DC new in central point INT newcentre = int (float (width) / 2); // Start rotation For (int x = srcx1; x <= srcx2; x ) For (int y = srcy1; y <= srcy2; y ) { ColorRef color (SRCDC, X, Y); INT NEWX = INT * SIN (THHETA) (Y-Centrey) * COS (Theta)); INT newy = int ((x-centralrex) * cos (ly) - (Y-Centrey) * sin (theeta)); SetPixel (MEMDC, NewX Newcentre, Newy Newcentre, Col); } // Copy to the target DC Bitblt (Destdc, DestX1, Desty1, Width, Height, MEMDC, 0, 0, MODE); / / Release memory SELECTOBJECT (MEMDC, OBMP); DELETEDC (MEMDC); DeleteObject (MEMBMP); } usage: Rotblt (DC, 0, 0, 150, 150, MEMDC, 200, 0, 45, SRCCopy); 6. How to copy the specified form to the system shear board in a bitmap Void cscreensnapdlg :: TOCLIPBOARD_BIO (CWND * WND, BOOL FULLWND) { CDC * DC; IF (FullWnd) {/ * Cut the entire window * / DC = New CWINDOWDC (WND);} / * Scratch the entire window * / Else {/ * Only grabborn * / DC = New CclientDC (WND); } / * Only capture the client area * / CDC MEMDC; MEMDC.CREATECOMPALDC (DC); CBITMAP BM; CRECT R; IF (FullWnd) WND-> GetWindowRect (& R); Else WND-> GetClientRect (& R); CString S; WND-> getWindowText (s); CSIZE SZ (R.Width (), R.HEIGHT ()); Bm.createCompaTibleBitmap (DC, SZ.CX, SZ.CY); CBITMAP * OLDBM = MEMDC.SELECTOBJECT (& BM); Memdc.bitblt (0, 0, SZ.CX, SZ.CY, DC, 0, 0, SRCCOPY); // Call directly OpenClipboard () without WND-> getParent () -> OpenClipboard (); WND-> OpenClipboard (); :: EmptyClipboard (); :: setClipboardData (cf_bitmap, bm.m_hobject); CloseClipboard (); / / Restore the original environment MEMDC.SELECTOBJECT (OLDBM); bm.detach (); DELETE DC; }