简体   繁体   English

在 MFC 中绘制 jpg

[英]Drawing a jpg in MFC

I've been trying to show a jpg image in MFC, but I can't get it drawn.我一直在尝试在 MFC 中显示 jpg 图像,但无法绘制它。 I'm a complete MFC newbie an everything I got up until now is mostly adapted from things I found on the net.我是一个完整的 MFC 新手,到目前为止我所做的一切都是根据我在网上找到的东西改编的。 Currently I have this:目前我有这个:

Picture.h:图片.h:

#pragma once
#include <afxwin.h>

class Picture
{
public:
   Picture();

   bool load(LPCTSTR filePath);

   bool draw( CDC* deviceContext
            , CRect clientRect
            , LPCRECT prcMFBounds);

   CSize getSize(CDC* pDC);

private:
   LPPICTURE m_picture;
};

Picture.cpp:图片.cpp:

#include "Picture.h"

Picture::Picture()
   : m_picture(0)
{
}

bool Picture::load(LPCTSTR szFile)
{
   HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
   if (hFile == INVALID_HANDLE_VALUE)
      return false;

   DWORD dwFileSize = GetFileSize(hFile, NULL);
   if (dwFileSize == (DWORD)-1)
   {
      CloseHandle(hFile);
      return false;
   }

   LPVOID pvData = NULL;

   // alloc memory based on file size
   HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
   if (hGlobal == NULL)
   {
      CloseHandle(hFile);
      return false;
   }

   pvData = GlobalLock(hGlobal);

   if (pvData == NULL)
   {
      GlobalUnlock(hGlobal);
      CloseHandle(hFile);
      return false;
   }

   DWORD dwBytesRead = 0;

   // read file and store in global memory
   bool bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL) != 0;

   GlobalUnlock(hGlobal);
   CloseHandle(hFile);

   if (!bRead)
      return false;

   LPSTREAM pstm = NULL;

   // create IStream* from global memory
   HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
   if (!(SUCCEEDED(hr)))
   {
      if (pstm != NULL)
         pstm->Release();
      return false;
   }

   else if (pstm == NULL)
      return false;

   // Create IPicture from image file
   if (m_picture)
      m_picture->Release();

   hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&(m_picture));
   if (!(SUCCEEDED(hr)))
   {
      pstm->Release();
      return false;
   }

   else if (m_picture == NULL)
   {
      pstm->Release();
      return false;
   }
   pstm->Release();

   return true;
}

bool Picture::draw(CDC* deviceContext, CRect clientRect, LPCRECT prcMFBounds)
{
   if (clientRect.IsRectNull())
   {
      CSize imageSize = getSize(deviceContext);
      clientRect.right = imageSize.cx;
      clientRect.bottom = imageSize.cy;
   }

   long pictureWidth = 0;
   long pictureHeigth = 0;

   m_picture->get_Width(&pictureWidth);
   m_picture->get_Height(&pictureHeigth);

   m_picture->Render( *deviceContext
                    , clientRect.left
                    , clientRect.top
                    , clientRect.Width()
                    , clientRect.Height()
                    , 0
                    , pictureHeigth
                    , pictureWidth
                    , -pictureHeigth
                    , prcMFBounds);
   return true;
}

CSize Picture::getSize(CDC* deviceContext)
{
   if (!m_picture)
      return CSize(0,0);

   LONG width, height; // HIMETRIC units
   m_picture->get_Width(&width);
   m_picture->get_Height(&height);
   CSize size(width, height);
   if (deviceContext==NULL)
   {
      CWindowDC dc(NULL);
      dc.HIMETRICtoDP(&size); // convert to pixels
   }
   else
   {
      deviceContext->HIMETRICtoDP(&size);
   }
   return size;
}

PictureView.h:图片视图.h:

#pragma once

#include <afxwin.h>
#include <string>

class Picture;

class PictureView : public CStatic
{
public:
   PictureView(std::string path);

protected:
   virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

private:
   Picture* m_picture;
};

PictureView.cpp:图片视图.cpp:

#include "PictureView.h"
#include "Picture.h"

