Chinaunix首页 | 论坛 | 博客
  • 博客访问: 104647414
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: C/C++

2008-04-14 23:31:20

作者:徐景周 来源:赛迪网 

13.如何在指定矩形中旋转显示文字

///////////////////////////////////////////////////////
//说明:
//  在矩形框中旋转方式显示文字,jingzhou xu
//参数: 	
//  pDC:		DC指针
//  str:		显示文字
//  rect:		显示范围
//  angle:		旋转角度
//	nOptions:	ExtTextOut()中相应设置<ETO_CLIPPED 和 ETO_OPAQUE>
///////////////////////////////////////////////////////
void DrawRotatedText(CDC* pDC, const CString str, CRect rect, 
                     double angle, UINT nOptions)
{
   //按比例转换角度值
   double pi = 3.141592654;
   double radian = pi * 2 / 360 * angle;
 
   //获取显示文字中心点
   CSize TextSize = pDC->GetTextExtent(str);
   CPoint center;
   center.x = TextSize.cx / 2;
   center.y = TextSize.cy / 2;
   //计算显示文字新的中心点
   CPoint rcenter;
   rcenter.x = long(cos(radian) * center.x - sin(radian) * center.y);
   rcenter.y = long(sin(radian) * center.x + cos(radian) * center.y);
   //绘制文字
   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);
}

14.如何将32 x 32像素图标转换为16 x 16像素值的图标

HICON Convert32x32IconTo16x16(HICON h32x32Icon)
{
  HDC hMainDC, hMemDC1, hMemDC2;
  HICON h16x16Icon;
  BITMAP bmp;
  HBITMAP hOldBmp1, hOldBmp2;
  ICONINFO IconInfo32x32, IconInfo16x16;
  GetIconInfo(h32x32Icon, &#38;IconInfo32x32);
  hMainDC = ::GetDC(m_hWnd);
  hMemDC1 = CreateCompatibleDC(hMainDC);
  hMemDC2 = CreateCompatibleDC(hMainDC);
  GetObject(IconInfo32x32.hbmColor, sizeof(BITMAP), &#38;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), &#38;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);
  IconInfo16x16.fIcon = TRUE;
  h16x16Icon = CreateIconIndirect(&#38;IconInfo16x16);
  DeleteObject(IconInfo32x32.hbmColor);
  DeleteObject(IconInfo16x16.hbmColor);
  DeleteObject(IconInfo32x32.hbmMask);
  DeleteObject(IconInfo16x16.hbmMask);
  DeleteDC(hMemDC1);
  DeleteDC(hMemDC2);
  ::ReleaseDC(m_hWnd, hMainDC);
  return h16x16Icon;
}

阅读(429) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~