2005年12月26日 (月)

CPicture を使う(CMyView)

class CMyView : public CScrollView
{
  ...
void CMyView::OnDraw(CDC* pDC)
{
  CMyDoc* pDoc = GetDocument();   ASSERT_VALID(pDoc);   if (pDC->IsPrinting())   {     int width =
      pDC->GetDeviceCaps(PHYSICALWIDTH);     int height =
      pDC->GetDeviceCaps(PHYSICALHEIGHT);     width -= pDC->GetDeviceCaps(PHYSICALOFFSETX) * 2;     height -= pDC->GetDeviceCaps(PHYSICALOFFSETY) * 2;     pDoc->m_picture.Stretch(
      pDC, CRect(0,0,width,height));   }   else   {     CRect rect;     GetClientRect(rect);     pDoc->m_picture.Render(pDC, rect);   } }
void CMyView::OnInitialUpdate()
{
  CScrollView::OnInitialUpdate();

  CSize sizeTotal;
  sizeTotal.cx = sizeTotal.cy = 100;

  CClientDC dc(this);
  CMyDoc* pDoc = GetDocument();
  if (pDoc->m_picture.HasPicture())
    pDoc->m_picture.GetDPSize(&dc, &sizeTotal);

  SetScrollSizes(MM_TEXT, sizeTotal);
}

|

CPicture を使う(CMyDoc)

class CMyDoc : public CDocument
{
  ...
public:
  CPicture m_picture;
  ...
  virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);
  ...
};
BOOL CMyDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
  if (!m_picture.LoadFile(lpszPathName))
    return FALSE;

  return TRUE;
}

|

2005年12月19日 (月)

CPicture に追加

void CPicture::Stretch(CDC* pDC, CRect rect)
{
  ASSERT(pDC != NULL);
  if (m_pPicture != NULL)
  {     CSize size(m_lWidth, m_lHeight);     pDC->HIMETRICtoDP(&size);     double dx = (double)rect.Width() / size.cx;     double dy = (double)rect.Height() / size.cy;     double d = (dx < dy ? dx : dy);     size.cx = (int)(size.cx * d);     size.cy = (int)(size.cy * d);     m_pPicture->Render(       pDC->GetSafeHdc(),       0, 0, size.cx, size.cy,       0, m_lHeight, m_lWidth, -m_lHeight, rect);   } }

|

2005年11月24日 (木)

CPicture::LoadFile の書き換え

BOOL CPicture::LoadFile(LPCTSTR szFileName)
{
    Release();     USES_CONVERSION; // <atlbase.h>
    HRESULT hr = ::OleLoadPicturePath(
        T2OLE(szFileName), 0, 0, 0,
        IID_IPicture, (void**)&m_pPicture);
    if (FAILED(hr))
    {
        m_pPicture = NULL;
        return FALSE;
    }
    m_pPicture->get_Width(&m_lWidth);
    m_pPicture->get_Height(&m_lHeight);     return TRUE;
}

|

2005年11月18日 (金)

OleLoadPicturePath

CPicture クラスを作ってみました。BMP、JPEG、WMF、ICO、GIF 形式を読み込んで表示できます。

class CPicture
{
protected:
  IPicture* m_pPicture;
  long m_lWidth, m_lHeight;

public:
  CPicture() : m_pPicture(NULL) {};
  virtual ~CPicture() { Release(); };
  BOOL LoadFile(LPCTSTR szFileName);
  void Render(CDC* pDC, CRect rect);
  void Release();
  void GetDPSize(CDC* pDC, LPSIZE pSize) const;
  BOOL HasPicture() { return m_pPicture != NULL; };
}; BOOL CPicture::LoadFile(LPCTSTR szFileName)
{
  Release();   OLECHAR wszPath[MAX_PATH+1];
  ::MultiByteToWideChar(
    CP_ACP, 0, szFileName, -1,
    wszPath, MAX_PATH);   HRESULT hr = ::OleLoadPicturePath(
    wszPath, 0, 0, 0,
    IID_IPicture, (void**)&m_pPicture);   if (FAILED(hr))
  {
    m_pPicture = NULL;
    return FALSE;
  }   m_pPicture->get_Width(&m_lWidth);
  m_pPicture->get_Height(&m_lHeight);   return TRUE;
}
void CPicture::Render(CDC* pDC, CRect rect)
{
  ASSERT(pDC != NULL);   if (m_pPicture != NULL)
  {
    CSize size(m_lWidth, m_lHeight);
    pDC->HIMETRICtoDP(&size);     m_pPicture->Render(
      pDC->GetSafeHdc(),
      0, 0, size.cx, size.cy,
      0, m_lHeight, m_lWidth, -m_lHeight, rect);
  }
} void CPicture::Release()
{
  if (m_pPicture == NULL) return;
  m_pPicture->Release();
  m_pPicture = NULL;
} void CPicture::GetDPSize(CDC* pDC, LPSIZE pSize) const
{
  ASSERT(m_pPicture != NULL);   ASSERT(pSize != NULL);   pSize->cx = m_lWidth;
  pSize->cy = m_lHeight;
  pDC->HIMETRICtoDP(pSize);
}

|