MFC里面添加背景方法很多,我分享自己的一种不闪烁的方法。
PS:里面用到的实例不是最简单的方法,自己琢磨下吧。
先创建一个基本对话框型的APP类的MFC工程,我下面展示已纸牌程序Poker开发为例。
1.首先创建一个CGame的父类,头文件部分:
class CGame{
public:
virtual ~CGame(){};
CGame(int,int,int);
void InitGame();
void DrawBackGround();
CDC* GetGameDC();
private:
int BKImage;//游戏主界面图片ID
protected:
CDC* GameDC;//游戏主界面的内存显示屏
int Height;
int Width;
};
这个是函数部分:
CGame::CGame(int width,int height,int bk){
Width=width;
Height=height;
BKImage=bk;
}
void CGame::InitGame(){
DrawBackGround();
}
void CGame::DrawBackGround(){
// delete GameDC;
GameDC=new CDC();
CBitmap bm;
if(bm.LoadBitmap(BKImage))//if else
{
GameDC->CreateCompatibleDC(NULL);
GameDC->SelectObject(&bm);
bm.DeleteObject();
}
}
CDC* CGame::GetGameDC(){
return GameDC;
}
注意DrawBackGround()是主要的函数
2.然后在CPokerDlg.h里面添加
#include "CGame.h"
和声明:
private:
CGame *Game;
3.在CPokerDlg::OnInitDialog()的
// TODO: Add extra initialization here下面添加
CRect rect;
this->GetClientRect(&rect);
Game=new CGame(rect.right,rect.bottom,IDB_bkBMP);
Game->InitGame();
4在CPokerDlg::OnPaint() 函数里面的else部分添加
CDialog::OnPaint();
CRect rect;
this->GetClientRect(&rect);
CClientDC dc(this);
CDC* gameDC=Game->GetGameDC();
dc.BitBlt(0,0,rect.right,rect.bottom,gameDC,0,0,SRCCOPY);//这里的就是重点,控制位图输出的形状