Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bitmaps in Edit Controls

Posted on 1999-07-21
15
Medium Priority
?
254 Views
Last Modified: 2013-12-03
I want to stick a bitmap in the background of an edit control so that the text displayed is done so ontop of my picture. Any ideas how to do this??
0
Comment
Question by:DBH21
[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
  • 8
  • 6
15 Comments
 
LVL 1

Expert Comment

by:malmq
ID: 1310886
In a draw routine you could display the bitmap and then draw the text to the screen.
0
 
LVL 1

Expert Comment

by:sgerlach
ID: 1310887
Create a class derived from CEdit and override the OnPaint message.  See Example below:

void CMyEdit::OnPaint()
{
      CPaintDC dc(this); // device context for painting

      
      // Do not call CEdit::OnPaint() for painting messages      
      CString val;
      GetWindowText(val);
      dc.SelectStockObject(WHITE_BRUSH);
      CBitmap bmMyBitmap;
      bmMyBitmap.LoadBitmap("MYBITMAP");
      CDC memDC;
      memDC.CreateCompatibleDC(&dc);
      CBitmap* pOldBitmap = (CBitmap*) memDC.SelectObject(&bmMyBitmap);
      CRect ctrlRect;
      GetWindowRect(ctrlRect);
      ScreenToClient(ctrlRect);

        //Right align the bitmap image
      dc.StretchBlt(ctrlRect.right - BM_WIDTH,
                        ctrlRect.top,
                        BM_WIDTH,
                        ctrlRect.Height(),
                        &memDC,
                        0,
                        0,
                        BM_WIDTH,
                        BM_HEIGHT,
                        MERGECOPY);
      dc.SetBkMode(TRANSPARENT);
      dc.TextOut(0,0,val);      
      memDC.SelectObject(pOldBitmap);
            

}

0
 
LVL 1

Expert Comment

by:sgerlach
ID: 1310888
Now that I think about it, you should override both OnPaint and OnDraw.  

In OnPaint, call the OnDraw routine.  Move the code I put in OnPaint into OnDraw.  
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

Author Comment

by:DBH21
ID: 1310889
OK, I tried to implement this as the following...

void CBMEdit::OnPaint()
{
      OnDraw();
}


void CBMEdit::OnDraw()
{
      int BM_WIDTH      = 500;     //I made these arbitary
      int BM_HEIGHT      = 500;     //What should they be???
      
      CPaintDC dc(this);            //device context for painting

      //Do not call CEdit::OnPaint() for painting messages
      CString val;
      GetWindowText(val);
      dc.SelectStockObject(WHITE_BRUSH);
      CBitmap bmMyBitmap;
      bmMyBitmap.LoadBitmap(IDB_LOGO_EMBOSSED_SMALL);
      CDC memDC;
      memDC.CreateCompatibleDC(&dc);
      CBitmap* pOldBitmap = (CBitmap*) memDC.SelectObject(&bmMyBitmap);
      CRect ctrlRect;
      GetWindowRect(ctrlRect);
      ScreenToClient(ctrlRect);

      //Right align the bitmap image
      dc.StretchBlt(
            ctrlRect.right - BM_WIDTH,
            ctrlRect.top,
            BM_WIDTH,
            ctrlRect.Height(),
            &memDC,
            0,
            0,
            BM_WIDTH,
            BM_HEIGHT,
            MERGECOPY);
      dc.SetBkMode(TRANSPARENT);
      dc.TextOut(0,0,val);
      memDC.SelectObject(pOldBitmap);
}

But when I use it in a program, it doesn't draw the bitmap.


0
 
LVL 1

Expert Comment

by:sgerlach
ID: 1310890
Include the OnPaint message in your message map:

BEGIN_MESSAGE_MAP(CPBMEdit, CEdit)
//{{AFX_MSG_MAP(CPBMEdit)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BM_WIDTH and BM_HEIGHT should be the width and height that you want the bitmap to be.
0
 

Author Comment

by:DBH21
ID: 1310891
It almost works but I'm presented with two problems:
1) when text is displayed overtop the bitmap image, the entire line turns white, i.e. this is not transparent. I want the text to directly overtop the bitmap.

2) the bitmap on the screen takes up half the width, and the other half is white.

thanks again
0
 

Author Comment

by:DBH21
ID: 1310892
It almost works but I'm presented with two problems:
1) when text is displayed overtop the bitmap image, the entire line turns white, i.e. this is not transparent. I want the text to directly overtop the bitmap.

2) the bitmap on the screen takes up half the width, and the other half is white.

thanks again
0
 

Author Comment

by:DBH21
ID: 1310893
Scratch the second problem... I fixed that one
0
 
LVL 1

Expert Comment

by:sgerlach
ID: 1310894
Hmmm...try doing the TextOut before the StretchBlt.
0
 

Author Comment

by:DBH21
ID: 1310895
It still gives me that white bar - every line where text is entered, the background is erased and made white, but the picture below remains.

Arrrghh its so close
0
 

Author Comment

by:DBH21
ID: 1310896
k.. I got it to work... I shouldn't have rejected that answer... send another one and I'll pass the points


Thanks
0
 
LVL 1

Accepted Solution

by:
sgerlach earned 150 total points
ID: 1310897
Answer.  What did you do different?
0
 

Author Comment

by:DBH21
ID: 1310898
before I forget, one more question... how do I determine a bitmap's dimensions throught the program? I tried GetBitmapDimensions, but that returns 0 unless you set it initially.
0
 

Author Comment

by:DBH21
ID: 1310899
I made the edit control ReadOnly...

It all came down to the font actually... when I highlighted the text... it was a different font than the display, and thus would add a white text box...

if the fonts are the same, there is no problem
0
 
LVL 1

Expert Comment

by:sgerlach
ID: 1310900
This might be a round about way of doing it, but here is what I do:
CBitmap bm;
bm.LoadMappedBitmap(IDB_...);
BITMAP bmInfo;
bm.GetBitmap(&bmInfo);
//bm.bmWidth -> Width
//bm.bmHeight -> Height

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

670 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