DEMO.CPP
上传用户:wep9318
上传日期:2007-01-07
资源大小:893k
文件大小:8k
源码类别:

图片显示

开发平台:

Visual C++

  1. // demo.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "demo.h"
  5. #include "MainFrm.h"
  6. #include "ChildFrm.h"
  7. #include "demoDoc.h"
  8. #include "demoView.h"
  9. #include "cimage.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. #define NUM_DOC_TYPES 4
  16. DocType doctypes[NUM_DOC_TYPES];
  17. /* why doesn't this compile?
  18.  = 
  19. {
  20. { CIMAGE_FORMAT_GIF, TRUE, FALSE, "GIF files", "gif" },
  21. { CIMAGE_FORMAT_JPEG, TRUE, TRUE, "JPEG files", "jpg" },
  22. { CIMAGE_FORMAT_PNG, TRUE, TRUE, "PNG files", "png" },
  23. { CIMAGE_FORMAT_BMP, TRUE, TRUE, "BMP files", "bmp" }
  24. };
  25. */
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CDemoApp
  28. BEGIN_MESSAGE_MAP(CDemoApp, CWinApp)
  29. //{{AFX_MSG_MAP(CDemoApp)
  30. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  31. ON_COMMAND(ID_IMAGE_FROM_BITMAP, OnImageFromBitmap)
  32. //}}AFX_MSG_MAP
  33. // Standard file based document commands
  34. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  35. ON_COMMAND(ID_FILE_OPEN, CDemoApp::OnFileOpen)
  36. // Standard print setup command
  37. ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CDemoApp construction
  41. CDemoApp::CDemoApp()
  42. {
  43. m_nFilterIndex = 1;
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. // The one and only CDemoApp object
  47. CDemoApp theApp;
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CDemoApp initialization
  50. BOOL CDemoApp::InitInstance()
  51. {
  52. // Standard initialization
  53. // If you are not using these features and wish to reduce the size
  54. //  of your final executable, you should remove from the following
  55. //  the specific initialization routines you do not need.
  56. doctypes[0].nID = CIMAGE_FORMAT_GIF;
  57. doctypes[0].bRead = TRUE;
  58. doctypes[0].bWrite = FALSE;
  59. doctypes[0].description = "GIF files";
  60. doctypes[0].ext ="*.gif";
  61. doctypes[1].nID = CIMAGE_FORMAT_JPEG;
  62. doctypes[1].bRead = TRUE;
  63. doctypes[1].bWrite = TRUE;
  64. doctypes[1].description = "JPEG files";
  65. doctypes[1].ext ="*.jpg";
  66. doctypes[2].nID = CIMAGE_FORMAT_PNG;
  67. doctypes[2].bRead = TRUE;
  68. doctypes[2].bWrite = TRUE;
  69. doctypes[2].description = "PNG files";
  70. doctypes[2].ext ="*.png";
  71. doctypes[3].nID = CIMAGE_FORMAT_BMP;
  72. doctypes[3].bRead = TRUE;
  73. doctypes[3].bWrite = TRUE;
  74. doctypes[3].description = "BMP files";
  75. doctypes[3].ext ="*.bmp";
  76. #ifdef _AFXDLL
  77. Enable3dControls(); // Call this when using MFC in a shared DLL
  78. #else
  79. Enable3dControlsStatic(); // Call this when linking to MFC statically
  80. #endif
  81. LoadStdProfileSettings(9);  // Load standard INI file options (including MRU)
  82. // Register the application's document templates.  Document templates
  83. //  serve as the connection between documents, frame windows and views.
  84. CMultiDocTemplate* pDocTemplate;
  85. pDocTemplate = new CMultiDocTemplate(
  86. IDR_DEMOTYPE,
  87. RUNTIME_CLASS(CDemoDoc),
  88. RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  89. RUNTIME_CLASS(CDemoView));
  90. demoTemplate = pDocTemplate;
  91. AddDocTemplate(pDocTemplate);
  92. // create main MDI Frame window
  93. CMainFrame* pMainFrame = new CMainFrame;
  94. if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  95. return FALSE;
  96. m_pMainWnd = pMainFrame;
  97. // Parse command line for standard shell commands, DDE, file open
  98. CCommandLineInfo cmdInfo;
  99.     // Alter behaviour to not open window immediately
  100.     cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
  101. ParseCommandLine(cmdInfo);
  102. // Dispatch commands specified on the command line
  103. if (!ProcessShellCommand(cmdInfo))
  104. return FALSE;
  105. // The main window has been initialized, so show and update it.
  106. pMainFrame->ShowWindow(m_nCmdShow);
  107. pMainFrame->UpdateWindow();
  108. return TRUE;
  109. }
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CAboutDlg dialog used for App About
  112. class CAboutDlg : public CDialog
  113. {
  114. public:
  115. CAboutDlg();
  116. // Dialog Data
  117. //{{AFX_DATA(CAboutDlg)
  118. enum { IDD = IDD_ABOUTBOX };
  119. //}}AFX_DATA
  120. // ClassWizard generated virtual function overrides
  121. //{{AFX_VIRTUAL(CAboutDlg)
  122. protected:
  123. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  124. //}}AFX_VIRTUAL
  125. // Implementation
  126. protected:
  127. //{{AFX_MSG(CAboutDlg)
  128. // No message handlers
  129. //}}AFX_MSG
  130. DECLARE_MESSAGE_MAP()
  131. };
  132. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  133. {
  134. //{{AFX_DATA_INIT(CAboutDlg)
  135. //}}AFX_DATA_INIT
  136. }
  137. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  138. {
  139. CDialog::DoDataExchange(pDX);
  140. //{{AFX_DATA_MAP(CAboutDlg)
  141. //}}AFX_DATA_MAP
  142. }
  143. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  144. //{{AFX_MSG_MAP(CAboutDlg)
  145. // No message handlers
  146. //}}AFX_MSG_MAP
  147. END_MESSAGE_MAP()
  148. // App command to run the dialog
  149. void CDemoApp::OnAppAbout()
  150. {
  151. CAboutDlg aboutDlg;
  152. aboutDlg.DoModal();
  153. }
  154. void CDemoApp::OnFileOpen() 
  155. {
  156. // prompt the user (with all document templates)
  157. CString newName;
  158. int nType = CIMAGE_FORMAT_GIF;
  159. if (!PromptForFileName(newName, AFX_IDS_OPENFILE,
  160.   OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE, &nType))
  161. return; // open cancelled
  162. OpenDocumentFile(newName);
  163. }
  164. // prompt for file name - used for open and save as
  165. // static function called from app
  166. BOOL CDemoApp::PromptForFileName(CString& fileName, UINT nIDSTitle, 
  167. DWORD dwFlags, BOOL bOpenFileDialog, int* pType)
  168. {
  169. CFileDialog dlgFile(bOpenFileDialog);
  170. CString title("Open bitmap file");
  171. dlgFile.m_ofn.Flags |= dwFlags;
  172. // dlgFile.m_ofn.Flags &= ~OFN_SHOWHELP;
  173. int nIndex = m_nFilterIndex;
  174. if (!bOpenFileDialog)
  175. {
  176. int nDocType = (pType != NULL) ? *pType : CIMAGE_FORMAT_BMP;
  177. nIndex = GetIndexFromType(nDocType, bOpenFileDialog);
  178. if (nIndex == -1)
  179. nIndex = 1;
  180. ASSERT(nIndex != -1);
  181. nIndex++;
  182. }
  183. dlgFile.m_ofn.nFilterIndex = nIndex;
  184. // strDefExt is necessary to hold onto the memory from GetExtFromType
  185. CString strDefExt = GetExtFromType(GetTypeFromIndex(nIndex-1, bOpenFileDialog));
  186. dlgFile.m_ofn.lpstrDefExt = strDefExt;
  187. CString strFilter = GetFileTypes(bOpenFileDialog);
  188. dlgFile.m_ofn.lpstrFilter = strFilter;
  189. dlgFile.m_ofn.lpstrTitle = title;
  190. dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
  191. BOOL bRet = (dlgFile.DoModal() == IDOK) ? TRUE : FALSE;
  192. fileName.ReleaseBuffer();
  193. if (bRet)
  194. {
  195. if (bOpenFileDialog)
  196. m_nFilterIndex = dlgFile.m_ofn.nFilterIndex;
  197. if (pType != NULL)
  198. {
  199. int nIndex = (int)dlgFile.m_ofn.nFilterIndex - 1;
  200. ASSERT(nIndex >= 0);
  201. *pType = GetTypeFromIndex(nIndex, bOpenFileDialog);
  202. }
  203. }
  204. return bRet;
  205. }
  206. int CDemoApp::GetIndexFromType(int nDocType, BOOL bOpenFileDialog)
  207. {
  208. int nCnt = 0;
  209. for (int i=0;i<NUM_DOC_TYPES;i++)
  210. {
  211. if (bOpenFileDialog ? doctypes[i].bRead : doctypes[i].bWrite)
  212. {
  213. if (doctypes[i].nID == nDocType)
  214. return nCnt;
  215. nCnt++;
  216. }
  217. }
  218. return -1;
  219. }
  220. int CDemoApp::GetTypeFromIndex(int nIndex, BOOL bOpenFileDialog)
  221. {
  222. int nCnt = 0;
  223. for (int i=0;i<NUM_DOC_TYPES;i++)
  224. {
  225. if (bOpenFileDialog ? doctypes[i].bRead : doctypes[i].bWrite)
  226. {
  227. if (nCnt == nIndex)
  228. return i;
  229. nCnt++;
  230. }
  231. }
  232. ASSERT(FALSE);
  233. return -1;
  234. }
  235. CString CDemoApp::GetExtFromType(int nDocType)
  236. {
  237. for (int i=0;i<NUM_DOC_TYPES;i++)
  238. {
  239. if (doctypes[i].nID == nDocType)
  240. return doctypes[i].ext;
  241. }
  242. return CString("");
  243. }
  244. BOOL CDemoApp::GetWritableType(int nDocType)
  245. {
  246. for (int i=0;i<NUM_DOC_TYPES;i++)
  247. {
  248. if (doctypes[i].nID == nDocType)
  249. return doctypes[i].bWrite;
  250. }
  251. return FALSE;
  252. }
  253. CString CDemoApp::GetFileTypes(BOOL bOpenFileDialog)
  254. {
  255. CString str;
  256. for (int i=0;i<NUM_DOC_TYPES;i++)
  257. {
  258. if (bOpenFileDialog && doctypes[i].bRead)
  259. {
  260. str += doctypes[i].description;
  261. str += (TCHAR)NULL;
  262. str += doctypes[i].ext;
  263. str += (TCHAR)NULL;
  264. }
  265. else if (!bOpenFileDialog && doctypes[i].bWrite)
  266. {
  267. str += doctypes[i].description;
  268. str += (TCHAR)NULL;
  269. str += doctypes[i].ext;
  270. str += (TCHAR)NULL;
  271. }
  272. }
  273. return str;
  274. }
  275. /////////////////////////////////////////////////////////////////////////////
  276. // CDemoApp commands
  277. void CDemoApp::OnImageFromBitmap() 
  278. {
  279. CBitmap bitmap;
  280. if (!bitmap.LoadBitmap(IDB_TEST_BITMAP))
  281. {
  282. AfxMessageBox("Could not load bitmap from resource");
  283. return;
  284. }
  285. CDemoDoc *doc = (CDemoDoc *)demoTemplate->OpenDocumentFile(NULL);
  286. if (doc)
  287. {
  288. CImage *newImage = new CImage(&bitmap);
  289. if (!newImage->IsOK())
  290. {
  291. AfxMessageBox("Could not create CImage");
  292. delete newImage;
  293. return;
  294. }
  295. doc->image = newImage;
  296. }
  297. }