001
16.10.2005, 21:49 Uhr
FloSoft
Medialer Over-Flow (Administrator)
|
hi, bei www.thecodeproject.com/bitmap/pictureshow.asp gibts eine "CPicture"-Klasse, die auch aus den Ressourcen beliebige formate (ok bmp, jpeg, gif) laden konnte. Dazu hab ich mir eine CStatic-Variante geschrieben:
C++: |
#include "stdafx.h" #include "ImageStatic.h"
IMPLEMENT_DYNAMIC(CImageStatic, CStatic)
BEGIN_MESSAGE_MAP(CImageStatic, CStatic) ON_WM_CTLCOLOR_REFLECT() ON_WM_DRAWITEM() END_MESSAGE_MAP()
CImageStatic::CImageStatic() : m_Picture() { m_bFit = 0; }
CImageStatic::~CImageStatic() { }
HBRUSH CImageStatic::CtlColor(CDC* pDC, UINT nCtlColor) { ModifyStyle(0,SS_OWNERDRAW); return NULL; }
void CImageStatic::SetBitmap(CString strBitmap) { m_Picture.FreePictureData(); m_Picture.Load(strBitmap); }
void CImageStatic::SetBitmap(UINT ressource, LPCSTR restype) { m_Picture.FreePictureData(); m_Picture.Load(ressource, restype); }
void CImageStatic::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC dc; dc.Attach(lpDrawItemStruct->hDC); CDC *pDC = &dc;
CRect window; GetClientRect(&window);
pDC->FillSolidRect(&window, RGB(255, 255, 255) );
int Width = 0; int Height = 0; int X = 1; int Y = 1;
if(m_bFit) { if(m_Picture.m_Width > m_Picture.m_Height) { Width = window.Width(); Height = (int)(double)((double)window.Width() / (double)m_Picture.m_Width * (double)m_Picture.m_Height); Y = (window.Height() - Height) / 2; } else { Width = (int)(double)((double)window.Height() / (double)m_Picture.m_Height * (double)m_Picture.m_Width); Height = window.Height(); X = (window.Width() - Width) / 2; } } else { Width = m_Picture.m_Width; Height = m_Picture.m_Height; X = (window.Width() - Width) / 2; Y = (window.Height() - Height) / 2; }
CRect rect( X, Y, X+Width, Y+Height);
m_Picture.Show(pDC, &rect); }
void CImageStatic::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) { DrawItem(lpDrawItemStruct); }
void CImageStatic::SetFit(BOOL bFit) { m_bFit = bFit; }
|
C++: |
#pragma once
#include "Picture.h"
class CImageStatic : public CStatic { DECLARE_DYNAMIC(CImageStatic)
public: CImageStatic(); virtual ~CImageStatic(); void SetBitmap(CString strBitmap); void SetBitmap(UINT ressource, LPCSTR restype); void SetFit(BOOL bFit);
protected: virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/); afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor); afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct); DECLARE_MESSAGE_MAP()
private: CPicture m_Picture; BOOL m_bFit; };
|
wenn man m_bFit setzt, passt er das Bild an die Größe des Rahmens an. -- class God : public ChuckNorris { }; |