Solved

Simple BitBlt Operation

Posted on 1997-04-24
5
439 Views
Last Modified: 2013-12-04
I can't get a simple bitblt to work within a dialog box. I have an messagebox() telling me that it's drawn, but we see no pretty pictures. BTW, this is in the OnPaint() function.. Here's some code:

      //MY CODE
      CPaintDC dc(this);
      HBITMAP hBitmap;
      if ((hBitmap= ::LoadBitmap( m_hInstance,
            MAKEINTRESOURCE( IDB_BITMAP_GRIDBASE ) ))==NULL)
            MessageBox("Bitmap not loaded");
      HDC hMemDC;
      if ((hMemDC = ::CreateCompatibleDC(dc.GetSafeHdc()))==NULL)
            MessageBox("Error in CreateCompatibleDC");
      SelectObject(hMemDC, hBitmap);
      if (!(StretchBlt(dc.m_hDC,0,0,179,179,hMemDC,0,0,179,179,SRCCOPY)))
            MessageBox("The Bitmap was not drawn");
      else
            MessageBox("The Bitmap was drawn");
      ::DeleteDC(hMemDC);
      ::DeleteObject(hBitmap);

I've gotten no error messages, none of my 'error' messagebox()es, but a box telling me the bitmap was drawn. It's not working. :) Any and all help would be appreciated..

CD
0
Comment
Question by:cdanek
  • 3
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
NickRepin earned 50 total points
ID: 1396910
You should use CPaintDC in response to a WM_PAINT message only. In other cases use CClientDC (I'm not sure - i'm using Borland OWL, ie TPaintDC, TClientDC...)
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1396911
If you will not satisfied with my answer, please post more code to clarify context you using.
0
 

Author Comment

by:cdanek
ID: 1396912
Edited text of question
0
 

Author Comment

by:cdanek
ID: 1396913
BTW, this function -is- called under a WM_PAINT msg. Any other ideas?
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1396914
Please do not accept answer if you don't satisfied with it.Just add comments or reject it. This lets you save your points if I (or somebody) will no be able to continue discussion (ie you can just reject wrong answer at any time).Here's Article Q107765 from MS knowledge base. I hope it will be useful. Try to modify your code as shown in (a).P.S. May be better for you to use CONTROL statement with SS_BITMAP style in DIALOG resource to avoid draw-coding?=================================================
The information in this article applies to:

(a) The Microsoft Foundation Classes (MFC) included with:

    - Microsoft C/C++ version 7.0
    - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and 1.52
    - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, and 2.1



This is the 16-bit version of this sample. There is an equivalent 32-bit sample available under the name BDLG32.

SUMMARY

In a Microsoft Foundation Class (MFC) Libraries application for Windows, it is occasionally useful to be able to display a bitmap larger than a normal icon in a dialog box.
You cannot automatically add a bitmap to a dialog box template using a dialog box editor or App Studio without using a third-party custom control, such as a VBX picture control. To have a bitmap larger or smaller than an icon display on a dialog box without such a control, the bitmap must be painted on the dialog box at run time using some other method. Four possible methods for doing this are:

(a) BitBlt() a bitmap onto the dialog box in its OnDraw() handler.
(b) BitBlt() or StretchBlt() into a "static" frame control.
(c) BitBlt() or StretchBlt() in OnEraseBkgnd().
(d) Use a CBitmapButton as a picture control.

The sample BDLG demonstrates these four methods for placing a bitmap on a dialog box. BDLG can be found in the Microsoft Software Library by searching on the word BDLG, the Q number of this article, or S14421.
MORE INFORMATION

The following four methods, implemented in the BDLG sample, demonstrate some possible ways to display a bitmap in a dialog box using the Microsoft Foundation Classes.

Method 1: BitBlt() in OnDraw()

The first method uses a simple BitBlt(), when the dialog box receives a WM_PAINT message, to paint a bitmap in a hard-coded location on a dialog box. This method demonstrates the following:

(a) Using LoadBitmap().
(b) Creating a compatible memory device context (DC).
(c) Saving a handle to an object selected out of a DC between messages.
(d) Using BitBlt().
(e) Cleaning up a loaded bitmap and memory DC when finished with them.

