CImgPlayerDlg.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // CImgPlayerDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "libimgplayer.h"
  5. #include "CImgPlayerDlg.h"
  6. #include "afxdialogex.h"
  7. #include <io.h>
  8. #define TIMERID 8
  9. CImgPlayerDlg::CImgPlayerDlg(CWnd* pParent /*=NULL*/)
  10. : CDialogEx(CImgPlayerDlg::IDD, pParent), m_pConfig(NULL)
  11. {
  12. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  13. m_bInit = false;
  14. m_bTimer = false;
  15. m_hMonitor = NULL;
  16. m_nPicInx = 0;
  17. m_nPlayCnt = 0;
  18. m_pMemDC = new CDC();
  19. m_pMemBitmap = new CBitmap();
  20. m_pOldBitmap = NULL;
  21. }
  22. CImgPlayerDlg::CImgPlayerDlg(CImgPlayConfig *pConfig, CWnd* pParent /*= NULL*/)
  23. : CDialogEx(CImgPlayerDlg::IDD, pParent), m_pMemDC(NULL), m_pMemBitmap(NULL)
  24. {
  25. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  26. m_bInit = false;
  27. m_bTimer = false;
  28. m_hMonitor = NULL;
  29. m_pConfig = pConfig;
  30. m_nPicInx = 0;
  31. m_nPlayCnt = 0;
  32. m_pMemDC = new CDC();
  33. m_pMemBitmap = new CBitmap();
  34. m_pOldBitmap = NULL;
  35. }
  36. CImgPlayerDlg::~CImgPlayerDlg()
  37. {
  38. delete m_pMemBitmap; m_pMemBitmap = NULL;
  39. delete m_pMemDC; m_pMemBitmap = NULL;
  40. }
  41. void CImgPlayerDlg::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CDialogEx::DoDataExchange(pDX);
  44. }
  45. void CImgPlayerDlg::ShowImage(CImage& img, UINT ID)
  46. {
  47. // 获得显示控件的DC
  48. CDC* pDC = m_pMemDC;
  49. // 获取HDC(设备句柄)来进行绘图操作
  50. HDC hDC = pDC->GetSafeHdc();
  51. CRect rect;
  52. GetDlgItem(ID)->GetClientRect(&rect);
  53. // 读取图片控件的宽和高
  54. int rw = rect.right-rect.left;
  55. int rh = rect.bottom-rect.top;
  56. // 设置图片控件背景色为黑色
  57. CBrush brush(RGB(0,0,0));
  58. pDC->FillRect(CRect(0, 0, rw, rh), &brush);
  59. // 读取图片的宽和高
  60. int iw = img.GetWidth();
  61. int ih = img.GetHeight();
  62. // 图片左上角在图片控件中的坐标
  63. int tx = 0;
  64. int ty = 0;
  65. // 图片绘制时的宽和高
  66. int nw = iw;
  67. int nh = ih;
  68. if (iw <= rw && ih <= rh)
  69. {
  70. float scale_x = (float) rw / iw;
  71. float scale_y = (float) rh / ih;
  72. float scale = scale_x - scale_y >= 1e-6? scale_y : scale_x;
  73. nw = (int)(iw * scale);
  74. nh = (int)(ih * scale);
  75. tx = (scale_x-scale_y >= 1e-6) ? (int)(rw-nw)/2 : 0;
  76. ty = (scale_x-scale_y >= 1e-6) ? 0 : (int)(rw-nw)/2;
  77. }
  78. else
  79. {
  80. // 计算横向和纵向缩放因子
  81. float scale_x = (float)iw / rw;
  82. float scale_y = (float)ih / rh;
  83. float scale;
  84. if (scale_x-scale_y >= 1e-6)
  85. scale = scale_x;
  86. else
  87. scale = scale_y;
  88. // 缩放后图片的宽和高
  89. nw = (int)(iw/scale);
  90. nh = (int)(ih/scale);
  91. // 缩放后的图片在m_Image的正中部位绘制,计算图片左上角在m_Image中的坐标
  92. tx = (scale_x-scale_y >= 1e-6) ? 0 : (int)(rw-nw)/2;
  93. ty = (scale_x-scale_y >= 1e-6) ? (int)(rh-nh)/2 : 0;
  94. }
  95. SetRect(&rect, tx, ty, tx+nw, ty+nh);
  96. // 将图片绘制到显示控件的指定区域
  97. SetStretchBltMode(hDC, HALFTONE); // 保持原有图片质量
  98. SetBrushOrgEx(hDC, 0, 0, NULL); // 设置刷子的起始点
  99. img.Draw(hDC,rect);
  100. brush.DeleteObject();
  101. //ReleaseDC(pDC);
  102. m_Image.Destroy(); //主动释放位图资源 相当于对Detach返回的结果执行DeleteObject
  103. pDC = GetDlgItem(ID)->GetDC();
  104. GetDlgItem(ID)->GetClientRect(&rect);
  105. pDC->BitBlt(0, 0, rect.Width(), rect.Height(), m_pMemDC, 0, 0, SRCCOPY);
  106. //DeleteObject(m_pMemBitmap);
  107. ReleaseDC(pDC);
  108. }
  109. BEGIN_MESSAGE_MAP(CImgPlayerDlg, CDialogEx)
  110. ON_WM_PAINT()
  111. ON_WM_QUERYDRAGICON()
  112. ON_WM_SIZE()
  113. ON_WM_CLOSE()
  114. ON_WM_TIMER()
  115. ON_WM_ERASEBKGND()
  116. END_MESSAGE_MAP()
  117. // CImgPlayerDlg 消息处理程序
  118. BOOL CImgPlayerDlg::OnInitDialog()
  119. {
  120. CDialogEx::OnInitDialog();
  121. // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
  122. // 执行此操作
  123. SetIcon(m_hIcon, TRUE); // 设置大图标
  124. SetIcon(m_hIcon, FALSE); // 设置小图标
  125. long ScreenWidth = GetSystemMetrics(SM_CXSCREEN); //主屏幕宽度
  126. long ScreenHeight = GetSystemMetrics(SM_CYSCREEN); //主屏幕高度
  127. MONITORINFOEX mix;
  128. mix.cbSize = sizeof(mix);
  129. if (m_pConfig->bPrimMonitor == false) // 默认情况,在副屏显示
  130. {
  131. POINT pt;
  132. pt.x = ScreenWidth+5;
  133. pt.y = 5;
  134. m_hMonitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL); //尝试获取副屏的句柄
  135. if (m_hMonitor != NULL)
  136. {
  137. GetMonitorInfo(m_hMonitor, (LPMONITORINFO)&mix);
  138. SetRect(&m_monitorRect, mix.rcMonitor.left, mix.rcMonitor.top, mix.rcMonitor.right, mix.rcMonitor.bottom);
  139. }
  140. else
  141. {
  142. OnCancel();
  143. return FALSE;
  144. }
  145. }
  146. else // 在主屏显示
  147. {
  148. m_hMonitor = MonitorFromWindow(this->GetSafeHwnd(), MONITOR_DEFAULTTONULL);
  149. GetMonitorInfo(m_hMonitor, (LPMONITORINFO)&mix);
  150. SetRect(&m_monitorRect, mix.rcMonitor.left, mix.rcMonitor.top, mix.rcMonitor.right, mix.rcMonitor.bottom);
  151. }
  152. // TODO: 在此添加额外的初始化代码
  153. m_bInit = true;
  154. // 去掉标题栏和边框
  155. ModifyStyle(WS_CAPTION, 0, 0);
  156. ModifyStyleEx(WS_EX_DLGMODALFRAME, 0, 0);
  157. if (m_pConfig->bFullScreen == true)
  158. {
  159. //ShowWindow(SW_MAXIMIZE); // 设置窗口最大化
  160. ::SetWindowPos(this->GetSafeHwnd(),HWND_TOP,m_monitorRect.left,m_monitorRect.top,
  161. m_monitorRect.Width(),m_monitorRect.Height(),SWP_SHOWWINDOW); //设置窗口的位置和大小
  162. }
  163. else
  164. {
  165. if (m_pConfig->nWndWidth > m_monitorRect.Width())
  166. {
  167. m_pConfig->nWndWidth = m_monitorRect.Width();
  168. }
  169. if (m_pConfig->nWndHeight > m_monitorRect.Height())
  170. {
  171. m_pConfig->nWndHeight = m_monitorRect.Height();
  172. }
  173. ::SetWindowPos(this->GetSafeHwnd(),HWND_TOP,m_monitorRect.left+m_pConfig->nWndX,m_monitorRect.top+m_pConfig->nWndY,
  174. m_pConfig->nWndWidth,m_pConfig->nWndHeight,SWP_SHOWWINDOW); //设置窗口的位置和大小
  175. }
  176. CRect rect;
  177. this->GetClientRect(&rect);
  178. //使PictureControl填充整个窗口,超出窗口并增加宽高各1个像素,以隐藏PictureControl的边框(注意控件是相对于窗口的)
  179. ::SetWindowPos(GetDlgItem(IDC_ShowImg)->GetSafeHwnd(),HWND_TOP,-1,-1,
  180. rect.Width()+2,rect.Height()+2,SWP_SHOWWINDOW);
  181. ::GetWindowRect(GetDlgItem(IDC_ShowImg)->GetSafeHwnd(), &rect); // 计算PictureControl的屏幕坐标
  182. //this->ScreenToClient(&rect); // 转换到相对于Dialog客户区的坐标
  183. //int borderWidth = -rect.left; // 计算边框的厚度
  184. // 双缓冲机制
  185. int x = rect.Width();
  186. int y = rect.Height();
  187. CDC *pDC = GetDlgItem(IDC_ShowImg)->GetDC();
  188. m_pMemDC->CreateCompatibleDC(pDC); // 依附图片控件DC创建兼容内存DC
  189. m_pMemBitmap->CreateCompatibleBitmap(pDC, x, y); // 创建兼容位图
  190. m_pOldBitmap = m_pMemDC->SelectObject(m_pMemBitmap); // 将位图选进内存DC,原位图保存到m_pOldBitmap
  191. ReleaseDC(pDC);
  192. DeleteObject(m_pOldBitmap);
  193. // 加载并显示第一张图片
  194. if (m_pConfig->nFileCnt > 0)
  195. {
  196. CString imgPath;
  197. imgPath.Format(_T("%s%s"),m_pConfig->strRootPath,m_pConfig->strFileNames[0]);
  198. // 判断文件是否存在
  199. if (_access(LPCTSTR(imgPath), 0) != -1)
  200. {
  201. HRESULT hRet = m_Image.Load(LPCTSTR(imgPath));
  202. if (S_OK == hRet){
  203. ShowImage(m_Image, IDC_ShowImg);
  204. }
  205. }
  206. }
  207. // 每m_nPlayInterval毫秒显示下一张图片
  208. if (m_pConfig->nFileCnt > 1)
  209. {
  210. if (m_pConfig->nPlayInterval <= 1000)
  211. {
  212. m_pConfig->nPlayInterval = 1000;
  213. }
  214. SetTimer(TIMERID, m_pConfig->nPlayInterval, 0);
  215. m_bTimer = true;
  216. }
  217. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  218. }
  219. // 如果向对话框添加最小化按钮,则需要下面的代码
  220. // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
  221. // 这将由框架自动完成。
  222. void CImgPlayerDlg::OnPaint()
  223. {
  224. if (IsIconic())
  225. {
  226. CPaintDC dc(this); // 用于绘制的设备上下文
  227. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  228. // 使图标在工作区矩形中居中
  229. int cxIcon = GetSystemMetrics(SM_CXICON);
  230. int cyIcon = GetSystemMetrics(SM_CYICON);
  231. CRect rect;
  232. GetClientRect(&rect);
  233. int x = (rect.Width() - cxIcon + 1) / 2;
  234. int y = (rect.Height() - cyIcon + 1) / 2;
  235. // 绘制图标
  236. dc.DrawIcon(x, y, m_hIcon);
  237. }
  238. else
  239. {
  240. CDialogEx::OnPaint();
  241. }
  242. }
  243. //当用户拖动最小化窗口时系统调用此函数取得光标
  244. //显示。
  245. HCURSOR CImgPlayerDlg::OnQueryDragIcon()
  246. {
  247. return static_cast<HCURSOR>(m_hIcon);
  248. }
  249. void CImgPlayerDlg::OnSize(UINT nType, int cx, int cy)
  250. {
  251. if (m_bInit == FALSE)
  252. return;
  253. CDialogEx::OnSize(nType, cx, cy);
  254. // TODO: 在此处添加消息处理程序代码
  255. GetDlgItem(IDC_ShowImg)->MoveWindow(0, 0, cx, cy);
  256. }
  257. void CImgPlayerDlg::OnClose()
  258. {
  259. // TODO: 在此添加消息处理程序代码和/或调用默认值
  260. CDialogEx::OnClose();
  261. EndPlay();
  262. }
  263. void CImgPlayerDlg::OnTimer(UINT_PTR nIDEvent)
  264. {
  265. // TODO: 在此添加消息处理程序代码和/或调用默认值
  266. if (nIDEvent == TIMERID)
  267. {
  268. m_nPicInx = (m_nPicInx+1)%m_pConfig->nFileCnt;
  269. if (m_pConfig->nPlayCnt != 0) // 非循环播放
  270. {
  271. if (m_nPicInx == 0)
  272. {
  273. m_nPlayCnt++;
  274. }
  275. if (m_nPlayCnt == m_pConfig->nPlayCnt)//播放次数到了
  276. {
  277. // 结束播放
  278. EndPlay();
  279. return;
  280. }
  281. }
  282. CString imgPath;
  283. imgPath.Format(_T("%s%s"),m_pConfig->strRootPath,m_pConfig->strFileNames[m_nPicInx]);
  284. // 判断文件是否存在
  285. if (_access((LPCTSTR)imgPath, 0) != -1)
  286. {
  287. HRESULT hRet = m_Image.Load(LPCTSTR(imgPath));
  288. if (S_OK == hRet){
  289. ShowImage(m_Image, IDC_ShowImg);
  290. }
  291. }
  292. }
  293. CDialogEx::OnTimer(nIDEvent);
  294. }
  295. // 屏蔽背景刷新
  296. BOOL CImgPlayerDlg::OnEraseBkgnd(CDC* pDC)
  297. {
  298. // TODO: 在此添加消息处理程序代码和/或调用默认值
  299. return TRUE;
  300. //return CDialogEx::OnEraseBkgnd(pDC);
  301. }
  302. BOOL CImgPlayerDlg::EndPlay()
  303. {
  304. // 停止定时器
  305. if (m_bTimer)
  306. {
  307. KillTimer(TIMERID);
  308. m_bTimer = false;
  309. // 释放绘图对象
  310. DeleteObject(m_pMemBitmap);
  311. ReleaseDC(m_pMemDC);
  312. }
  313. // 关闭窗口
  314. EndDialog(IDCANCEL);
  315. return TRUE;
  316. }