Could anyone tell me where i'm going wrong,(using visual studio 6)
please
regards
here is the code:
#include <afxwin.h>
#define IDB_BITMAP 100
// Declare the application class
class CBitmapApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
// Create an instance of the application class
CBitmapApp BitmapApp;
// Declare the main window class
class CBitmapWindow : public CFrameWnd
{
CBitmap *bitmap;
public:
CBitmapWindow();
afx_msg void HandleBitmap();
afx_msg void OnSize(UINT, int, int); // USED FOR RESIZING
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};
// The message map
BEGIN_MESSAGE_MAP(CBitmapWindow, CFrameWnd)
ON_BN_CLICKED(IDB_BITMAP, HandleBitmap)
ON_WM_SIZE() //RESIZE
END_MESSAGE_MAP()
// The InitInstance function is called once
// when the application first executes
BOOL CBitmapApp::InitInstance()
{
m_pMainWnd = new CBitmapWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
// The constructor for the window class
/*CBitmapWindow::CBitmapWindow()
{
CRect r;
// Create the window itself
Create(NULL,
"CBitmap Tests",
WS_OVERLAPPEDWINDOW,
CRect(0,0,200,200));
// Get the size of the client rectangle
GetClientRect(&r);
r.InflateRect(-20,-20);
// Create a button
bitmap = new CBitmap();
bitmap->Create("Push me",
WS_CHILD|WS_VISIBLE|BS_PUSHBITMAP,
r,
this,
IDB_BITMAP);
}*/
BOOL GetBitmapAndPalette(UINT nIDResource, CBitmap &bitmap, CPalette &pal)
{
LPCTSTR lpszResourceName = (LPCTSTR)nIDResource;
HBITMAP hBmp = (HBITMAP)::LoadImage( AfxGetInstanceHandle(),
lpszResourceName, IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION );
if( hBmp == NULL )
return FALSE;
bitmap.Attach( hBmp );
// Create a logical palette for the bitmap
DIBSECTION ds;
BITMAPINFOHEADER &bmInfo = ds.dsBmih;
bitmap.GetObject( sizeof(ds), &ds );
int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount;
// Create a halftone palette if colors > 256.
CClientDC dc(NULL); // Desktop DC
if( nColors > 256 )
pal.CreateHalftonePalette( &dc );
else
{
// Create the palette
RGBQUAD *pRGB = new RGBQUAD[nColors];
CDC memDC;
memDC.CreateCompatibleDC(&dc);
memDC.SelectObject( &bitmap );
::GetDIBColorTable( memDC, 0, nColors, pRGB );
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries = nColors;
for( int i=0; i < nColors; i++)
{
pLP->palPalEntry.peRed = pRGB.rgbRed;
pLP->palPalEntry.peGreen = pRGB.rgbGreen;
pLP->palPalEntry.peBlue = pRGB.rgbBlue;
pLP->palPalEntry.peFlags = 0;
}
pal.CreatePalette( pLP );
delete[] pLP;
delete[] pRGB;
}
return TRUE;
}
void CBitmapWindow::OnPaint()
{
CPaintDC dc(this);
// Create a memory DC compatible with the paint DC
CDC memDC;
memDC.CreateCompatibleDC( &dc );
CBitmap bitmap;
CPalette palette;
GetBitmapAndPalette( IDB_BITMAP, bitmap, palette );
memDC.SelectObject( &bitmap );
// Select and realize the palette
if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE && palette.m_hObject != NULL )
{
dc.SelectPalette( &palette, FALSE );
dc.RealizePalette();
}
dc.BitBlt(0, 0, 180, 180, &memDC, 0, 0,SRCCOPY);
}
please
regards
here is the code:
#include <afxwin.h>
#define IDB_BITMAP 100
// Declare the application class
class CBitmapApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
// Create an instance of the application class
CBitmapApp BitmapApp;
// Declare the main window class
class CBitmapWindow : public CFrameWnd
{
CBitmap *bitmap;
public:
CBitmapWindow();
afx_msg void HandleBitmap();
afx_msg void OnSize(UINT, int, int); // USED FOR RESIZING
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};
// The message map
BEGIN_MESSAGE_MAP(CBitmapWindow, CFrameWnd)
ON_BN_CLICKED(IDB_BITMAP, HandleBitmap)
ON_WM_SIZE() //RESIZE
END_MESSAGE_MAP()
// The InitInstance function is called once
// when the application first executes
BOOL CBitmapApp::InitInstance()
{
m_pMainWnd = new CBitmapWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
// The constructor for the window class
/*CBitmapWindow::CBitmapWindow()
{
CRect r;
// Create the window itself
Create(NULL,
"CBitmap Tests",
WS_OVERLAPPEDWINDOW,
CRect(0,0,200,200));
// Get the size of the client rectangle
GetClientRect(&r);
r.InflateRect(-20,-20);
// Create a button
bitmap = new CBitmap();
bitmap->Create("Push me",
WS_CHILD|WS_VISIBLE|BS_PUSHBITMAP,
r,
this,
IDB_BITMAP);
}*/
BOOL GetBitmapAndPalette(UINT nIDResource, CBitmap &bitmap, CPalette &pal)
{
LPCTSTR lpszResourceName = (LPCTSTR)nIDResource;
HBITMAP hBmp = (HBITMAP)::LoadImage( AfxGetInstanceHandle(),
lpszResourceName, IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION );
if( hBmp == NULL )
return FALSE;
bitmap.Attach( hBmp );
// Create a logical palette for the bitmap
DIBSECTION ds;
BITMAPINFOHEADER &bmInfo = ds.dsBmih;
bitmap.GetObject( sizeof(ds), &ds );
int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount;
// Create a halftone palette if colors > 256.
CClientDC dc(NULL); // Desktop DC
if( nColors > 256 )
pal.CreateHalftonePalette( &dc );
else
{
// Create the palette
RGBQUAD *pRGB = new RGBQUAD[nColors];
CDC memDC;
memDC.CreateCompatibleDC(&dc);
memDC.SelectObject( &bitmap );
::GetDIBColorTable( memDC, 0, nColors, pRGB );
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries = nColors;
for( int i=0; i < nColors; i++)
{
pLP->palPalEntry.peRed = pRGB.rgbRed;
pLP->palPalEntry.peGreen = pRGB.rgbGreen;
pLP->palPalEntry.peBlue = pRGB.rgbBlue;
pLP->palPalEntry.peFlags = 0;
}
pal.CreatePalette( pLP );
delete[] pLP;
delete[] pRGB;
}
return TRUE;
}
void CBitmapWindow::OnPaint()
{
CPaintDC dc(this);
// Create a memory DC compatible with the paint DC
CDC memDC;
memDC.CreateCompatibleDC( &dc );
CBitmap bitmap;
CPalette palette;
GetBitmapAndPalette( IDB_BITMAP, bitmap, palette );
memDC.SelectObject( &bitmap );
// Select and realize the palette
if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE && palette.m_hObject != NULL )
{
dc.SelectPalette( &palette, FALSE );
dc.RealizePalette();
}
dc.BitBlt(0, 0, 180, 180, &memDC, 0, 0,SRCCOPY);
}