The displayed bitmap can be static or dynamic in the dialog box. Static mode is to use the resource editor of VC6.0. First, introduce a Bitmap to be displayed in the resource view, then place a Picture control in the dialog box, and select Bitmap in the Type drop-down box of Its Properties dialog box, enter the ID of the imported bitmap in the Image box. The imported bitmap is displayed in the compiled dialog box. This method has an obvious disadvantage, that is, the bitmap must be painted first and cannot be changed when the program is running. Based on this, the following describes how to dynamically display bitmaps in the dialog box by setting up a program to display the current time in a timely manner.
First, create a dialog box-based program named MyDialog.
Clear the TODO: Text in the dialog box, move the "OK" and "cancel" buttons to the bottom of the dialog box, and adjust the dialog box to the appropriate size.
Open Class View, right-clickCMyDialogDlg, add the following member variables:
Protected:
CRect m_bitmapRect; // The Position of the bitmap in the dialog box
Add drawing member function:
Protected:
BOOL DrawBitmap (CString strTime, CRect * bitmapRect );// StrTime indicates the time string. bitmapRect indicates the area in the window where the image is drawn.
UseClassWizard is the message response function OnTimer () that CMyDialogDlg adds WM_TIMER, and the message response function OnDestroy () of WM_DESTROY ().
InAdd the following code for CMyDialogDlg: OnInitDialog:
SetTimer (1,100, NULL); // sets the timer
CRect rect;
GetClientRect (& rect); // obtain the size of the customer Zone
ScreenToClient (& rect); // converts screen coordinates to client coordinates
Int width = 180, height = 45; // width and height of the bitmap to be created
M_bitmapRect.left = rect. right-25-width; // The right boundary of the bitmap is 25 pixels away from the right boundary of the dialog box. m_bitmapRect.top = rect. top + 35; // the upper boundary of the bitmap is 35 pixels above the upper boundary of the dialog box. m_bitmapRect.right = m_bitmapRect.left + width;
M_bitmapRect.bottom = m_bitmapRect.top + height;
Write drawing code.
BOOL CMyDialogDlg: DrawBitmap (CString str, CRect * bitmapRect)
{
CBitmap bitmap, * pOldBitmap;
CFont font, * pOldFont;
CDC SourceDC, * pDC;
BOOL result;
PDC = GetDC (); // get the device description table of the current window
If (pDC = NULL)
{
KillTimer (1 );
MessageBox ("system resources are insufficient, please close the program ");
Return FALSE;
}
SourceDC. CreateCompatibleDC (pDC); // create a bitmap compatible with the Display Device
Bitmap. CreateCompatibleBitmap (pDC, bitmapRect-> Width (), bitmapRect-> Height ());
Font. CreatePointFont (200, "Arial", pDC); // create the dot matrix font pOldBitmap = SourceDC. SelectObject (& bitmap); // select the bitmap into the memory field
POldFont = SourceDC. SelectObject (& font); // select the font to the memory Environment
SourceDC. SetBkMode (OPAQUE); // you can specify the background mode of a bitmap as opacity.
SourceDC. SetBkColor (RGB (0, 0, 0); // you can specify the black color of the bitmap background.
SourceDC. FillSolidRect (0, 0, bitmapRect-> Width (), bitmapRect-> Height (), RGB (0, 0); // fill the bitmap
SourceDC. SetTextColor (RGB (, 0, 0); // you can specify the text color.
// Display text in the in-place Graph
RECT rect;
Rect. left = 0, rect. top = 0, rect. right = bitmapRect-> Width (), rect. bottom = bitmapRect-> Height ();