Main.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //---------------------------------------------------------------------------------------------
  2. //---------------------------------------------------------------------------------------------
  3. //
  4. // MNGView Sample Application for VC6:
  5. // Loads all MNG/JNG/PNG Files LibMNG can do
  6. // Can save a single Frame to PNG Format.
  7. //
  8. // This code is public domain.
  9. // Created by Nikolaus Brennig, November 14th, 2000.
  10. // [email protected]
  11. // http://cust.nol.at/ppee
  12. //
  13. // Tab: 4
  14. //
  15. //---------------------------------------------------------------------------------------------
  16. //---------------------------------------------------------------------------------------------
  17. #define WIN32_LEAN_AND_MEAN
  18. #include "Main.h"
  19. //---------------------------------------------------------------------------------------------
  20. // Libs (its up to you to make VC find the libs; set the paths in the options):
  21. //---------------------------------------------------------------------------------------------
  22. #pragma comment(lib,"comctl32.lib")
  23. #pragma comment(lib,"libmng.lib")
  24. #pragma comment(lib,"libjpeg.lib")
  25. #pragma comment(lib,"libz.lib")
  26. #pragma comment(lib,"lcmsstat.lib")
  27. //---------------------------------------------------------------------------------------------
  28. // Vars:
  29. //---------------------------------------------------------------------------------------------
  30. HWND hPicWin;
  31. HINSTANCE hinst;
  32. HMENU hMenu;
  33. HDC MemDC, hdc;
  34. HBITMAP MemImage, DefaultMemImage;
  35. ANIMFILE AnimFile;
  36. RECT rcRect;
  37. OPENFILENAME ofn, sfn;
  38. int W, H, Bits;
  39. int dx, dy;
  40. char OFNFile[1024];
  41. char CurDir[1024];
  42. //---------------------------------------------------------------------------------------------
  43. // Loads a file:
  44. //---------------------------------------------------------------------------------------------
  45. VOID LoadOFN( HWND hwnd )
  46. {
  47. GetCurrentDirectory( sizeof(CurDir), CurDir );
  48. ofn.lStructSize = sizeof(OPENFILENAME);
  49. ofn.hwndOwner = hwnd;
  50. ofn.lpstrFile = OFNFile;
  51. ofn.nMaxFile = sizeof(OFNFile);
  52. ofn.lpstrFilter = "Network Graphics (*.mng;*.jng:*.png)\0*.mng;*.jng;*.png\0";
  53. ofn.nFilterIndex = 1;
  54. ofn.lpstrFileTitle = NULL;
  55. ofn.nMaxFileTitle = NULL;
  56. ofn.lpstrInitialDir = CurDir;
  57. ofn.lpstrTitle = "Load:";
  58. ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST |
  59. OFN_HIDEREADONLY | OFN_NONETWORKBUTTON;
  60. // Display the Open dialog box.
  61. if( GetOpenFileName(&ofn) )
  62. {
  63. dx = dy = W = H = 0;
  64. if( AnimFile.isAnimation == 1 ) CleanUpMNG();
  65. LoadMNG( OFNFile, hwnd, hdc );
  66. }
  67. }
  68. //---------------------------------------------------------------------------------------------
  69. // Saves a file:
  70. //---------------------------------------------------------------------------------------------
  71. VOID SaveSFN( HWND hwnd )
  72. {
  73. GetCurrentDirectory( sizeof(CurDir), CurDir );
  74. // Initialize OPENFILENAME
  75. sfn.lStructSize = sizeof(OPENFILENAME);
  76. sfn.hwndOwner = hwnd;
  77. sfn.lpstrFile = OFNFile;
  78. sfn.nMaxFile = sizeof(OFNFile);
  79. sfn.lpstrFilter = "PNG\0*.png\0";
  80. sfn.nFilterIndex = 1;
  81. sfn.lpstrFileTitle = 0;
  82. sfn.nMaxFileTitle = 0;
  83. sfn.lpstrInitialDir = CurDir;
  84. sfn.lpstrTitle = "Save an Image";
  85. sfn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_NONETWORKBUTTON;
  86. // Display the Open dialog box.
  87. if( GetSaveFileName(&sfn) )
  88. {
  89. SaveMNG( OFNFile, MemDC, MemImage );
  90. }
  91. }
  92. //---------------------------------------------------------------------------------------------
  93. // For stringhandling...
  94. //---------------------------------------------------------------------------------------------
  95. VOID catpath( char *dst, const char *src )
  96. {
  97. int len = lstrlen(dst);
  98. if( len > 0 && (dst[len-1] != '\\' && dst[len-1] != '/') ) lstrcat( dst, "\\" );
  99. lstrcat( dst, src );
  100. }
  101. //---------------------------------------------------------------------------------------------
  102. // MainWindow WindowProc
  103. //---------------------------------------------------------------------------------------------
  104. long WINAPI WindowProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
  105. {
  106. switch( message )
  107. {
  108. case WM_TIMER:
  109. {
  110. int wTimerID = wParam;
  111. if( AnimFile.isAnimation == 1 && wTimerID == 2 )
  112. {
  113. UpdateMNG();
  114. SendMessage( hPicWin, WM_PAINT, 0, 0 );
  115. }
  116. }
  117. break;
  118. case WM_COMMAND:
  119. switch( LOWORD(wParam) )
  120. {
  121. case FILE_OPEN:
  122. LoadOFN(hwnd);
  123. break;
  124. case FILE_SAVE:
  125. SaveSFN(hwnd);
  126. break;
  127. case FILE_EXIT:
  128. DestroyWindow( hPicWin );
  129. break;
  130. case HELP_ABOUT:
  131. Warning(
  132. "MNGView Sample Application for VC6.\n" \
  133. "This Code is Public Domain.\n" \
  134. "Created by Nikolaus Brennig."
  135. );
  136. break;
  137. }
  138. break;
  139. case WM_ERASEBKGND:
  140. return 0L;
  141. case WM_PAINT:
  142. // GetDC:
  143. GetClientRect( hPicWin, &rcRect );
  144. hdc = GetDC( hPicWin );
  145. if( MemDC == 0 )
  146. {
  147. BitBlt( hdc, 0, 0, rcRect.right, rcRect.bottom, MemDC, 0, 0, BLACKNESS );
  148. ReleaseDC( hPicWin, hdc );
  149. break;
  150. }
  151. // Erase:
  152. // Upper area...
  153. BitBlt( hdc, 0, 0, rcRect.right, (0+dy), MemDC, 0, 0, BLACKNESS );
  154. // Lower area...
  155. BitBlt( hdc, 0, (0+dy)+H, rcRect.right, rcRect.bottom - ((0+dy)+H), MemDC, 0, 0, BLACKNESS );
  156. // Left area...
  157. BitBlt( hdc, 0, 0, (0+dx), rcRect.bottom, MemDC, 0, 0, BLACKNESS );
  158. // Right area...
  159. BitBlt( hdc, (0+dx)+W, 0, rcRect.right, rcRect.bottom, MemDC, 0, 0, BLACKNESS );
  160. // Show Imageframe:
  161. BitBlt( hdc, dx, dy, W, H, MemDC, 0, 0, SRCCOPY );
  162. // Release DC...
  163. ReleaseDC( hPicWin, hdc );
  164. break;
  165. case WM_HSCROLL:
  166. {
  167. int nScrollCode = (int) LOWORD(wParam); // scroll bar value
  168. if( nScrollCode == SB_LINELEFT ) dx += 10;
  169. if( nScrollCode == SB_LINERIGHT ) dx -= 10;
  170. SendMessage( hwnd, WM_PAINT, 0, 0 );
  171. }
  172. break;
  173. case WM_VSCROLL:
  174. {
  175. int nScrollCode = (int) LOWORD(wParam); // scroll bar value
  176. if( nScrollCode == SB_LINEUP ) dy += 10;
  177. if( nScrollCode == SB_LINEDOWN ) dy -= 10;
  178. SendMessage( hwnd, WM_PAINT, 0, 0 );
  179. }
  180. break;
  181. case WM_GETMINMAXINFO:
  182. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 550;
  183. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 450;
  184. return 0L;
  185. case WM_DESTROY:
  186. if( AnimFile.isAnimation == 1 )
  187. CleanUpMNG();
  188. if( hMenu ) DestroyMenu( hMenu );
  189. PostQuitMessage(0);
  190. return 0L;
  191. }
  192. return DefWindowProc( hwnd, message, wParam, lParam );
  193. }
  194. //---------------------------------------------------------------------------------------------
  195. // ok, initen wir mal das Window mit den Styleparametern...
  196. //---------------------------------------------------------------------------------------------
  197. BOOL InitApplication( HINSTANCE hInstance, int nCmdShow, LPSTR lpCommandLine )
  198. {
  199. WNDCLASSEX wcex;
  200. ZeroMemory( &wcex, sizeof(wcex) );
  201. wcex.cbSize = sizeof(wcex);
  202. wcex.style = 0;
  203. wcex.lpfnWndProc = WindowProc;
  204. wcex.cbClsExtra = 0;
  205. wcex.cbWndExtra = 0;
  206. wcex.hInstance = hInstance;
  207. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(APPICON));
  208. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  209. wcex.hbrBackground = HBRUSH(GetStockObject(BLACK_BRUSH));
  210. wcex.lpszMenuName = 0;
  211. wcex.lpszClassName = "MNGViewClass";
  212. wcex.hIconSm = 0;
  213. if( !RegisterClassEx(&wcex) )
  214. return Error( "RegisterClass failed!" );
  215. // Init:
  216. W = 0;
  217. H = 0;
  218. AnimFile.isAnimation = 0;
  219. MemDC = 0;
  220. MemImage = 0;
  221. DefaultMemImage = 0;
  222. // Create the Window:
  223. hPicWin = CreateWindowEx(
  224. 0,
  225. "MNGViewClass",
  226. TITLE,
  227. WS_OVERLAPPEDWINDOW|WS_SYSMENU|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_HSCROLL|WS_VSCROLL,
  228. CW_USEDEFAULT,
  229. CW_USEDEFAULT,
  230. CW_USEDEFAULT,
  231. CW_USEDEFAULT,
  232. NULL, NULL,
  233. hInstance,
  234. NULL
  235. );
  236. if( !hPicWin )
  237. return Error( "Couldn't create Window!" );
  238. // Load our menu...
  239. hMenu = LoadMenu( GetModuleHandle(NULL), MAKEINTRESOURCE(THEMENU) );
  240. if( hMenu == 0 ) Warning( "Unable to load Menu!" );
  241. SetMenu( hPicWin, hMenu );
  242. InitCommonControls();
  243. UpdateWindow(hPicWin);
  244. ShowWindow(hPicWin, SW_NORMAL);
  245. return true;
  246. }
  247. //---------------------------------------------------------------------------------------------
  248. // Ok. Das ist die Startfunktion, die wird als erstes von Windows augerufen...
  249. //---------------------------------------------------------------------------------------------
  250. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  251. {
  252. MSG msg;
  253. hinst = hInstance;
  254. if( !InitApplication(hInstance, nCmdShow, lpCmdLine) )
  255. return -1;
  256. while( GetMessage(&msg, NULL, 0, 0) )
  257. {
  258. TranslateMessage(&msg);
  259. DispatchMessage(&msg);
  260. }
  261. return msg.wParam;
  262. }
  263. //---------------------------------------------------------------------------------------------
  264. // Error
  265. //---------------------------------------------------------------------------------------------
  266. BOOL Error( const char *err )
  267. {
  268. MessageBox( hPicWin, err, TITLE, MB_ICONHAND+MB_OK );
  269. DestroyWindow( hPicWin );
  270. return FALSE;
  271. }
  272. //---------------------------------------------------------------------------------------------
  273. // Warning
  274. //---------------------------------------------------------------------------------------------
  275. BOOL Warning( const char *err )
  276. {
  277. MessageBox( hPicWin, err, TITLE, MB_ICONHAND+MB_OK );
  278. return FALSE;
  279. }