MFC 对话框添加背景图片
时间:2010-04-30 来源:cjjchong
1. 导入bitmap文件,生成ID :IDB_BITMAP2
导入时需要注意的是,只能导入bitmap的,可以将其他格式的图片转换成bmp的。
转换时,选择24位位图,这样才能尽可能的保真,16色和256色的会严重丢失颜色。
2. h文件
protected:
HICON m_hIcon;
// Generated message map functions
afx_msg void OnPaint();
3. cpp文件 1) BEGIN_MESSAGE_MAP(类名, CDialog) //{{AFX_MSG_MAP(CAutorunPacklist) ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP()
2)void 类名::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle 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;
// Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CPaintDC dc(this); CRect rect; GetClientRect(&rect); CDC dcMem; dcMem.CreateCompatibleDC(&dc); CBitmap bmpBackground; bmpBackground.LoadBitmap(IDB_BITMAP2); //IDB_BITMAP是你自己的图对应的ID BITMAP bitmap; bmpBackground.GetBitmap(&bitmap); CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground); dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0, bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
CDialog::OnPaint(); } }
3. cpp文件 1) BEGIN_MESSAGE_MAP(类名, CDialog) //{{AFX_MSG_MAP(CAutorunPacklist) ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP()
2)void 类名::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle 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;
// Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CPaintDC dc(this); CRect rect; GetClientRect(&rect); CDC dcMem; dcMem.CreateCompatibleDC(&dc); CBitmap bmpBackground; bmpBackground.LoadBitmap(IDB_BITMAP2); //IDB_BITMAP是你自己的图对应的ID BITMAP bitmap; bmpBackground.GetBitmap(&bitmap); CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground); dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0, bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
CDialog::OnPaint(); } }
相关阅读 更多 +