Solved

changing a color in TBitmap

Posted on 1998-09-01
4
297 Views
Last Modified: 2013-12-26
Hi y´all,

Painting the Bitmaps isn´t all that difficult, but handling any special
behaviour is.

I´m starting out with a bitmap having a flag.
Different players should have different colors, still using the same bitmap (the
flag if you will).
I somehow cannot extract the palette from the bitmap.

The Bitmap is defined in a resource.

code as follows:
foo::Paint(TDC &DC...)
{
 TBitmap *BM = new TBitmap(GetModule(),IDB_FLAG);
 //paints ok (using TMemoryDC and SelectObject)

 //I´ve tried this (a little naíve...)
 TPalette Palette(*BM);
 //does compile but also generates an access of 0xfffff
 //not good.
 
}

if( any ideas ) { :-) }
else { :-(( }
0
Comment
Question by:MostlyWater
[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
4 Comments
 
LVL 2

Expert Comment

by:duneram
ID: 1299771
0
 
LVL 2

Expert Comment

by:duneram
ID: 1299772
the last one has some important notes for using TPalette....
0
 
LVL 3

Accepted Solution

by:
brosenb0 earned 100 total points
ID: 1299773
I just had the same problem recently.  The way I got around it was to derive a class from TDib and include palette handling and painting functionality.  I'll include the source code for you to play with.

//MyDIB.h

#if !defined(__mydib_h)              // Sentry, use file only if it's not already included.
#define __mydib_h

/*
       OVERVIEW
       ========
       Class definition for TMyDib (TDib).
*/

#include <owl\owlpch.h>
#pragma hdrstop


#include "ntnstlap.rh"            // Definition of all resources.


//{{TDib = TMyDib}}
class TMyDib : public TDib {
public:
       TMyDib (HGLOBAL handle, TAutoDelete autoDelete = NoAutoDelete);
       TMyDib (HINSTANCE instance, TResId resID);
       TMyDib (const char *name);
       BOOL SetupDib();
       BOOL CreatePalette();
       Paint  (TDC &dc, TRect rectDC, TRect rectDIB);
       virtual ~TMyDib ();

protected:
      TPalette *m_pPalette;
};    //{{TMyDib}}


#endif                                      // __mydib_h sentry.


//MyDIB.Cpp
/*
    OVERVIEW
    ========
    Source file for implementation of TMyDib (TDib).      
*/

#include <owl\owlpch.h>
#pragma hdrstop

#include "mydib.h"


//{{TMyDib Implementation}}

//--Create from handle to existing dib
TMyDib::TMyDib (HGLOBAL handle, TAutoDelete autoDelete):
       TDib(handle, autoDelete)
{
       // INSERT>> Your constructor code here.
      SetupDib();
}

//--Create from resource
TMyDib::TMyDib(HINSTANCE instance, TResId resID):
      TDib(instance, resID)
{
      SetupDib();
}

//--Load from .bmp or .dib file
TMyDib::TMyDib(const char *name):
      TDib(name)
{
      SetupDib();
}


TMyDib::~TMyDib ()
{
       // INSERT>> Your destructor code here.
      if(m_pPalette) delete m_pPalette;
}

BOOL TMyDib::SetupDib()
{
      BOOL bResult;

      //--Allocate objects
      m_pPalette = new TPalette(*this);
      if(!m_pPalette) {
            bResult = FALSE;
            goto _Done;
      }

      //--Create the dib's palette
      bResult = CreatePalette();

      //--Add what ever else you want to setup here

_Done:
      return bResult;
}

BOOL TMyDib::CreatePalette()
{
      BITMAPINFO   *pInfo;
      PALETTEENTRY  palEntry;
      WORD wNumColors;

      pInfo = GetInfo();
      wNumColors = (WORD)NumColors();

      if(wNumColors) {
            for (int i = 0; i < (int)wNumColors; i++) {
                  palEntry.peRed   = pInfo->bmiColors[i].rgbRed;
                  palEntry.peGreen = pInfo->bmiColors[i].rgbGreen;
                  palEntry.peBlue  = pInfo->bmiColors[i].rgbBlue;
                  palEntry.peFlags = 0;
                  m_pPalette->SetPaletteEntry(i, palEntry);
            }
      }
      return TRUE;
}


TMyDib::Paint  (TDC &dc, TRect rectDC, TRect rectDIB)
{
      BOOL bResult;

      //--Get the DIB's palette, then select it into DC
      if (m_pPalette == NULL)
            if(!SetupDib())
                  return FALSE;

      //--Select as background palette & realize the palette
      dc.SelectObject(*m_pPalette, TRUE);
      dc.RealizePalette();

      //--Make sure to use the stretching mode, best for color pictures
      dc.SetStretchBltMode(COLORONCOLOR);

      //--Determine whether to call StretchDIBits() or SetDIBitsToDevice()
      if ((rectDC.Width() == rectDIB.Width()) &&
            (rectDC.Height() == rectDIB.Height()))
            bResult = dc.SetDIBitsToDevice(rectDC, rectDIB.TopLeft(), (TDib)*this);
      else
            bResult = dc.StretchDIBits(rectDC, rectDIB, (TDib)*this, SRCCOPY);

      return bResult;
}

To implement the class, create a TMyDib pointer as a member of your view (or dialog) class.  Instantiate a new object during view or dialog initialization, pass parameters to the TMyDib constructor.  Delete the TMyDib object in your view's destructor.  Don't create a new object each time your paint function is called or your painting will be very slow and will flicker.  I implemented it in a dialog like this: -

class MyTDLGClient : public TDialog {
public:
    MyTDLGClient (TWindow *parent, TResId resId = IDD_CLIENT, TModule *module = 0);
    virtual ~MyTDLGClient ();

public:
    {.....}
   
protected:
    void EvPaint ();
    {.....}

    TMyDib    *m_pDibLogo;
};

bool MyTDLGClient::EvInitDialog (HWND hWndFocus)
{
    bool bResult;

    bResult = TDialog::EvInitDialog(hWndFocus);

    // INSERT>> Your code here.
    m_pDibLogo = new TMyDib(GetApplication()->GetInstance(), IDB_SETUPLOGO);

    return bResult;
}

void MyTDLGClient::EvPaint ()
{
    TDialog::EvPaint();

    // INSERT>> Your code here.
    TClientDC dc(*this);
    HWND hwndStatic;
    TRect rectDest;
    TPoint topLeft, botRight;

    //--Get pointer to app's TModule
   TModule *pModule = GetApplication();

   //--I painted into a static control to get the sunken border.  You can paint where ever
   //--you want.  Just create a rectangle within the dest. dc.

   //--Get the static control's rectangle
   hwndStatic = GetDlgItem(IDC_STATICLOGO);
   TWindow wndStatic(hwndStatic, pModule);
   rectDest = wndStatic.GetWindowRect();

    //--Convert the rectangle to client coordinates
    topLeft  = rectDest.TopLeft();
    botRight = rectDest.BottomRight();
    ScreenToClient(topLeft);
    ScreenToClient(botRight);
    rectDest.Set(topLeft.x, topLeft.y, botRight.x, botRight.y);

    //--Create a rectangle for the DIB
    TRect rectDIB(TPoint(0,0), TPoint(m_pDibLogo->Width(), m_pDibLogo->Height()));

    //--Paint the DIB
    m_pDibLogo->Paint(dc, rectDest, rectDIB);
}


MyTDLGClient::~MyTDLGClient
{
    if(m_pDibLogo) delete m_pDibLogo;
}
0
 

Author Comment

by:MostlyWater
ID: 1299774
Adjusted points to 100
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
selectSingleNode in Access 2013 11 78
Tech Writing Terminology - Pane or Panel? 8 137
two submit buttons one form 15 114
WCAG (Web Content Accessibility Guidelines) levels 3 143
When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
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.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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