PictureView::PictureView(std::string path)
{
   m_picture = new Picture();
   m_picture->load(path.c_str());
}

void PictureView::DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/)
{
   CRect rect;
   GetClientRect(&rect);
   m_picture->draw(GetDC(), rect, rect);
}

Constructor call:构造函数调用:

CWnd* GenericChildWidget::addImage(std::string path, int horizontalPos, int width, int verticalPos, int height, int childId)
{
   PictureView* image = new PictureView(path);
   image->Create("", SS_OWNERDRAW, CRect(horizontalPos, verticalPos, width + horizontalPos, height + verticalPos), this, childId);
   return image;
}

The problem is that I can't get the DrawItem function to be called.问题是我无法调用 DrawItem 函数。 What am I doing wrong?我究竟做错了什么? Any help will be appreciated.任何帮助将不胜感激。

Here's an easy way to draw an image in an MFC dialog box:这是在 MFC 对话框中绘制图像的简单方法:

link with gdiplus.dllgdiplus.dll链接

#include "atlimage.h>
#include "gdiplus.h>
using namespace Gdiplus
.
.
.

CImage ci;

ci.Load((CString)"D:\\Pictures\\mycat.jpg");

CDC *dc = AfxGetMainWnd()->GetDC();

HDC hdc = *dc;

ci.Draw(hdc,10,10);

Hope it works!希望它有效!

I'm not sure that the GetDC in the call to m_picture->draw will necessarily refer to the same DC that's given in the CREATESTRUCT.我不确定调用 m_picture->draw 中的 GetDC 是否一定会引用 CREATESTRUCT 中给出的相同 DC。 What I've done is:我所做的是:

      CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

EDIT: Since I can't comment, hopefully an edit will suffice.编辑:由于我无法发表评论,希望编辑就足够了。 I think your colleague is mistaken, as I just recently implemented a CStatic control using SS_OWNERDRAW.我认为你的同事错了,因为我最近使用 SS_OWNERDRAW 实现了一个 CStatic 控件。 I'm guessing that the addition of WS_CHILD |我猜是添加了 WS_CHILD | WS_VISIBLE on the Create call is key. Create 调用上的 WS_VISIBLE 是关键。

with the help of a colleague of mine I've found the answer.在我同事的帮助下,我找到了答案。 He told me it's not possible to have an ownerdrawn CStatic.他告诉我不可能拥有自有的 CStatic。 So when I now inherit PictureView from CButton and make it BS_OWNERDRAW my image is rendered.因此,当我现在从 CButton 继承 PictureView 并将其设为 BS_OWNERDRAW 时,我的图像已呈现。

Personally I think this is an ugly solution but I'm so tired of this problem now that I don't really care that much.就我个人而言,我认为这是一个丑陋的解决方案,但我现在对这个问题感到厌倦,以至于我不太在意。 These are the changes I've made to make it work:这些是我为使其工作所做的更改:

Constructor call:构造函数调用:

CWnd* GenericChildWidget::addImage(std::string path, int horizontalPos, int width, int verticalPos, int height, int childId)
{
   PictureView* image = new PictureView(path);
   image->Create("", BS_OWNERDRAW | WS_CHILD | WS_VISIBLE, CRect(horizontalPos, verticalPos, width + horizontalPos, height + verticalPos), this, childId);
   return image;
}

PictureView.h:图片视图.h:

#pragma once

#include <afxwin.h>
#include <string>

class Picture;

class PictureView : public CButton
{
public:
   PictureView(std::string path);

protected:
   virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

private:
   Picture* m_picture;
};

Thank's everybody for all the help.谢谢大家的帮助。

I've never used MFC but a quick perusal of the CStatic::DrawItem documentation says that it must be created with the SS_OWNERDRAW style for DrawItem to get called.我从未使用过 MFC,但快速阅读CStatic::DrawItem 文档说它必须使用SS_OWNERDRAW样式创建,才能调用DrawItem You haven't shown the Create line for your PictureView so perhaps it's that?您还没有为您的PictureView显示Create行,所以也许是这样?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM