VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 302|回复: 0

关于SDI重绘窗口的问题

[复制链接]
89_avatar_middle
最佳答案
2 
在线会员 发表于 2022-4-1 18:50:51 | 显示全部楼层 |阅读模式
6驿站币
本帖最后由 lwei2 于 2022-4-1 18:54 编辑

如题,最近在用SDI做应用程序,想要做的效果如下截图:

通过学习和借鉴一些自绘的例子,我的代码如下


  1. #include "StdAfx.h"
  2. #include "MyFrameWnd.h"

  3. #define  ID_CLOSE_BTN 110
  4. #define  ID_MAX_BTN   111
  5. #define  ID_MIN_BTN   112

  6. // CMyFrameWnd

  7. IMPLEMENT_DYNCREATE(CMyFrameWnd, CFrameWndEx)

  8. CMyFrameWnd::CMyFrameWnd()
  9. {
  10.         m_nTitleHeight = 20;// 38;
  11.         m_nFrameWidth = 3;

  12.         Image *pImage = CSkinManager::GetInstance()->GetSkinItem(_T("dialog\\minimize.bmp"));
  13.         m_WindowBtnImage.push_back(pImage);

  14.         pImage = CSkinManager::GetInstance()->GetSkinItem(_T("dialog\\maxmize.bmp"));
  15.         m_WindowBtnImage.push_back(pImage);

  16.         pImage = CSkinManager::GetInstance()->GetSkinItem(_T("dialog\\close.bmp"));       
  17.         m_WindowBtnImage.push_back(pImage);

  18.         m_pBkImage = CSkinManager::GetInstance()->GetSkinItem(_T("dialog\\DialogBkg.png"));

  19.         m_iXButtonHovering = -1;

  20.         m_bNCHovering = FALSE;
  21.         m_bNCTracking = FALSE;

  22.         m_rcIcon = CRect(15, 13, 15 + GetSystemMetrics(SM_CXSMICON), 13 + GetSystemMetrics(SM_CYSMICON));
  23. }

  24. CMyFrameWnd::~CMyFrameWnd()
  25. {
  26. }


  27. BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWndEx)
  28.         ON_WM_NCPAINT()
  29.         ON_WM_NCCALCSIZE()
  30.         ON_WM_NCHITTEST()
  31.         ON_WM_GETMINMAXINFO()
  32.         ON_WM_NCACTIVATE()
  33.         ON_WM_ACTIVATE()
  34.         ON_WM_NCLBUTTONDBLCLK()
  35.         ON_WM_NCLBUTTONDOWN()
  36.         ON_WM_NCLBUTTONUP()
  37.         ON_WM_NCMOUSEMOVE()
  38.         ON_WM_NCMOUSEHOVER()
  39.         ON_WM_NCMOUSELEAVE()
  40.         ON_WM_SIZE()
  41.         ON_WM_SYSCOMMAND()
  42.         ON_WM_NCCREATE()
  43. END_MESSAGE_MAP()


  44. // CMyFrameWnd message handlers
  45. void CMyFrameWnd::OnNcPaint()
  46. {
  47.         CWindowDC dc(this);

  48.         if (m_pBkImage == NULL)
  49.         {
  50.                 return;
  51.         }

  52.         CDC MemDC;

  53.         // 获取位置
  54.         CRect rcWindow;
  55.         GetWindowRect(&rcWindow);
  56.         rcWindow.OffsetRect(-rcWindow.left, -rcWindow.top);

  57.         CRect rcClient;
  58.         GetClientRect(&rcClient);

  59.         //剪除掉客户区
  60.         rcClient.OffsetRect(m_nFrameWidth, m_nTitleHeight);
  61.         dc.ExcludeClipRect(rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);

  62.         MemDC.CreateCompatibleDC(&dc);

  63.         CBitmap bmp;
  64.         bmp.CreateCompatibleBitmap(&dc, rcWindow.Width(), rcWindow.Height());
  65.         MemDC.SelectObject(&bmp);
  66.         MemDC.SetBkMode(TRANSPARENT);

  67.         Gdiplus::Graphics graphics(MemDC.GetSafeHdc());
  68.         RectF destRect;

  69.         //绘制底部和边框
  70.         destRect.X = 0;
  71.         destRect.Y = rcWindow.Height() - m_nFrameWidth;
  72.         destRect.Width = rcWindow.Width();
  73.         destRect.Height = m_nFrameWidth;
  74.         graphics.DrawImage(m_pBkImage, destRect, m_nTitleHeight, m_pBkImage->GetHeight() - m_nFrameWidth, m_nFrameWidth, m_nFrameWidth, UnitPixel);

  75.         destRect.X = rcWindow.right - m_nTitleHeight;
  76.         destRect.Y = rcWindow.bottom - m_nTitleHeight;
  77.         destRect.Width = m_nTitleHeight;
  78.         destRect.Height = m_nTitleHeight;
  79.         graphics.DrawImage(m_pBkImage, destRect, m_pBkImage->GetWidth() - m_nTitleHeight, m_pBkImage->GetHeight() - m_nTitleHeight, m_nTitleHeight, m_nTitleHeight, UnitPixel);

  80.         destRect.X = 0;
  81.         destRect.Y = rcWindow.Height() - m_nTitleHeight;
  82.         destRect.Width = m_nTitleHeight;
  83.         destRect.Height = m_nTitleHeight;
  84.         graphics.DrawImage(m_pBkImage, destRect, 0, m_pBkImage->GetHeight() - m_nTitleHeight, m_nTitleHeight, m_nTitleHeight, UnitPixel);

  85.         //左边框
  86.         destRect.X = 0;
  87.         destRect.Y = m_nTitleHeight;
  88.         destRect.Width = m_nFrameWidth;
  89.         destRect.Height = rcWindow.Height() - 2 * m_nTitleHeight;
  90.         graphics.DrawImage(m_pBkImage, destRect, 0, m_nTitleHeight, m_nFrameWidth, m_nFrameWidth, UnitPixel);

  91.         //右边框
  92.         destRect.X = rcWindow.Width() - m_nFrameWidth;
  93.         destRect.Y = m_nTitleHeight;
  94.         destRect.Width = m_nFrameWidth;
  95.         destRect.Height = rcWindow.Height() - 2 * m_nTitleHeight;
  96.         graphics.DrawImage(m_pBkImage, destRect, m_pBkImage->GetWidth() - m_nFrameWidth, m_nTitleHeight, m_nFrameWidth, m_nFrameWidth, UnitPixel);

  97.         //绘制标题栏
  98.         destRect.X = 0;
  99.         destRect.Y = 0;
  100.         destRect.Width = rcWindow.Width();
  101.         destRect.Height = m_nTitleHeight;
  102.         graphics.DrawImage(m_pBkImage, destRect, 50, 0, 5, m_nTitleHeight, UnitPixel);
  103.         graphics.DrawImage(m_pBkImage, 0, 0, 0, 0, 50, m_nTitleHeight, UnitPixel);
  104.         graphics.DrawImage(m_pBkImage, rcWindow.Width() - 50, 0, m_pBkImage->GetWidth() - 50, 0, 50, m_nTitleHeight, UnitPixel);

  105.         int xPos = 5;
  106.         int yPos = 5;

  107.         HICON hIcon = GetIcon(FALSE);
  108.         if (hIcon)
  109.         {
  110.                 DrawIconEx(MemDC, xPos, yPos - 2, hIcon, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);

  111.                 xPos += GetSystemMetrics(SM_CXSMICON);
  112.                 xPos += 5;
  113.         }

  114.         CString strText;
  115.         GetWindowText(strText);
  116.         if (!strText.IsEmpty())
  117.         {
  118.                 //绘制标题
  119.                 LOGFONT lfFont;
  120.                 memset(&lfFont, 0, sizeof(lfFont));
  121.                 lfFont.lfHeight = 12;
  122.                 lfFont.lfWeight |= FW_THIN;
  123.                 lstrcpy(lfFont.lfFaceName, _T("宋体"));
  124.                 Gdiplus::Font font(dc.GetSafeHdc(), &lfFont);

  125.                 StringFormat stringFormat;
  126.                 stringFormat.SetFormatFlags(StringFormatFlagsNoWrap);
  127.                 stringFormat.SetAlignment(StringAlignmentNear);
  128.                 stringFormat.SetLineAlignment(StringAlignmentNear);
  129.                 CStringW strTitle(strText);
  130.                 SolidBrush brush((ARGB)Color::White);
  131.                 PointF point;

  132.                 point.X = (Gdiplus::REAL)xPos;
  133.                 point.Y = (Gdiplus::REAL)yPos;
  134.                 graphics.DrawString(strTitle, strTitle.GetLength(), &font, point, &stringFormat, &brush);

  135.         }

  136.         CRect rcPaint;
  137.         dc.GetClipBox(&rcPaint);
  138.         std::map<int, CMyButton>::iterator iter;
  139.         for (iter = m_TitleBtn.begin(); iter != m_TitleBtn.end(); iter++)
  140.         {
  141.                 CMyButton& dcControl = iter->second;
  142.                 CRect rcControl;
  143.                 dcControl.GetRect(&rcControl);

  144.                 // 判断当前按钮是否需要重绘
  145.                 if (!rcPaint.IsRectEmpty() && !CRect().IntersectRect(&rcControl, rcPaint))
  146.                 {
  147.                         continue;
  148.                 }

  149.                 dcControl.DrawButton(graphics);
  150.         }

  151.         dc.BitBlt(0, 0, rcWindow.Width(), rcWindow.Height(), &MemDC, 0, 0, SRCCOPY);

  152.         dc.SelectClipRgn(NULL);
  153. }

  154. //处理标题栏大小
  155. void CMyFrameWnd::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp)
  156. {
  157.         if (bCalcValidRects)
  158.         {
  159.                 CRect& rc = (CRect&)lpncsp->rgrc[0];//get the client rectangle   
  160.                 rc.top += m_nTitleHeight;
  161.                 rc.left += m_nFrameWidth;
  162.                 rc.bottom -= m_nFrameWidth;
  163.                 rc.right -= m_nFrameWidth;

  164.         }
  165. }

  166. LRESULT CMyFrameWnd::OnNcHitTest(CPoint point)
  167. {
  168.         CRect rtButton;
  169.         CRect rcWindow;
  170.         GetWindowRect(&rcWindow);
  171.         CPoint pt = point;
  172.         pt.x -= rcWindow.left;
  173.         pt.y -= rcWindow.top;

  174.         int iButton = TButtonHitTest(pt, rtButton);
  175.         if (iButton != -1)
  176.         {
  177.                 switch (iButton)
  178.                 {
  179.                 case ID_CLOSE_BTN:
  180.                 {
  181.                         return HTCLOSE;
  182.                 }
  183.                 break;
  184.                 case ID_MAX_BTN:
  185.                 {

  186.                         return HTMAXBUTTON;
  187.                 }
  188.                 break;
  189.                 case ID_MIN_BTN:
  190.                 {
  191.                         return HTMINBUTTON;
  192.                 }
  193.                 break;
  194.                 }
  195.         }
  196.         else if (m_rcIcon.PtInRect(pt))
  197.         {
  198.                 return HTSYSMENU;
  199.         }

  200.         return CFrameWndEx::OnNcHitTest(point);
  201. }

  202. void CMyFrameWnd::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
  203. {
  204.         lpMMI->ptMinTrackSize.y = m_nTitleHeight + m_nFrameWidth;
  205.         CFrameWnd::OnGetMinMaxInfo(lpMMI);
  206. }

  207. BOOL CMyFrameWnd::OnNcActivate(BOOL /*bActive*/)
  208. {
  209.         OnNcPaint();
  210.         return TRUE;
  211. }

  212. void CMyFrameWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
  213. {
  214.         CFrameWndEx::OnActivate(nState, pWndOther, bMinimized);

  215.         OnNcPaint();
  216. }

  217. void CMyFrameWnd::OnNcLButtonDblClk(UINT /*nHitTest*/, CPoint point)
  218. {
  219.         CRect rcWindow;
  220.         GetWindowRect(&rcWindow);
  221.         rcWindow.bottom = rcWindow.top + m_nTitleHeight;

  222.         CPoint pt = point;
  223.         CRect rtButton;
  224.         int iButton = TButtonHitTest(pt, rtButton);
  225.         if (iButton != -1)
  226.         {
  227.                 m_TitleBtn[iButton].LButtonDown();
  228.                 OnNcPaint();
  229.                 return;
  230.         }

  231.         if (rcWindow.PtInRect(pt))
  232.         {
  233.                 if (IsZoomed())
  234.                 {
  235.                         ShowWindow(SW_RESTORE);
  236.                 }
  237.                 else
  238.                 {
  239.                         ShowWindow(SW_MAXIMIZE);
  240.                 }

  241.                 OnNcPaint();
  242.                 return;
  243.         }

  244.         OnNcPaint();
  245. }

  246. void CMyFrameWnd::OnNcLButtonDown(UINT nHitTest, CPoint point)
  247. {
  248.         CRect rtButton;
  249.         CRect rcWindow;
  250.         GetWindowRect(&rcWindow);
  251.         CPoint pt = point;
  252.         pt.x -= rcWindow.left;
  253.         pt.y -= rcWindow.top;

  254.         int iButton = TButtonHitTest(pt, rtButton);
  255.         if (iButton != -1)
  256.         {
  257.                 m_TitleBtn[iButton].LButtonDown();
  258.                 OnNcPaint();
  259.                 return;
  260.         }


  261.         if (m_rcIcon.PtInRect(pt))
  262.         {
  263.                 CMenu *pSysMenu = GetSystemMenu(FALSE);
  264.                 if (pSysMenu)
  265.                 {
  266.                         pSysMenu->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this, NULL);
  267.                 }
  268.                 return;
  269.         }
  270.         else if (nHitTest >= HTLEFT && nHitTest <= HTBOTTOMRIGHT || nHitTest == HTCAPTION)
  271.         {
  272.                 CFrameWnd::OnNcLButtonDown(nHitTest, point);
  273.                 return;
  274.         }


  275.         CFrameWndEx::OnNcLButtonDown(nHitTest, point);
  276. }

  277. void CMyFrameWnd::OnNcLButtonUp(UINT nHitTest, CPoint point)
  278. {
  279.         CRect rtButton;
  280.         CRect rcWindow;
  281.         GetWindowRect(&rcWindow);
  282.         CPoint pt = point;
  283.         pt.x -= rcWindow.left;
  284.         pt.y -= rcWindow.top;

  285.         int iButton = TButtonHitTest(pt, rtButton);
  286.         if (iButton != -1)
  287.         {
  288.                 switch (iButton)
  289.                 {
  290.                 case ID_CLOSE_BTN:
  291.                 {
  292.                         SendMessage(WM_SYSCOMMAND, SC_CLOSE, 0);
  293.                 }
  294.                 break;
  295.                 case ID_MAX_BTN:
  296.                 {
  297.                         if (IsZoomed())
  298.                         {
  299.                                 SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
  300.                         }
  301.                         else
  302.                         {
  303.                                 SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  304.                         }
  305.                 }
  306.                 break;
  307.                 case ID_MIN_BTN:
  308.                 {
  309.                         SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
  310.                 }
  311.                 break;
  312.                 default:
  313.                 {
  314.                         m_TitleBtn[iButton].LButtonUp();
  315.                         OnNcPaint();
  316.                 }
  317.                 break;
  318.                 }
  319.         }
  320.         else
  321.         {
  322.                 CFrameWndEx::OnNcLButtonUp(nHitTest, point);
  323.         }
  324. }

  325. void CMyFrameWnd::OnNcMouseMove(UINT nHitTest, CPoint point)
  326. {
  327.         if (!m_bNCTracking)
  328.         {
  329.                 TRACKMOUSEEVENT tme;
  330.                 tme.cbSize = sizeof(tme);
  331.                 tme.hwndTrack = m_hWnd;
  332.                 tme.dwFlags = TME_NONCLIENT;
  333.                 tme.dwHoverTime = 1;
  334.                 m_bNCTracking = _TrackMouseEvent(&tme);
  335.         }

  336.         if (nHitTest >= HTLEFT && nHitTest <= HTBOTTOMRIGHT ||
  337.                 nHitTest == HTCAPTION)
  338.         {
  339.                 CFrameWnd::OnNcMouseMove(nHitTest, point);

  340.         }

  341.         CRect rtButton;
  342.         CRect rcWindow;
  343.         GetWindowRect(&rcWindow);
  344.         point.x -= rcWindow.left;
  345.         point.y -= rcWindow.top;
  346.         int iButton = TButtonHitTest(point, rtButton);
  347.         if (iButton != m_iXButtonHovering)
  348.         {
  349.                 if (m_iXButtonHovering != -1)
  350.                 {
  351.                         m_TitleBtn[m_iXButtonHovering].MouseLeave();
  352.                         m_iXButtonHovering = -1;
  353.                 }
  354.                 if (iButton != -1)
  355.                 {
  356.                         m_iXButtonHovering = iButton;
  357.                         m_TitleBtn[m_iXButtonHovering].MouseHover();
  358.                 }

  359.                 OnNcPaint();
  360.         }
  361. }

  362. void CMyFrameWnd::OnNcMouseHover(UINT /*nFlags*/, CPoint /*point*/)
  363. {
  364.         m_bNCHovering = TRUE;
  365.         //CFrameWndEx::OnNcMouseHover(nFlags, point);
  366. }

  367. void CMyFrameWnd::OnNcMouseLeave()
  368. {
  369.         m_bNCTracking = FALSE;
  370.         m_bNCHovering = FALSE;

  371.         if (m_iXButtonHovering != -1)
  372.         {
  373.                 m_TitleBtn[m_iXButtonHovering].MouseLeave();
  374.                 m_iXButtonHovering = -1;
  375.         }

  376.         //CFrameWndEx::OnNcMouseLeave();
  377. }

  378. void CMyFrameWnd::OnSize(UINT nType, int cx, int cy)
  379. {
  380.         CFrameWndEx::OnSize(nType, cx, cy);

  381.         if (nType != SIZE_MINIMIZED && nType != SIZE_MAXHIDE)
  382.         {
  383.                 if (m_Rgn.GetSafeHandle())
  384.                 {
  385.                         m_Rgn.DeleteObject();
  386.                 }

  387.                 CRect rc;
  388.                 GetWindowRect(&rc); //获得窗口矩形
  389.                 rc -= rc.TopLeft();
  390.                 m_Rgn.CreateRoundRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1, 5, 5); //根据窗口矩形创建一个圆角矩形
  391.                 SetWindowRgn(m_Rgn, TRUE); //根据圆角矩形指定的区域改变窗口的形状
  392.         }

  393.         CRect rcWnd;
  394.         GetWindowRect(&rcWnd);
  395.         rcWnd.OffsetRect(-rcWnd.left, -rcWnd.top);

  396.         CRect rMin(rcWnd.right - 74, 8, rcWnd.right - 54, 30);
  397.         m_TitleBtn[ID_MIN_BTN].SetRect(rMin);
  398.         CRect rMax(rcWnd.right - 52, 8, rcWnd.right - 32, 30);
  399.         m_TitleBtn[ID_MAX_BTN].SetRect(rMax);
  400.         CRect rClose(rcWnd.right - 30, 8, rcWnd.right - 10, 30);
  401.         m_TitleBtn[ID_CLOSE_BTN].SetRect(rClose);

  402.         if (nType == SIZE_MAXIMIZED || nType == SIZE_RESTORED)
  403.         {
  404.                 OnNcPaint();
  405.         }

  406. }

  407. void CMyFrameWnd::OnSysCommand(UINT nID, LPARAM lParam)
  408. {
  409.         if (nID != SC_CLOSE)
  410.         {
  411.                 OnNcPaint();
  412.         }

  413.         CFrameWndEx::OnSysCommand(nID, lParam);
  414. }

  415. LRESULT CMyFrameWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  416. {
  417.         if (message == 0x125)
  418.         {
  419.                 OnNcPaint();
  420.         }

  421.         if (message == 0x00AE ||//WM_NCUAHDRAWCAPTION
  422.                 message == 0x00AF)//WM_NCUAHDRAWFRAME)
  423.         {
  424.                 OnNcPaint();
  425.                 return 0;
  426.         }

  427.         return CFrameWndEx::WindowProc(message, wParam, lParam);
  428. }

  429. BOOL CMyFrameWnd::OnNcCreate(LPCREATESTRUCT lpCreateStruct)
  430. {
  431.         if (!CFrameWndEx::OnNcCreate(lpCreateStruct))
  432.                 return FALSE;

  433.         CRect rcWnd;
  434.         GetWindowRect(&rcWnd);
  435.         rcWnd.OffsetRect(-rcWnd.left, -rcWnd.top);

  436.         CRect rcControl;
  437.         rcControl.left = rcWnd.right - 35;
  438.         rcControl.top = 5;
  439.         rcControl.right = rcControl.left + 20;
  440.         rcControl.bottom = rcControl.top + 20;
  441.         AddTitleButton(rcControl, m_WindowBtnImage[2], 3, ID_CLOSE_BTN);

  442.         AddTitleButton(rcControl, m_WindowBtnImage[1], 3, ID_MAX_BTN);

  443.         AddTitleButton(rcControl, m_WindowBtnImage[0], 3, ID_MIN_BTN);


  444.         return TRUE;
  445. }

  446. //往对话框添加一个按钮
  447. CMyButton* CMyFrameWnd::AddTitleButton(LPRECT lpRect, Image *pImage, UINT nCount /*= 3*/, UINT nID/* = 0*/, LPCTSTR lpszCaption/*=NULL*/)
  448. {
  449.         CMyButton dcControl;
  450.         dcControl.CreateButton(lpszCaption, lpRect, this, pImage, nCount, nID);
  451.         m_TitleBtn[nID] = dcControl;

  452.         return &m_TitleBtn[nID];
  453. }

  454. int CMyFrameWnd::TButtonHitTest(CPoint point, CRect& rtButton)
  455. {
  456.         std::map<int, CMyButton>::iterator iter;
  457.         for (iter = m_TitleBtn.begin(); iter != m_TitleBtn.end(); iter++)
  458.         {
  459.                 CMyButton& dcControl = iter->second;
  460.                 if (dcControl.PtInButton(point))
  461.                 {
  462.                         dcControl.GetRect(&rtButton);
  463.                         return dcControl.GetControlId();
  464.                 }
  465.         }
  466.         return -1;
  467. }
