用static text显示图像,并设置按钮点击下一张
效果图:
点击上一张按钮,显示上一张
点击下一张按钮,显示下一张
点击自动按钮,自动显示文件内图像
1.在界面上添加static text控件,并添加变量为m_pic;添加三个按钮,更改名字为“上一张”、“自动”、和“下一张”,并为按钮添加变量分别为:m_buttonpre,m_buttonauto,m_buttonnext。
2.在CxxxDlg中的初始化代码中添加以下代码:
添加两个接收变量:m_w,m_h。
m_pic.ModifyStyle(0xf, SS_BITMAP | SS_CENTERIMAGE); //图像显示模式
#define HBMP(filepath,width,height) (HBITMAP)LoadImage(AfxGetInstanceHandle(),filepath,IMAGE_BITMAP,width,height,LR_LOADFROMFILE|LR_CREATEDIBSECTION)
CRect rect; //定义区域
m_pic.GetWindowRect(&rect);
m_w = rect.Width();
m_h = rect.Height();
i = 1; //循环标志位
CString path; //定义路径
path.Format(TEXT("E:/中策/0308/030801/%d.bmp"),i); //获得路径
m_pic.SetBitmap(HBMP(path, m_w, m_h)); //赋予图像宽高
m_isauto = true; //是否自动
注意,#define内容结尾没有分号。
3.为“下一张”按钮添加点击事件:
i++;
if (i > 24)
{
i = 1;
}
CString path;
path.Format(TEXT("E:/中策/0308/030801/%d.bmp"),i);
m_pic.SetBitmap(HBMP(path, m_w, m_h));
4.为“上一张”添加点击事件:
i--;
if (i < 1)
{
i = 24;
}
CString path;
path.Format(TEXT("E:/中策/0308/030801/%d.bmp"),i);
m_pic.SetBitmap(HBMP(path, m_w, m_h));
5.为“自动”按钮添加点击事件:
if (true == m_isauto)//按下自动按钮
{
//上一张下一张按钮变灰
m_buttonpre.EnableWindow(FALSE);
m_buttonnext.EnableWindow(FALSE);
//自动 -> 暂停
m_buttonauto.SetWindowTextW(TEXT("暂停"));
//启动定时器
SetTimer(1, 100, NULL);
}
else//按下暂停按钮
{
//上一张下一张按钮恢复
m_buttonpre.EnableWindow(TRUE);
m_buttonnext.EnableWindow(TRUE);
//暂停 -> 自动
m_buttonauto.SetWindowTextW(TEXT("自动"));
//停止定时器
KillTimer(1);
}
m_isauto = !m_isauto;
由于“自动”按钮调用了time函数,所以定义time函数
//定时器处理函数
void Ctest4Dlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialogEx::OnTimer(nIDEvent);
//调用下一张函数
OnBnClickedButton3();
}