【问题标题】:Zoom Bitmap Image from center in mfc在mfc中从中心缩放位图图像
【发布时间】:2015-06-02 05:33:34
【问题描述】:

我是 MFC 的新手。如何从中心缩放位图图像。在我的代码中,图像从左上角放大。但是图像应该从滚动条的中心放大..

void CRightUp::ImageDraw(int sHeight,int sWidth){
    CDC *screenDC = GetDC();
    CDC mDC;
    mDC.CreateCompatibleDC(screenDC);
    CBitmap bitmap;
    bitmap.CreateCompatibleBitmap(screenDC, sWidth, sHeight);
    CBitmap *pob = mDC.SelectObject(&bitmap);
    mDC.SetStretchBltMode(HALFTONE);
    m_img.StretchBlt(mDC.m_hDC, 0, 0, sWidth, sHeight, 0, 0,   m_img.GetWidth(), m_img.GetHeight(), SRCCOPY);
    mDC.SelectObject(pob);
    m_logoImage.SetBitmap((HBITMAP)bitmap.Detach());
    ReleaseDC(screenDC);}

【问题讨论】:

  • 什么是 m_logoImage?您每次都在创建一个新的 HBITMAP,而不仅仅是绘制它。为什么不直接处理 in OnPaint 方法呢?
  • @Garr Godfrey m_logoImage CStatic 对象。我已经使用图片控件来显示图像了。
  • 好吧,您可以通过将 CStatic 对象设置为窗口的大小并设置其对齐属性来做到这一点......或者我会发布一个仅绘制它的答案。 CRightUp 是 CWnd 的衍生产品吗?
  • CFormView 继承自 CWnd。根据您拥有的子控件,我发布的答案应该有效
  • 也就是说,如果您在 m_logoImage 上设置 SS_CENTERIMAGE 样式并将其设置为客户区的完整大小,您的技术应该可以工作。

标签: visual-c++ mfc


【解决方案1】:

为什么要使用 m_logoImage:

void CRightUp::ImageDraw(int sHeight,int sWidth){
    RECT r;
    GetClientRect(&r);
    CDC *screenDC = GetDC();
    m_img.StretchBlt(screenDC->m_hDC, ((r.right-r.left)-sWidth)/2, 
         ((r.bottom-r.top)-sHeight)/2, 
         sWidth, sHeight, 0, 0,  
         m_img.GetWidth(), m_img.GetHeight(), SRCCOPY);
    ReleaseDC(screenDC);
}

这只会暂时绘制它。如果您快速调用 ImageDraw(例如,每 1/10 秒或更长时间),在完全缩放之前应该没问题。此时,您可以在 CStatic 或 OnPaint 或 OnEraseBackground 中处理它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多