Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
8.0 KiB

  1. // BMPInfoDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "BMPInfo.h"
  5. #include "BMPInfoDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CBMPInfoDlg dialog
  13. CBMPInfoDlg::CBMPInfoDlg(CWnd* pParent /*=NULL*/)
  14. : CDialog(CBMPInfoDlg::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CBMPInfoDlg)
  17. m_FileType = 0;
  18. m_FileSize = 0;
  19. m_Reserved1 = 0;
  20. m_Reserved2 = 0;
  21. m_OffBits = 0;
  22. m_BitmapHeaderSize = 0;
  23. m_BitmapWidth = 0;
  24. m_BitmapHeight = 0;
  25. m_BitmapPlanes = 0;
  26. m_BitmapBitCount = 0;
  27. m_BitmapCompression = 0;
  28. m_BitmapImageSize = 0;
  29. m_BitmapXPelsPerMeter = 0;
  30. m_BitmapYPelsPerMeter = 0;
  31. m_BitmapClrUsed = 0;
  32. m_BitmapClrImportant = 0;
  33. m_bManipulateImage = FALSE;
  34. //}}AFX_DATA_INIT
  35. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  36. }
  37. void CBMPInfoDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CBMPInfoDlg)
  41. DDX_Text(pDX, IDC_FILE_TYPE, m_FileType);
  42. DDX_Text(pDX, IDC_FILE_SIZE, m_FileSize);
  43. DDX_Text(pDX, IDC_FILE_RESERVED1, m_Reserved1);
  44. DDX_Text(pDX, IDC_FILE_RESERVED2, m_Reserved2);
  45. DDX_Text(pDX, IDC_FILE_OFFBITS, m_OffBits);
  46. DDX_Text(pDX, IDC_BITMAP_HEADERSIZE, m_BitmapHeaderSize);
  47. DDX_Text(pDX, IDC_BITMAP_WIDTH, m_BitmapWidth);
  48. DDX_Text(pDX, IDC_BITMAP_HEIGHT, m_BitmapHeight);
  49. DDX_Text(pDX, IDC_BITMAP_PLANES, m_BitmapPlanes);
  50. DDX_Text(pDX, IDC_BITMAP_BITCOUNT, m_BitmapBitCount);
  51. DDX_Text(pDX, IDC_BITMAP_COMPRESSION, m_BitmapCompression);
  52. DDX_Text(pDX, IDC_BITMAP_IMAGESIZE, m_BitmapImageSize);
  53. DDX_Text(pDX, IDC_BITMAP_XPELSPERMETER, m_BitmapXPelsPerMeter);
  54. DDX_Text(pDX, IDC_BITMAP_YPELSPERMETER, m_BitmapYPelsPerMeter);
  55. DDX_Text(pDX, IDC_BITMAP_CLRUSED, m_BitmapClrUsed);
  56. DDX_Text(pDX, IDC_BITMAP_CLRIMPORTANT, m_BitmapClrImportant);
  57. DDX_Check(pDX, IDC_IMAGE_MANIPULATION_CHECKBOX, m_bManipulateImage);
  58. //}}AFX_DATA_MAP
  59. }
  60. BEGIN_MESSAGE_MAP(CBMPInfoDlg, CDialog)
  61. //{{AFX_MSG_MAP(CBMPInfoDlg)
  62. ON_WM_PAINT()
  63. ON_WM_QUERYDRAGICON()
  64. ON_BN_CLICKED(IDC_IMAGE_MANIPULATION_CHECKBOX, OnImageManipulationCheckbox)
  65. //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CBMPInfoDlg message handlers
  69. BOOL CBMPInfoDlg::OnInitDialog()
  70. {
  71. CDialog::OnInitDialog();
  72. SetIcon(m_hIcon, TRUE); // Set big icon
  73. SetIcon(m_hIcon, FALSE); // Set small icon
  74. return TRUE; // return TRUE unless you set the focus to a control
  75. }
  76. void CBMPInfoDlg::OnPaint()
  77. {
  78. if (IsIconic())
  79. {
  80. CPaintDC dc(this); // device context for painting
  81. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  82. // Center icon in client rectangle
  83. int cxIcon = GetSystemMetrics(SM_CXICON);
  84. int cyIcon = GetSystemMetrics(SM_CYICON);
  85. CRect rect;
  86. GetClientRect(&rect);
  87. int x = (rect.Width() - cxIcon + 1) / 2;
  88. int y = (rect.Height() - cyIcon + 1) / 2;
  89. // Draw the icon
  90. dc.DrawIcon(x, y, m_hIcon);
  91. }
  92. else
  93. {
  94. CDialog::OnPaint();
  95. }
  96. }
  97. HCURSOR CBMPInfoDlg::OnQueryDragIcon()
  98. {
  99. return (HCURSOR) m_hIcon;
  100. }
  101. LRESULT CBMPInfoDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  102. {
  103. if (message == WM_DROPFILES) {
  104. //
  105. // process the dropped file request
  106. //
  107. UINT result = 0; // error result
  108. UINT cch = 0; // size of the characters in buffer
  109. LPTSTR lpszFile = NULL; // buffer
  110. UINT iFile = 0;
  111. //
  112. // call once to get information on the DROP
  113. //
  114. result = DragQueryFile((HDROP) wParam,iFile, NULL, cch);
  115. //
  116. // alloc the needed mem for the operation
  117. //
  118. if (result > 0) {
  119. lpszFile = (LPTSTR)GlobalAlloc(GPTR,result + 1);
  120. if (lpszFile) {
  121. //
  122. // call again to fill the file information
  123. //
  124. cch = (result + 1); // assign the character count here
  125. result = DragQueryFile((HDROP) wParam,iFile,lpszFile,cch);
  126. m_BitmapFileName = lpszFile;
  127. GlobalUnlock(lpszFile);
  128. GlobalFree(lpszFile);
  129. UpdateFileInformation();
  130. }
  131. } else {
  132. AfxMessageBox("Could not allocate enough Memory for Bitmap File Name");
  133. }
  134. }
  135. return CDialog::WindowProc(message, wParam, lParam);
  136. }
  137. void CBMPInfoDlg::UpdateFileInformation()
  138. {
  139. BITMAPFILEHEADER bmfh;
  140. ZeroMemory(&bmfh, sizeof(BITMAPFILEHEADER));
  141. BITMAPINFOHEADER bmih;
  142. ZeroMemory(&bmih, sizeof(BITMAPINFOHEADER));
  143. CFile ImageFile;
  144. CFileException Exception;
  145. //
  146. // attempt to open the image file
  147. //
  148. if (!ImageFile.Open(m_BitmapFileName,CFile::modeRead,&Exception))
  149. AfxThrowFileException(Exception.m_cause);
  150. else {
  151. //
  152. // read the image file header and check if it's valid.
  153. //
  154. if (ImageFile.Read((LPSTR)&bmfh, sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER))
  155. AfxMessageBox("Error reading BITMAPFILEHEADER");
  156. else {
  157. //
  158. // read the image info header and check if it's valid.
  159. //
  160. if (ImageFile.Read((LPSTR)&bmih, sizeof(BITMAPINFOHEADER)) != sizeof(BITMAPINFOHEADER))
  161. AfxMessageBox("Error reading BITMAPINFOHEADER");
  162. else {
  163. //
  164. // update file header members here
  165. //
  166. if (bmfh.bfType != BMP_HEADER_MARKER)
  167. AfxMessageBox("invalid BMP file format detected");
  168. else {
  169. m_FileType = bmfh.bfType;
  170. m_FileSize = bmfh.bfSize;
  171. m_Reserved1 = bmfh.bfReserved1;
  172. m_Reserved2 = bmfh.bfReserved2;
  173. m_OffBits = bmfh.bfOffBits;
  174. //
  175. // update image info header members here
  176. //
  177. m_BitmapHeaderSize = bmih.biSize;
  178. m_BitmapWidth = bmih.biWidth;
  179. m_BitmapHeight = bmih.biHeight;
  180. m_BitmapPlanes = bmih.biPlanes;
  181. m_BitmapBitCount = bmih.biBitCount;
  182. m_BitmapCompression = bmih.biCompression;
  183. m_BitmapImageSize = bmih.biSizeImage;
  184. m_BitmapXPelsPerMeter = bmih.biXPelsPerMeter;
  185. m_BitmapYPelsPerMeter = bmih.biYPelsPerMeter;
  186. m_BitmapClrUsed = bmih.biClrUsed;
  187. m_BitmapClrImportant = bmih.biClrImportant;
  188. //
  189. // display the current filename in the window header
  190. //
  191. SetWindowText(RipFileName());
  192. }
  193. if (m_bManipulateImage) {
  194. ManipulateImage(&ImageFile);
  195. }
  196. UpdateData(FALSE);
  197. }
  198. }
  199. //
  200. // close the image file
  201. //
  202. ImageFile.Close();
  203. }
  204. }
  205. CString CBMPInfoDlg::RipFileName()
  206. {
  207. int index = 0;
  208. CString RippedFile = "";
  209. index = m_BitmapFileName.ReverseFind('\\');
  210. index ++;
  211. while (index <= (m_BitmapFileName.GetLength()-1)) {
  212. RippedFile = RippedFile + m_BitmapFileName[index];
  213. index++;
  214. }
  215. return (RippedFile);
  216. }
  217. void CBMPInfoDlg::OnImageManipulationCheckbox()
  218. {
  219. if(m_bManipulateImage)
  220. m_bManipulateImage = FALSE;
  221. else
  222. m_bManipulateImage = TRUE;
  223. }
  224. void CBMPInfoDlg::ManipulateImage(CFile *pImageFile)
  225. {
  226. AfxMessageBox("There are no image manipulation routines written yet... sorry. :) ");
  227. }