From: cs92njc@cyrillus.brunel.ac.uk.ac.uk (Nicholas J Clayton)
Newsgroups: comp.os.ms-windows.programmer.misc
Subject: Bitmaps on a dialog box
Date: 22 Jan 93 01:16:43 GMT
Reply-To: cs92njc@cyrillus.brunel.ac.uk.ac.uk
Organization: Sun Microsystems, Inc.

A while ago I asked if anyone knew how draw a bitmap onto a dialog box, to achieve the same sort of affect as the large 'icon' in the WinWord About box. Thanks to everyone who replied. The following is the method I used:

[ABOUT.DLG - The dialog definition file]
DLGINCLUDE RCDATA DISCARDABLE
BEGIN
    "ABOUT.H\0"
END
/* ABOUT.H #defines IDI_ABOUT 101 */

/* This line references a bitmap file to be used on the dialog	*/
About BITMAP c:\coverage\help\coverage.bmp

100 DIALOG 84, 44, 152, 64
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "About Coverage Chart Generator"
FONT 8, "MS Sans Serif"
BEGIN
    DEFPUSHBUTTON   "OK", IDOK, 56, 44, 40, 14
    LTEXT           "Program name here", -1, 44, 8, 96, 12
    LTEXT           "Copyright message here", -1, 44, 32, 95, 8
    LTEXT           "Version number here", -1, 44, 20, 92, 8
/* This is the important line. We define a control, with ID IDI_ABOUT. This */
/* control is an ownerdrawn control so that Windows will send us messages   */
/* saying when the picture on the control needs to be drawn. AFAIK, the     */
/* dialog editor will not create this line correctly. You need to edit the  */
/* file and add the window style parameters yourself.                       */   
    CONTROL         "", IDI_ABOUT, "BUTTON", BS_OWNERDRAW | WS_CHILD | WS_VISIBLE, 4, 4, 32,
                    32
END
[END ABOUT.DLG]

Each dialog box in my program has a seperate .C source file. The following is excerpted from ABOUT.C

[ABOUT.C]
    case WM_DRAWITEM:
    {
        HBITMAP hAboutBitmap;
	HDC hDCAbout;
	HDC hDCMem;

	/* Get a DC for the about frame				*/
	hDCAbout = GetDC(GetDlgItem(hWndDlg, IDI_ABOUT));

	/* Create a compatible DC from the frame                */
	hDCMem = CreateCompatibleDC(hDCAbout);

	hAboutBitmap = LoadBitmap(hInst, "About");

        /* Select the bitmap into the memory DC			*/
    	SelectObject(hDCMem, hAboutBitmap);

	/* Blit it onto the box					*/
	BitBlt(hDCAbout, 0, 0, 64, 64, hDCMem, 0, 0, SRCCOPY);

	/* Delete the two DCs and the bitmap			*/
    	DeleteDC(hDCAbout);
    	DeleteDC(hDCMem);
	DeleteObject(hAboutBitmap);
    }
    return FALSE;

Hopefully, the above code is fairly simple to follow. The first 3 lines initialise the working variables for this section of code. hAboutBitmap is a handle to the bitmap, while hDCAbout and hDCMem are DCs for the About box and a memory block respectively.

The next line is the only method I could use to get a DC for the about frame. If anyone knows of less ugly method, please let me know (I have an aversion to functions which use information from functions in their parameter list). Then the program creates a compatible memory DC into which the bitmap can be selected.

The bitmap is then loaded. Note that "About" is the name assigned to the bitmap in the .dlg file earlier. We then select the bitmap into memory DC, ready for blitting. Bitblt then blits the contents of the memory DC (the bitmap) onto the about frame DC. Is this the simplest way of doing it, or is there a method of blitting without the need to first select the bitmap into the memory DC? It does seem to be a longwinded way of going about it.

The last three lines just delete the three resources that were used by the program.

My thanks again to those who responded to my original posting.

Rgds, Nik
---
_/     _/ _/_/_/ _/  _/  For those who are wondering why,
_/_/   _/   _/   _/ _/   What we call SF ain't Sci-Fi,    
_/  _/ _/   _/   _/_/         It's just there a fine line 
_/   _/_/   _/   _/ _/        Between Robert Heinlein,      
_/     _/ _/_/_/ _/  _/  And "Son of the Two Headed Fly"