C++ 在MFC中绘制jpg

C++ 在MFC中绘制jpg,c++,mfc,C++,Mfc,我一直试图在MFC中显示jpg图像,但我无法绘制它。我是一个完全的MFC新手,到目前为止我所做的一切都是根据我在网上找到的东西改编的。目前我有: 图h: #pragma once #include <afxwin.h> class Picture { public: Picture(); bool load(LPCTSTR filePath); bool draw( CDC* deviceContext , CRect clientRe

我一直试图在MFC中显示jpg图像,但我无法绘制它。我是一个完全的MFC新手,到目前为止我所做的一切都是根据我在网上找到的东西改编的。目前我有:

图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;
};
PictureView.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;
};
#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;
};
构造函数调用:

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;
}
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;
}
问题是我无法调用DrawItem函数。我做错了什么?
任何帮助都将不胜感激。

我从未使用过MFC,但快速阅读说明它必须使用
SS_OWNERDRAW
样式创建,才能调用
DrawItem
。你还没有为你的
PictureView
显示
Create
行,所以可能是这样的?

我不确定调用m_picture->draw中的GetDC是否一定会引用CREATESTRUCT中给出的相同DC。我所做的是:

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

编辑:因为我不能评论,希望编辑就足够了。我想你的同事弄错了,因为我最近刚刚用SS_OWNERDRAW实现了一个CStatic控件。我猜在Create调用中添加可见的WS|u CHILD | WS|u是关键。

在我同事的帮助下,我找到了答案。他告诉我不可能有一个自己绘制的CStatic。因此,当我现在从CButton继承PictureView并使其成为BS_OWNERDRAW时,我的图像被渲染

就我个人而言,我认为这是一个丑陋的解决方案,但我已经厌倦了这个问题,现在我真的不太在乎。以下是我为使其正常工作所做的更改:

构造函数调用:

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;
}
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:

#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;
};
#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;
};
#pragma一次
#包括
#包括
班级图片;
类PictureView:公共CButton
{
公众:
PictureView(标准::字符串路径);
受保护的:
虚空DrawItem(LPDRAWITEMSTRUCT LPDRAWITEMSTRUCT);
私人:
图片*m_图片;
};

感谢大家的帮助。

以下是在MFC对话框中绘制图像的简单方法:

链接到
gdiplus.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);

希望它能起作用

您是否需要一个按钮或菜单项来保存图像,并将其命名为PictureView::DrawItem?其思想是:另一个进程有一个摄像头并对事物拍照。它将这些图片存储为jpg的。当某个条件发生时,它会通知我的进程,并提供与该条件对应的映像的路径。我需要向用户显示一个包含图像的对话框,用户现在必须选择如何继续。事实并非如此,我现在已经更改了代码。然而,这并没有改变这个问题。我添加的代码如下所示:PictureView*image=newpictureview(path);图像->创建(“”,SS_OWNERDRAW,CRect(水平位置,垂直位置,宽度+水平位置,高度+垂直位置),this,childId);有什么方法可以让我把它以更可读的格式发布在这里吗?@Tom:你可以通过编辑你的原始问题来添加这些信息。有可能,但我无法调用DrawItem函数,所以我还不能测试它。我的错-显然我可以对我自己的答案发表评论。“生活和学习”。你是对的,当WS_CHILD和WS_VISIBLE被添加时,它作为一个CStatic工作。