1.将bmp文件放入到程序所在文件夹下并导入bmp文件
2.对类C0Dlg中的OnPaint函数修改代码:
void Czjj0Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); //用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
//使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
//绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this); //窗体DC
CBitmap bmp; //位图变量
bmp.LoadBitmap(IDB_BITMAP1); //载入位图资源
CDC memdc; //临时DC
memdc.CreateCompatibleDC(&dc); //创建临时DC
memdc.SelectObject(&bmp); //选中位图对象
int width, height; //定义位图宽度和高度
BITMAP bmp1;
bmp.GetBitmap(&bmp1); //获取位图信息
width = bmp1.bmWidth; //位图宽度
height = bmp1.bmHeight; //位图高度
CRect rect;
this->GetClientRect(&rect); //获取窗体大小
//将位图绘制在窗体上作为背景
dc.StretchBlt(rect.left, rect.top, rect.Width(),rect.Height(), &memdc, 0, 0, width, height, SRCCOPY);
}
}
1.将bmp文件放入到程序所在文件夹下并导入bmp文件
2.对类C0Dlg中的OnPaint函数修改代码:
void Czjj0Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); //用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
//使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
//绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
CDC dcbmp;
dcbmp.CreateCompatibleDC(&dc);
CBitmap Background;
Background.LoadBitmap(IDB_BITMAP1);
BITMAP bmp;
Background.GetBitmap(&bmp);
CBitmap* pbmpOld = dcbmp.SelectObject(&Background);
dc.StretchBlt(0, 0, rect.Width(), rect.Height(), &dcbmp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
}
}
3.添加WM_ERASEBKGND消息的处理函数,避免背景的闪烁。