复制代码


其中CMyButton的代码实现如下:

  1. #include "StdAfx.h"
  2. #include "DUIButton.h"

  3. // 绘制按钮
  4. void CMyButton::DrawButton(Gdiplus::Graphics&  graphics)
  5. {
  6.         // 获取按钮图片信息
  7.         UINT iCount = m_nImageCount;
  8.         int        iButtonIndex = 0;
  9.         if (m_bDisabled && iCount >= 4) iButtonIndex = 3;
  10.         else if (m_bPressed && iCount >= 3)iButtonIndex = 2;
  11.         else if (m_bHovering && iCount >= 2)iButtonIndex = 1;
  12.         else iButtonIndex = 0;

  13.         // 在指定位置绘制按钮
  14.         int iWidth = m_pImage->GetWidth() / iCount;
  15.         int iHeight = m_pImage->GetHeight();
  16.         RectF grect;
  17.         grect.X = (Gdiplus::REAL)m_rcRect.left;
  18.         grect.Y = (Gdiplus::REAL)m_rcRect.top;
  19.         grect.Width = (Gdiplus::REAL)m_rcRect.Width();
  20.         grect.Height = (Gdiplus::REAL)m_rcRect.Height();

  21.         graphics.DrawImage(m_pImage, grect, (Gdiplus::REAL)iWidth*iButtonIndex, 0, (Gdiplus::REAL)iWidth, (Gdiplus::REAL)iHeight, UnitPixel);

  22.         StringFormat stringFormat;

  23.         if (m_pIcon)
  24.         {
  25.                 PointF ptIcon(m_ptIcon.x, m_ptIcon.y);
  26.                 graphics.DrawImage(m_pIcon, ptIcon);

  27.                 grect.X = (Gdiplus::REAL)m_ptIcon.x + m_pIcon->GetWidth() + 2;
  28.                 grect.Width = (Gdiplus::REAL)m_rcRect.Width() - m_pIcon->GetWidth() - 2;

  29.                 stringFormat.SetFormatFlags(StringFormatFlagsDirectionVertical);
  30.                 stringFormat.SetAlignment(StringAlignmentCenter);
  31.                 stringFormat.SetLineAlignment(StringAlignmentNear);
  32.         }
  33.         else
  34.         {
  35.                 //stringFormat.SetFormatFlags( StringFormatFlagsDirectionVertical);
  36.                 stringFormat.SetAlignment(StringAlignmentCenter);
  37.                 stringFormat.SetLineAlignment(StringAlignmentCenter);
  38.         }

  39.         if (!m_strCaption.IsEmpty())
  40.         {
  41.                 //绘制文字
  42.                 FontFamily fontFamily(L"宋体");
  43.                 Gdiplus::Font font(&fontFamily, 10, FontStyleRegular, UnitPoint);


  44.                 CStringW strTitle(m_strCaption);
  45.                 SolidBrush brush((ARGB)Color::White);
  46.                 if (m_bDisabled)
  47.                 {
  48.                         brush.SetColor((ARGB)Color::Gray);
  49.                 }

  50.                 graphics.DrawString(strTitle, strTitle.GetLength(), &font, grect, &stringFormat, &brush);
  51.         }

  52. }

  53. // 创建按钮
  54. void CMyButton::CreateButton(LPCTSTR lpszCaption, LPRECT lpRect, CWnd* pParent, Image* pImage, UINT nCount/* = 4*/, UINT nID/* = 0*/)
  55. {
  56.         if (lpszCaption)
  57.         {
  58.                 m_strCaption = lpszCaption;
  59.         }
  60.         m_pParent = pParent;
  61.         m_rcRect = lpRect;
  62.         m_pImage = pImage;
  63.         m_nImageCount = nCount;
  64.         m_nID = nID;
  65. }

  66. //设置按钮图标信息
  67. void  CMyButton::SetIconInfo(Image *pIcon, CPoint ptPos/* = CPoint(5,3)*/, BOOL bRedraw/*=FALSE*/)
  68. {
  69.         m_pIcon = pIcon;
  70.         m_ptIcon = CPoint(m_rcRect.left + ptPos.x, m_rcRect.top + ptPos.y);
  71.         if (bRedraw)
  72.         {
  73.                 m_pParent->InvalidateRect(&m_rcRect);
  74.         }
  75. }

  76. void CMyButton::SetRect(LPCRECT lpRect)
  77. {
  78.         CPoint ptIconOrg(m_ptIcon.x - m_rcRect.left, m_ptIcon.y - m_rcRect.top);
  79.         m_rcRect = lpRect;
  80.         m_ptIcon = CPoint(m_rcRect.left + ptIconOrg.x, m_rcRect.top + ptIconOrg.y);
  81. }
复制代码


目前我的效果如下:

各位大佬,请问要怎么改才能做成想要SDI重绘2的效果呢?劳烦大佬们帮忙看看,给点意见或建议,小弟在此多谢了。



SDI重绘1

SDI重绘1

SDI重绘2

SDI重绘2




上一篇:MFC应用程序被小红伞拦截,视为病毒
下一篇:VS的函数入口参数提示功能
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-12-3 19:00

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表