Solved

How is the pallete changed using the CPallete Class

Posted on 1997-08-06
5
350 Views
Last Modified: 2013-11-20
How is the pallete changed using the CPallete Class.
I want to know how to change the pallete on a window using the CPallete class.
0
Comment
Question by:Maisonave
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:davmarc
ID: 1303166
The high-level answer is to create a new CPalette instance, set its color entries and properties, and apply it to a device context (GDI objects are not directly related to windows)

You'll be able to find a lot of documentation about palettes on the MSDN Library. An old but still great article you can't miss if you want to know everything about the Windows Palette Manager is "The Palette Manager: How and Why" by Ron Gery, again on MSDN.

Davide Marcato.
0
 

Author Comment

by:Maisonave
ID: 1303167
I don't have access to MSDN library.
I already new that a CPalette instance had to be created.
My question was how do I use the class to change the palette of a window.
For example, I have the following code:
      CFile file;
      CPalette palette;
      MYPALETTE pal;
      pal.palVersion = 0x300;
      pal.palNumEntries = 256;
      CString palettefile = FileLocation.Left(FileLocation.ReverseFind('\\')+1) + "palette.dat";
      if (!file.Open(palettefile, CFile::modeRead, NULL))
      {
            return;
      }
      file.Read(&pal.palPalEntry,768);
      file.Close();
      palette.CreatePalette((tagLOGPALETTE*) &pal);
      PALETTEENTRY pal_s;
      for (int p = 0;p<256;p++)
      {
            memcpy(&pal_s,pal.palPalEntry+p*3,3);
            palette.SetPaletteEntries(p, 1,&pal_s);
      }
      m_Static.GetDC()->SelectPalette(&palette, FALSE);
      m_Static.GetDC()->RealizePalette();
//********************************************************************
In the above code, I pulled out palette data from a file.
I then tried to modify the CPalette class using the data.
I then tried to apply the CPalette class to the attached window.
However, I get no effect.  The palette does not change.

Some my question is, "How is a window modified using CPalette Class?"



0
 
LVL 4

Expert Comment

by:davmarc
ID: 1303168
Sorry for not understanding the point of your question at first.
Your code seems fine. Curios at the name of the window (m_Static)...is it a CStatic member? If so, maybe the problem has to do with the control not caring of your settings while drawing itself. Try your code on another non-control window.
If this is the case, consider handling the message WM_CTLCOLOR or  something similar depending on what your goal is.

As for MSDN, you can freely access the online version of the   library if you take a minute to register at http://www.microsoft.com/msdn.

Davide Marcato.
0
 

Author Comment

by:Maisonave
ID: 1303169
I don't have access to MSDN library.
I already new that a CPalette instance had to be created.
My question was how do I use the class to change the palette of a window.
For example, I have the following code:
      CFile file;
      CPalette palette;
      MYPALETTE pal;
      pal.palVersion = 0x300;
      pal.palNumEntries = 256;
      CString palettefile = FileLocation.Left(FileLocation.ReverseFind('\\')+1) + "palette.dat";
      if (!file.Open(palettefile, CFile::modeRead, NULL))
      {
            return;
      }
      file.Read(&pal.palPalEntry,768);
      file.Close();
      palette.CreatePalette((tagLOGPALETTE*) &pal);
      PALETTEENTRY pal_s;
      for (int p = 0;p<256;p++)
      {
            memcpy(&pal_s,pal.palPalEntry+p*3,3);
            palette.SetPaletteEntries(p, 1,&pal_s);
      }
      m_Static.GetDC()->SelectPalette(&palette, FALSE);
      m_Static.GetDC()->RealizePalette();
//********************************************************************
In the above code, I pulled out palette data from a file.
I then tried to modify the CPalette class using the data.
I then tried to apply the CPalette class to the attached window.
However, I get no effect.  The palette does not change.

So my question is, "How is a window modified using CPalette Class?"



0
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
ID: 1303170
Is m_Static an object of CStatic? Static Controls do not support a 256 colors palette, they use the standard 20 colors Windows reserves for system use because they use RGB macros mapped to the 20 colors instead of PALETTERGB macros internally.

If m_Static is an object of CWnd, you can do it with PALETTERGB or PALETTEINDEX or Blt a 256 colors bitmap. And you must process WM_QUERYNEWPALETTE and WM_PALETTECHANGED messages in your top-level window (the frame window) as shown below.

BEGIN_MESSAGE_MAP(CTestWnd, CFrameWnd)
    ...
    ON_WM_PALETTECHANGED()
    ON_WM_QUERYNEWPALETTE()
    ...
END_MESSAGE_MAP()

//-----------------------------------------------------------------------------
afx_msg void CTestWnd::OnPaletteChanged(CWnd *pFocusWnd)
{
    if (pFocusWnd != this && !this->IsChild(pFocusWnd))
         this->Invalidate();
}
//-----------------------------------------------------------------------------
afx_msg BOOL CTestWnd::OnQueryNewPalette()
{
    this->Invalidate();
   
    return FALSE;
}

In OnPaint(), you need to SelectPalette() and RealizePalette().
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running shell command from Access 3 69
countEvens challenge 2 78
has77  challenge 9 73
scoreUp challenge 14 57
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now