1、下面代码实现了dialog上图片移动,图片可以使bmp、gif等;
2、本代码解决了两个难题:
(1)曾经试过很多方法在dialog上加载图片,但是大多数都是bmp格式,不支持gif格式,并且图片还不能拉伸;
(2)在dialog上加载图片,图片和dialog会产生重叠的效果;
3、首先得添加PictureEx.h和PictureEx.cpp两个文件,其中CPictureEx类支持gif格式,并且能拉伸图片;
CPictureEx m_gif;//m_gif为picture control控件的变量,类型为CPictureEx;
4、下面这两行代码实现了使dialog透明,其上面的图片不透明,
SetWindowLong(this->m_hWnd, GWL_EXSTYLE, GetWindowLong(this->m_hWnd, GWL_EXSTYLE) ^ 0x80000);
SetLayeredWindowAttributes(GetSysColor(COLOR_BTNFACE), 0, 1);
5、加载图片
CRect rect;
GetDlgItem(IDC_STATIC_PICTURE)->GetClientRect(rect);
if (m_gif.Load(_T("1.gif")))
{
m_gif.SetPaintRect(&rect);
m_gif.GetPaintRect(&rect);
m_gif.Draw();
}
SetTimer(1, 20, NULL);
6、定时器里实现图片移动
int upstep = 0;//左右移动
int rigthstep = 0;//上下移动
void DialogStar::OnTimer(UINT_PTR nIDEvent)
{
CRect crect;//图片移动后新的rect
CRect rect;//图片的rect
CRect prect;//dialog的rect
// TODO: Add your message handler code here and/or call default
switch (nIDEvent)
{
case 1:
upstep += 2;
rigthstep += 3;
GetDlgItem(IDC_STATIC_PICTURE)->GetClientRect(rect);
GetDlgItem(IDC_STATIC_PICTURE)->GetParent()->GetClientRect(prect);
crect.top = prect.bottom - rect.bottom - upstep;
crect.bottom = crect.top + rect.bottom;
crect.left = rigthstep;
crect.right = crect.left + rect.right;
GetDlgItem(IDC_STATIC_PICTURE)->MoveWindow(crect);
if (crect.top <= 0 || (crect.left > prect.right - rect.right))
{
KillTimer(1);
GetDlgItem(IDC_STATIC_PICTURE)->CloseWindow();
upstep = 0;
rigthstep = 0;
CDialogEx::OnCancel();
}
break;
default:
break;
}
CDialogEx::OnTimer(nIDEvent);
}