Solved

256 color in CListCtrl

Posted on 1998-08-31
3
431 Views
Last Modified: 2013-11-19
Hi,
I have CTreeCtrl within a dialog box and I want to use 256 color bitmaps that a stored in a CImageList. But everything I tried seemed
not to work. The Images are allways displayed  in gray.

Does anybody have a solution for this problem?

Thanx in advance,
 Tom

0
Comment
Question by:stzclaus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
motigust earned 100 total points
ID: 1321492
Hi Stzclaus,

The problem is that you have needs to adjust the palette of your conrol to handle the 256 colors Bitmaps.

You should to write a program that gets the colors from the bitmap and creates a palette.

Then select the palette, and realize it, it should look something like this:
   GetBitmapAndPalette(IDB_1,&MyBitmap,Palette);
   dc.SelectPalette( &palette, FALSE );
   dc.RealizePalette();

The function that gets the plette from the bitmap should be somthing like this:

BOOL CMyClass::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[i].peRed = pRGB[i].rgbRed;
                  pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
                  pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
                  pLP->palPalEntry[i].peFlags = 0;
            }

            pal.CreatePalette( pLP );

            delete[] pLP;
            delete[] pRGB;
      }

      return TRUE;
}



Have fun,

      Moti  :-)

0
 

Author Comment

by:stzclaus
ID: 1321493
Hi Moti,

Thank you for your answer. I've tried the code you gave me, but the bitmaps still appear in grayscale. I don't know exactly what else is wrong in my code.
Maybe you could help me with the following questions.
 How do I have to create the CImageList?
   I use: pMyImageList->Create(IDB_BLABLA,XX,Y,0)
   Maybe I should use pMyImageList->Create(x,x ILC_COLOR8,x,x) ??
   If I do so, do I have to Add every Bitmap I want to use? And how do I get the Bitmap? With the    same function you gave to me?

To what DC do I have to select the pallet?
 I tried  CClientDC myDC (this) and CClientDC myDC (NULL)
Is this wrong?

It would be great, if you could help me!

Many, many thanx in advance

Tom

0
 

Author Comment

by:stzclaus
ID: 1321494
Hi Moti!

It works!

I think the only problem was, that I used the wrong create-function for my CImageList.
I tried Create(x,x ILC_COLOR8,x,x), Add (IDB_IMAGEWHATSOEVER,xx ) and everything was fine. I didn't even had to create or add a pallete!

Anyway thank you for your troubles!

Tom


0

Featured Post

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question