Because this is a common technique, a basic version of the few steps needed to draw a bitmap on a dialog box using MFC are listed below. These steps assume you have already created a C++ class, named CMyDlg, and that it has been associated with either a dialog box template created in App Studio or with a dialog editor.
(1) Add a bitmap resource with an ID of IDB_MYBITMAP (or whatever ID you want) to your project. With App Studio, you can do this by either creating a new bitmap resource and setting the ID, or by choosing Resource, Import, and importing an already created bitmap with a .BMP extension.
(2) Add a WM_PAINT handler to your CMyDlg class. With Visual C++ 1.0, just use Class Wizard to add an OnPaint() handler to your dialog box class.
(3) Now, your OnPaint() function will have to load and use BitBlt() to paint the bitmap in your resource onto the dialog box. The code to do this is the following:void CMyDlg::OnPaint() {

      CPaintDC dc( this ); // Device context for painting


      CBitmap bmp, *poldbmp;
      CDC memdc;


      // Load the bitmap resource
      bmp.LoadBitmap( IDB_CORPLOGO );


      // Create a compatible memory DC
      memdc.CreateCompatibleDC( &dc );


      // Select the bitmap into the DC
      poldbmp = memdc.SelectObject( &bmp );


      // Copy (BitBlt) bitmap from memory DC to screen DC
      dc.BitBlt( 10, 10, 47, 47, &memdc, 0, 0, SRCCOPY );


      memdc.SelectObject( poldbmp );


      // Do not call CDialog::OnPaint() for painting messages
}After adding this code and adding an OnPaint entry to the message map for your CMyDlg class, you should see the bitmap image displayed in the dialog box at run time.

Note that the first four parameters to CDC::BitBlt() depend on your program and the size of the bitmap resource. The first two (10, 10) position the upper-left corner where the bitmap will be drawn on the dialog box's client area. The second two (47, 47) specify the width and height of the bitmap area to copy over in logical units. These dimensions can be less than the size of the actual bitmap. The sixth and seventh parameters specify the upper-left corner of the bitmap in the memory DC to start copying from.One other thing to note is that the CDC and CPaintDC objects in the code above are created on the stack so you do not need to call the Windows API functions DeleteDC() and ReleaseDC() on the memory and paint DC objects, respectively.
For more information about bitmaps and device contexts, refer to the documentation for the Windows API and MFC versions of BitBlt(), SelectObject(), and CreateCompatibleDC(). For a more complete example of this, please see the BDLG sample.

Method 2: BitBlt() or StretchBlt() in "Static" Frame Control

The second method uses StretchBlt() and, using techniques similar to those in the "Paint" dialog box, draws the bitmap in the client area of a "static" picture frame control.
The BDLG sample uses Class Wizard to associate a CStatic member variable with the static control using its "Edit Variables" capability. Besides the above techniques, it demonstrates calculating the client area of a child control. Note that subclassing the CStatic member (after using Class Wizard to associate the control, just change the type in the header from CStatic to a class you've derived from CStatic) would allow you to have the subclassed control paint a bitmap in its client area when it itself received a WM_PAINT.

Method 3: BitBlt() or StretchBlt() in OnEraseBkgnd()

The third method StretchBlt's the bitmap onto the background of a dialog box in the dialog box's WM_ERASEBKGND handler, OnEraseBkgnd(). It also demonstrates handling WM_CTLCOLOR messages and returning background brushes from that handler (transparent in this case).

Method 4: CBitmapButton as Picture Control

The fourth and last method uses a CBitmapButton class to CBitmapButton::AutoLoad() a bitmap into a disabled owner-draw button on a dialog box. It is probably the simplest method of the four, although it does not allow for much flexibility or changing of what is being painted to reflect changes in the dialog box.
Please note the following when creating your resources for using a CBitmapButton class. When you add the button to your dialog box template, which you will later be associating with a CBitmapButton, the ID is relatively unimportant, but the button must be set as owner-draw and the caption text will be used to load your bitmap. If your button caption text is "BITB", you should create your bitmap and give it an ID of "BITBU". Your bitmap ID MUST have the double quotation marks or it will be saved as just a numeric ID and fail to load when you use CBitmapButton::AutoLoad(). See the documentation for CBitmapButton for more information.

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 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

11 Experts available now in Live!

Get 1:1 Help Now