Main.c
上传用户:s6549606
上传日期:2015-11-11
资源大小:12002k
文件大小:5k
- #include <windows.h>
- #include <commdlg.h>
- #include <string.h>
- #include "jpeg.h"
- ////////////////////////////////////////////////////////////
- int PASCAL WinMain (HANDLE, HANDLE, LPSTR, int);
- LRESULT CALLBACK MainWndProc(HWND , UINT,WPARAM, LPARAM);
- extern void ScrollProc (HWND hWnd, int message, WORD wPos, WORD wScrollType);
- extern void DisProc(HWND hWnd,HGLOBAL hbi);
- //extern void SizeProc(HWND hWnd);
- extern void GetJPGSize(char *fn);
- extern void DecodeJPG(HWND ,char *,HGLOBAL *);
- extern void FitNewSize (HWND hWnd);
- void getopenname(HWND hWnd,char *openname);
- ////////////////////////////////////////////////////////////
- HINSTANCE hinst;
- HGLOBAL hbi;
- HMENU hMenu;
- int imgwidth,imgheight;
- BOOL HSCROLL,VSCROLL;
- /*------------------------- WinMain() ------------------------*/
- int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
- LPSTR lpszCmdLine, int nCmdShow)
- {
- MSG msg;
- HWND hWnd;
- WNDCLASS wndclass;
- RECT rc;
- int DesktopWidth,DesktopHeight,FrameLeft,FrameTop,FrameWidth,FrameHeight;
-
- hinst=hInstance;
- if ( ! hPrevInstance )
- {
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- wndclass.lpfnWndProc = MainWndProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hInstance;
- wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
- wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
- wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
- wndclass.lpszMenuName = "FileMenu";
- wndclass.lpszClassName = "JPEG Viewer";
- if ( ! RegisterClass (&wndclass) )
- return FALSE;
- }
- hMenu = LoadMenu(hInstance,"FileMenu");
-
- GetWindowRect(GetDesktopWindow(),&rc);
- DesktopWidth = rc.right - rc.left;
- DesktopHeight = rc.bottom - rc.top;
- FrameLeft = DesktopWidth / 10;
- FrameTop = DesktopHeight /10;
- FrameWidth = FrameLeft*8;
- FrameHeight = FrameTop*8;
-
- hWnd = CreateWindow (
- "JPEG Viewer",
- "JPEG Viewer",
- WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU,//|WS_THICKFRAME,
- FrameLeft,
- FrameTop,
- FrameWidth,
- FrameHeight,
- NULL,
- NULL,
- hInstance,
- NULL);
- if ( ! hWnd )
- return FALSE;
-
- SetMenu(hWnd,hMenu);
- ShowWindow (hWnd, nCmdShow);
- UpdateWindow (hWnd);
- while ( GetMessage (&msg, NULL, 0, 0) )
- {
- TranslateMessage (&msg);
- DispatchMessage (&msg);
- }
- return msg.wParam;
- }
- /*---------------------- WndProc() ---------------------------*/
- LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
- {
- char openname[256];
- switch (message)
- {
- case WM_CREATE:
- hbi=NULL;
- imgheight=0;
- imgwidth=0;
- HSCROLL=VSCROLL=FALSE;
- break;
- case WM_COMMAND:
- switch (wParam)
- {
- case IDM_OPEN:
- getopenname(hWnd,openname);
- if(openname[0]!=' ')
- {
- if(hbi!=NULL)
- {
- GlobalUnlock(hbi);
- GlobalFree(hbi);
- hbi=NULL;
- }
- imgheight=0;
- imgwidth=0;
- GetJPGSize(openname);
- if((imgwidth!=0)&&(imgheight!=0))
- {
- //the InvalidateRect function is useful here
- HSCROLL=VSCROLL=FALSE;
- InvalidateRect(hWnd,NULL,TRUE);
- UpdateWindow(hWnd);
- FitNewSize(hWnd);
- DecodeJPG(hWnd,openname,&hbi);
- }
- }
- break;
- case IDM_EXIT:
- SendMessage (hWnd, WM_CLOSE, 0, 0);
- return 0;
- }
- break;
- case WM_PAINT:
- DisProc(hWnd,hbi);
- break;
- case WM_HSCROLL:
- case WM_VSCROLL:
- ScrollProc (hWnd, message, LOWORD (lParam), wParam);
- break;
-
- case WM_DESTROY:
- DestroyMenu(hMenu);
- PostQuitMessage (0);
- return 0;
- default:
- break;
- }
- return DefWindowProc (hWnd, message, wParam, lParam);
- }
- /*----------------------------GetOpenName()----------------------------*/
- void getopenname(HWND hWnd,char *openname)
- {
- OPENFILENAME ofn;
- char szFile[256],szFileTitle[256];
- char szFilter[40];
- int len;
-
- strcpy(szFilter,"JPEG Image(*.jpg)|*.jpg|");
-
- len=strlen(szFilter);
- szFilter[len-1]=' ';
- szFilter[len-7]=' ';
- // set all struct members to zero
- memset(&ofn,0,sizeof(OPENFILENAME));
- //initialize the OPENFILENAME members.
- szFile[0] = ' ';
- ofn.lStructSize = sizeof(OPENFILENAME);
- ofn.hwndOwner = hWnd;
- ofn.lpstrFilter = szFilter;
- ofn.lpstrFile = szFile;
- ofn.nMaxFile = sizeof(szFile);
- ofn.lpstrFileTitle = szFileTitle;
- ofn.nMaxFileTitle = sizeof(szFileTitle);
- ofn.lpstrInitialDir = NULL;
- ofn.Flags = OFN_OVERWRITEPROMPT;
- if (GetOpenFileName(&ofn))
- lstrcpy(openname, ofn.lpstrFile);
- else
- lstrcpy(openname,"