在大一时,学过MFC,不过那时刚接触编程,C/C++学完不久,跟重要的是没动手编程,后来又做ACM去了。现在大三了,各种实验课设老师都要求用VC可视化,于是借着机会学了点MFC皮毛,在此总结一些常用技巧,简直贻笑大方了!由于很多东西没有上升的理论层面,在此只能介绍下应用,方便学习交流,同时以备后面用到。
首先谈谈动态更新对话框背景图片,以营造动画效果。我只用到了其中一种,在此列出。
要动态更新对话框背景图片,就得先会添加背景图片,然后重载定时器函数实时更新即可。关于添加背景图片,敬请参考
http://blog.csdn.net/xj2419174554/article/details/1825741。
在该类OnInitDialog函数中添加SetTimer语句,如下:
BOOL CCarShowDlg::OnInitDialog() { nowOne=1; CDialog::OnInitDialog(); SetTimer(1, 200, NULL); return true; }
然后添加消息响应WM_TIMER。一般步骤为点击菜单栏“查看”,选择“建立类向导”,在相应对话框类中选择”WM_TIMER“,系统自动添加OnTimer函数,然后在里面添加代码即可,如下
void CCarShowDlg::OnTimer(UINT nIDEvent) { Invalidate(); CDialog::OnTimer(nIDEvent); }
此方法是调用OnPaint()函数动态更新的,所以应在OnPaint()函数中设置你想要的效果。举个例子
/************************************************ * *功能:重绘对话框背景,模拟小车行驶动画 * *************************************************/ void CCarShowDlg::OnPaint() { CPaintDC dc(this); CRect rc; GetClientRect(&rc); CDC dcMem; dcMem.CreateCompatibleDC(&dc); CBitmap bmpBackground; if(nowOne==1) { if(Direction) nowOne++; else nowOne--; bmpBackground.LoadBitmap(IDB_BITMAP1); } else if(nowOne==2) { if(Direction) nowOne++; else nowOne--; bmpBackground.LoadBitmap(IDB_BITMAP2); } else if(nowOne==3) { if(Direction) nowOne++; else nowOne--; bmpBackground.LoadBitmap(IDB_BITMAP3); } else if(nowOne==4) { if(Direction) nowOne++; else nowOne--; bmpBackground.LoadBitmap(IDB_BITMAP4); } else if(nowOne==5) { if(Direction) nowOne++; else nowOne--; bmpBackground.LoadBitmap(IDB_BITMAP5); } else if(nowOne==6) { if(Direction) nowOne++; else nowOne--; bmpBackground.LoadBitmap(IDB_BITMAP6); } else if(nowOne==7) { nowOne=1; bmpBackground.LoadBitmap(IDB_BITMAP1); } else if(nowOne==0) { nowOne=6; bmpBackground.LoadBitmap(IDB_BITMAP6); } BITMAP bitmap; bmpBackground.GetBitmap(&bitmap); CBitmap* pbmpPri = dcMem.SelectObject(&bmpBackground); dc.StretchBlt(0,0,rc.Width(), rc.Height(), &dcMem,0,0,bitmap.bmWidth, bitmap.bmHeight, SRCCOPY); }
上述程序的运行结果为:
以上代码可能参照先驱博客,在此表示感谢!如有错误,欢迎大牛斧正!