๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.ms-windows.programmer.graphics
Article #32

Icons and bitmaps in memory DC's

#32
From: Thomas Hiilbig
Date: Tue, 05 Jan 1999 00:00
174 lines
6063 bytes
Hi there

I'm trying to put an icon into a DC before I actually paint the DC into a
window. 
My problem is: If I have a DIB-bitmap into the temporary memoryDC, the
transparent parts of the icon is changing the color of the bitmap.... Can
anyone help me out?

I do it like this:

Load a *.ico file into a CImageList
Load a *.bmp as DIB into a CBitmap
Create a memoryDC, compatible with the system display
Select the DIB bitmap into the memoryDC
ajust the MemoryDC's colorpalette to match the bitmap colors
draw the icon (in the CImageList) onto the bitmap (in the MemDC)
copying the bitmap into the windows paintDC (using BitBlt)

---

The bitmap I'm using is a bitmap containing  at least one color beyond the
20 standard windows colors and the icon is only using standard windows
colors...

Can anyone help me here?!?

Thomas Hiilbig Pedersen
Danfoss A/S
hiilbig@danfoss.dk

P.S.
Here is a code example to put the bitmap and the icon onto a dialog window
(m_Imagelist and m_Bitmap are member variables of the dialog class)... What
do I do wrong?

/********************
Init the dialog - load icon and bitmap
********************/
BOOL CBitmapLoaderDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

//  m_MemDC.CreateCompatibleDC(NULL);
  m_Imagelist.Create(16,16, ILC_COLOR | ILC_MASK, 2, 2);
    HANDLE icon;
  //if(!(icon = LoadImage(NULL, "c:\\akm\\aksymbol\\alarm.ico", IMAGE_ICON,
NULL, NULL, LR_LOADFROMFILE /*| LR_CREATEDIBSECTION*/)))
    if(!(icon = LoadImage(NULL, "c:\\akm\\aksymbol\\standby.ico",
IMAGE_ICON, NULL, NULL, LR_LOADFROMFILE)))
  {
    AfxMessageBox("Alarm.ico blev ikke indl๏ฟฝst fra disken");
    return 0;
  }
  else
  {
    if((m_Imagelist.Add((HICON)icon)) == -1)
      AfxMessageBox("Alarm.ico blev ikke lagt ind i CImageList");
  }

  BOOL bResult = m_Bitmap.Attach(::LoadImage(NULL,
"c:\\picture\\Superbmp.bmp", IMAGE_BITMAP, 0, 0,
	                 LR_LOADFROMFILE  | LR_CREATEDIBSECTION |
LR_DEFAULTSIZE));

  if(!bResult)
  {
    AfxMessageBox("Picture was not loaded");
    return 0;
  }
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code
below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

/********************
Paint the dialog
********************/
void CBitmapLoaderDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
    CPoint point(100,147);
		CPaintDC dc(this); // device context for painting
    CPalette pal;

    CDC MemDC;
    MemDC.CreateCompatibleDC(NULL);

    CBitmap* OldBitmap = MemDC.SelectObject(&m_Bitmap);
    AjustPalette(MemDC, pal);
    dc.SelectPalette(&pal, FALSE);
    dc.RealizePalette();
    MemDC.SelectPalette(&pal, FALSE);
    MemDC.RealizePalette();

    m_Imagelist.Draw(&MemDC, 0, (POINT) point, ILD_TRANSPARENT);
    
    dc.BitBlt(0,0,700,800, &MemDC, 0,0, SRCCOPY);

    MemDC.SelectObject(OldBitmap);

    CDialog::OnPaint();
	}
}


#define PALVERSION 0x300	// magic number for LOGPALETTE
/********************
Ajust the colors of a DC
********************/
void CBitmapLoaderDlg::AjustPalette(CDC &MemDC, CPalette &Pal)
{
	CWindowDC dcScreen(NULL);

  BOOL bRet = FALSE;
	RGBQUAD* colors = new RGBQUAD[255];

	UINT nColors = GetDIBColorTable(MemDC, 0, 255, colors);

  if (nColors > 0) {
		// Allocate memory for logical palette 
		int len = sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * nColors;
		LOGPALETTE* pLogPal = (LOGPALETTE*)new char[len];
		if (!pLogPal)
			return;

		// set version and number of palette entries
		pLogPal->palVersion = PALVERSION;
		pLogPal->palNumEntries = nColors;

		// copy color entries 
		for (UINT i = 0; i < nColors; i++) {
    //for (UINT i = 10; i < 245; i++) {
			pLogPal->palPalEntry[i].peRed   = colors[i].rgbRed;
			pLogPal->palPalEntry[i].peGreen = colors[i].rgbGreen;
			pLogPal->palPalEntry[i].peBlue  = colors[i].rgbBlue;
			pLogPal->palPalEntry[i].peFlags = 0;
		}

    // create the palette and destroy LOGPAL
		bRet = Pal.CreatePalette(pLogPal);
 		delete [] (char*)pLogPal;
	}
  else {
		CWindowDC dcScreen(NULL);
		bRet = Pal.CreateHalftonePalette(&MemDC);
	}
	delete colors;
}
Technical Details
Message-ID: <01be38bb$f82d22e0$36f7080a@W022501.Danfoss.dk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 110a07,224a306aaba6e3bf,start
X-Google-Attributes: gid110a07,public
X-Google-Thread: 104d65,224a306aaba6e3bf,start
X-Google-Attributes: gid104d65,public
X-Google-Thread: f4307,224a306aaba6e3bf,start
X-Google-Attributes: gidf4307,public
X-Google-Thread: 101278,224a306aaba6e3bf,start
X-Google-Attributes: gid101278,public
X-Google-Thread: 105df1,224a306aaba6e3bf,start
X-Google-Attributes: gid105df1,public
X-Google-Thread: f7a51,224a306aaba6e3bf,start
X-Google-Attributes: gidf7a51,public
X-Google-Thread: fbd5d,224a306aaba6e3bf,start
X-Google-Attributes: gidfbd5d,public
From: "Thomas Hiilbig" <hiilbig@danfoss.dk>
Subject: Icons and bitmaps in memory DC's
Date: 1999/01/05
Message-ID: <01be38bb$f82d22e0$36f7080a@W022501.Danfoss.dk>
X-Deja-AN: 429029261
Organization: Danfoss A/S
Newsgroups: comp.os.ms-windows.programmer.graphics,comp.os.ms-windows.programmer.misc,fj.os.ms-windows.programming,microsoft.public.jp.visualc,microsoft.public.vc,microsoft.public.vc.language,microsoft.public.vc.mfc