[PR]生年月日で2010年運命占い:初回無料!貴女の悩みを占い師に相談
001: #include <windows.h>
002: #include <wingdi.h>
003: #include <commctrl.h>
004: #include "wingui.h"
005:
006:
007:
008: HINSTANCE hAppInst;
009: HWND hAppWnd;
010: UINT iWidth,iHeight,iLength;
011: UINT iSize,biSize,bmpLoad;
012: UINT iOrgWidth,iOrgHeight;
013: UINT iGHist[512];
014: LPBYTE lpOrgBMP,lpBMP;
015: LPBITMAPINFO lpBInfo;
016: extern MenuInfo MI[];
017:
018:
019: int WINAPI WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst,
020: LPSTR lpsCmdLine, int nCmdShow)
021: {
022: LPCTSTR szClassName = "BMP画像処理";
023: bmpLoad = 0; iWidth = 300; iHeight = 200;
024:
025:
026:
027: if(!hPrevInst){
028: if(!InitApp(hCurInst,szClassName)) return 0;
029: hAppInst = hCurInst;
030: }
031:
032:
033:
034: if(!InitInstance(hCurInst,szClassName,lpsCmdLine,nCmdShow)){
035: return 0;
036: }
037:
038: MSG msg;
039: while(GetMessage(&msg,NULL,0,0)){
040: TranslateMessage(&msg);
041: DispatchMessage(&msg);
042: }
043: return msg.wParam;
044: }
045:
046:
047:
048: BOOL InitApp(HINSTANCE hInst, LPCSTR szClassName)
049: {
050: WNDCLASSEX wc;
051: wc.cbSize = sizeof(wc);
052: wc.style = CS_HREDRAW | CS_VREDRAW;
053: wc.lpfnWndProc = WndProc;
054: wc.cbClsExtra = 0;
055: wc.cbWndExtra = 0;
056: wc.hInstance = hInst;
057: wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
058: wc.hCursor = LoadCursor(NULL,IDC_ARROW);
059: wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
060: wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
061: wc.lpszMenuName = NULL;
062: wc.lpszClassName = (LPCSTR)szClassName;
063: return (RegisterClassEx(&wc));
064: }
065:
066:
067:
068: BOOL InitInstance(HINSTANCE hInst,LPCSTR szClassName,
069: LPSTR lpsCmdLine,int nCmdShow)
070: {
071: TCHAR lpStr[256]; LPSTR ifile;
072:
073:
074:
075: if(strlen(lpsCmdLine)) {
076: ifile = lpsCmdLine;
077:
078: if(!LoadBMP(ifile,lpBInfo,lpOrgBMP)){
079: MessageBox(NULL,"読込み失敗",NULL,MB_OK); return 0;
080: }
081:
082: set_image_size(lpBInfo);
083:
084: lpBMP=(LPBYTE)GlobalAlloc(GPTR,iLength*iHeight);
085: CopyMemory(lpBMP,lpOrgBMP,iLength*iHeight);
086: }
087:
088: if(bmpLoad) wsprintf(lpStr,"%s %d*%d Pixel %dBit",
089: ifile,iWidth,iHeight,
090: lpBInfo->bmiHeader.biBitCount);
091: else wsprintf(lpStr,"BMP画像処理");
092:
093:
094:
095: HWND hWnd;
096: hWnd = CreateWindow(szClassName,lpStr,
097: WS_OVERLAPPEDWINDOW,
098: CW_USEDEFAULT,
099: CW_USEDEFAULT,
100: 15+iWidth,
101: 70+iHeight,
102: NULL,
103: NULL,
104: hInst,
105: NULL);
106: if(!hWnd) return FALSE;
107:
108: DragAcceptFiles(hWnd,TRUE);
109:
110: ShowWindow(hWnd,nCmdShow);
111:
112: UpdateWindow(hWnd);
113:
114: hAppWnd = hWnd;
115: return TRUE;
116: }
117:
118:
119:
120: LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg,
121: WPARAM wParam, LPARAM lParam)
122: {
123: static HWND hwB1,hwB2,hwB3,hwSel;
124: static UINT s,mi,mlen,mlen_max;
125: TCHAR szFn[MAX_PATH];
126: HDC hdc; HDROP hDrop; PAINTSTRUCT ps;
127: switch(iMsg){
128: case WM_CHAR:
129: if(wParam=='q') SendMessage(hwnd,WM_DESTROY,NULL,NULL); break;
130: case WM_CREATE: {
131: InitCommonControls();
132:
133: hwB1=CreateWindow("Button","開く", WS_CHILD | WS_VISIBLE,
134: 5,5,60,25,hwnd,(HMENU)ID_OPEN,hAppInst,NULL);
135: hwB2=CreateWindow("Button","保存", WS_CHILD | WS_VISIBLE,
136: 65,5,60,25,hwnd,(HMENU)ID_SAVE,hAppInst,NULL);
137: hwB3=CreateWindow("Button","実行", WS_CHILD | WS_VISIBLE,
138: 125,5,60,25,hwnd,(HMENU)ID_IP,hAppInst,NULL);
139:
140: mi = mlen_max = 0;
141: while(MI[mi].menu!=NULL){
142: if(mlen_max < strlen(MI[mi].menu))
143: mlen_max = strlen(MI[mi].menu);
144: mi++;
145: }
146: mlen = 8*(mlen_max+1);
147: hwSel=CreateWindow("COMBOBOX",NULL,
148: WS_CHILD|WS_VISIBLE|CBS_DROPDOWN,
149: 190,5,mlen,200,hwnd,(HMENU)0,hAppInst,NULL);
150: mi = 0;
151: while(MI[mi].menu!=NULL){
152: SendMessage(hwSel,CB_ADDSTRING,0,(LPARAM)MI[mi].menu);
153: mi++;
154: }
155: SendMessage(hwSel,CB_SETCURSEL,0,1);
156: } break;
157: case WM_COMMAND:
158: switch(LOWORD(wParam)){
159: case 'q':
160: SendMessage(hwnd,WM_DESTROY,NULL,NULL); break;
161: case ID_OPEN:
162: if(OpenFile(hwnd,szFn)){
163: reLoadBMP(szFn);
164: }
165: break;
166: case ID_SAVE:
167: if(SaveFile(hwnd,szFn)){
168: SaveBMP(szFn,lpBInfo,lpBMP);
169: }
170: break;
171: case ID_IP:
172: if(bmpLoad){
173: s=SendMessage(hwSel,CB_GETCURSEL,0,0);
174: MI[s].proc();
175: InvalidateRgn(hwnd,NULL,TRUE);
176: UpdateWindow(hwnd);
177: }
178: break;
179: }
180: break;
181: case WM_DROPFILES:
182:
183: hDrop=(HDROP)wParam;
184: DragQueryFile(hDrop,0,szFn,MAX_PATH);
185: DragFinish(hDrop);
186:
187: reLoadBMP(szFn);
188: break;
189: case WM_PAINT:
190: if(bmpLoad){
191: hdc=BeginPaint(hwnd,&ps);
192: StretchDIBits(hdc,4,35,iWidth,iHeight,
193: 0,0,iWidth,iHeight,lpBMP,lpBInfo,
194: DIB_RGB_COLORS,SRCCOPY);
195: EndPaint(hwnd,&ps);
196: }
197: break;
198: case WM_DESTROY:
199: GlobalFree(lpBInfo);
200: GlobalFree(lpOrgBMP);
201: PostQuitMessage(0);
202: break;
203: }
204: return DefWindowProc(hwnd,iMsg,wParam,lParam);
205: }
206:
207:
208:
209: void GetWinPos(HWND hWnd,UINT *x,UINT *y)
210: {
211: WINDOWPLACEMENT wndpl;
212: GetWindowPlacement(hWnd,&wndpl);
213: *x = wndpl.rcNormalPosition.left;
214: *y = wndpl.rcNormalPosition.top;
215: return;
216: }
217:
218:
219:
220: void set_image_size(LPBITMAPINFO bInfo)
221: {
222: iWidth = bInfo->bmiHeader.biWidth;
223: iHeight = bInfo->bmiHeader.biHeight;
224: iSize = iWidth*iHeight;
225: iOrgWidth = iWidth;
226: iOrgHeight = iHeight;
227:
228: if((iWidth*3)%4==0) iLength = iWidth*3;
229: else iLength = iWidth*3+(4-(iWidth*3)%4);
230: biSize = iHeight*iLength;
231:
232: bmpLoad = 1;
233: }
234:
235: void set_ip_window(UINT xsize,UINT ysize)
236: {
237: if(xsize && ysize){
238: iWidth = xsize;
239: iHeight = ysize;
240: } else {
241: if(xsize > iWidth) return;
242: if(ysize > iHeight) return;
243: iWidth = iOrgWidth;
244: iHeight = iOrgHeight;
245: }
246: iSize = iWidth*iHeight;
247:
248: if((iWidth*3)%4==0) iLength = iWidth*3;
249: else iLength = iWidth*3+(4-(iWidth*3)%4);
250: biSize = iHeight*iLength;
251:
252: lpBInfo->bmiHeader.biWidth = iWidth;
253: lpBInfo->bmiHeader.biHeight = iHeight;
254: }
255:
256:
257:
258: UINT OpenFile(HWND hWnd, LPTSTR lpFname)
259: {
260: static OPENFILENAME ofn;
261: TCHAR szFn[MAX_PATH],szFt[MAX_PATH];
262: FillMemory(szFn,MAX_PATH,0);
263:
264: ofn.lStructSize = sizeof(OPENFILENAME);
265: ofn.hwndOwner = hWnd;
266: ofn.hInstance = NULL;
267: ofn.lpstrFilter =
268: "Bitmap(*.bmp)\0*.bmp\0すべてのファイル(*.*)\0*.*\0\0";
269: ofn.lpstrCustomFilter = NULL;
270: ofn.nMaxCustFilter = 0;
271: ofn.nFilterIndex = 1;
272: ofn.lpstrFile = szFn;
273: ofn.nMaxFile = 255;
274: ofn.lpstrFileTitle = szFt;
275: ofn.nMaxFileTitle = 255;
276: ofn.lpstrInitialDir = NULL;
277: ofn.lpstrTitle = "Bitmap";
278: ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
279: ofn.nFileOffset = 0;
280: ofn.nFileExtension = 0;
281: ofn.lpstrDefExt = "bmp";
282: ofn.lCustData = 0;
283: ofn.lpfnHook = NULL;
284: ofn.lpTemplateName = NULL;
285:
286: if(!GetOpenFileName(&ofn)) return FALSE;
287: wsprintf(lpFname,szFn);
288: return TRUE;
289: }
290:
291:
292:
293: UINT SaveFile(HWND hWnd, LPTSTR lpFname)
294: {
295: static OPENFILENAME ofn;
296: TCHAR szFn[MAX_PATH],szFt[MAX_PATH];
297: FillMemory(szFn,MAX_PATH,0);
298:
299: ofn.lStructSize = sizeof(OPENFILENAME);
300: ofn.hwndOwner = hWnd;
301: ofn.hInstance = NULL;
302: ofn.lpstrFilter =
303: "Bitmap(*.bmp)\0*.bmp\0すべてのファイル(*.*)\0*.*\0\0";
304: ofn.lpstrCustomFilter = NULL;
305: ofn.nMaxCustFilter = 0;
306: ofn.nFilterIndex = 1;
307: ofn.lpstrFile = szFn;
308: ofn.nMaxFile = 255;
309: ofn.lpstrFileTitle = szFt;
310: ofn.nMaxFileTitle = 255;
311: ofn.lpstrInitialDir = NULL;
312: ofn.lpstrTitle = "Bitmap";
313: ofn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
314: ofn.nFileOffset = 0;
315: ofn.nFileExtension = 0;
316: ofn.lpstrDefExt = "bmp";
317: ofn.lCustData = 0;
318: ofn.lpfnHook = NULL;
319: ofn.lpTemplateName = NULL;
320:
321: if(!GetSaveFileName(&ofn)) return FALSE;
322: wsprintf(lpFname,szFn);
323: return TRUE;
324: }
325:
326:
327:
328: UINT LoadBMP(LPCTSTR lpFname,LPBITMAPINFO& biBuf,LPBYTE& lpBuf)
329: {
330: HANDLE hdlBmp;
331: BITMAPFILEHEADER bfhBmp;
332: DWORD dwdFileSize =0;
333: DWORD dwdPixelsSize=0;
334:
335: hdlBmp=CreateFile(lpFname,GENERIC_READ,0,NULL,OPEN_EXISTING,
336: FILE_ATTRIBUTE_NORMAL,NULL);
337: if(hdlBmp==INVALID_HANDLE_VALUE){
338: MessageBox(NULL,"読込みに失敗しました",NULL,MB_OK);
339: return FALSE;
340: }
341:
342: ReadFile(hdlBmp,&bfhBmp,sizeof(BITMAPFILEHEADER),&dwdFileSize,NULL);
343:
344: biBuf=(LPBITMAPINFO)GlobalAlloc(GPTR,sizeof(BITMAPINFO));
345: ReadFile(hdlBmp,biBuf,sizeof(BITMAPINFOHEADER),&dwdFileSize,NULL);
346:
347: dwdPixelsSize=((biBuf->bmiHeader.biWidth+3)&0xfffffffc) *
348: biBuf->bmiHeader.biHeight*3;
349: lpBuf=(LPBYTE)GlobalAlloc(GPTR,dwdPixelsSize);
350: ReadFile(hdlBmp,lpBuf,dwdPixelsSize,&dwdFileSize,NULL);
351:
352: CloseHandle(hdlBmp);
353:
354: if(bfhBmp.bfType!='M'*256+'B' || biBuf->bmiHeader.biBitCount!=24){
355:
356: GlobalFree(biBuf);
357: GlobalFree(lpBuf);
358: MessageBox(NULL,"ファイルの種類が不正です",NULL,MB_OK);
359:
360: return FALSE;
361: }
362: return TRUE;
363: }
364:
365:
366:
367: UINT reLoadBMP(LPCTSTR lpFname)
368: {
369: UINT wx,wy; TCHAR lpStr[256];
370:
371: GlobalFree(lpBInfo);
372: GlobalFree(lpOrgBMP);
373: GlobalFree(lpBMP);
374:
375: if(LoadBMP(lpFname,lpBInfo,lpOrgBMP)){
376:
377: set_image_size(lpBInfo);
378:
379: lpBMP=(LPBYTE)GlobalAlloc(GPTR,iLength*iHeight);
380: CopyMemory(lpBMP,lpOrgBMP,iLength*iHeight);
381:
382: GetWinPos(hAppWnd,&wx,&wy);
383: MoveWindow(hAppWnd,wx,wy,15+iWidth,iHeight+70,TRUE);
384:
385: wsprintf(lpStr,"%s %d*%d Pixel %dBit",
386: lpFname,iWidth,iHeight,lpBInfo->bmiHeader.biBitCount);
387: SetWindowText(hAppWnd,(LPSTR)lpStr);
388: return TRUE;
389: }
390: return FALSE;
391: }
392:
393:
394:
395: UINT SaveBMP(LPCTSTR lpFname,LPBITMAPINFO biBuf,LPBYTE lpBuf)
396: {
397: HANDLE hdlBmp;
398: BITMAPFILEHEADER bfhBmp;
399: LONG offset=14+40;
400: DWORD dwdFileSize=0;
401: DWORD dwdPixelsSize=iHeight*iLength;
402:
403: hdlBmp=CreateFile(lpFname,GENERIC_WRITE,0,NULL,OPEN_ALWAYS,
404: FILE_ATTRIBUTE_NORMAL,NULL);
405: if(hdlBmp==INVALID_HANDLE_VALUE){
406: MessageBox(NULL,"書込みに失敗しました",NULL,MB_OK);
407: return FALSE;
408: }
409: bfhBmp.bfType ='M'*256+'B';
410: bfhBmp.bfSize = offset+dwdPixelsSize;
411: bfhBmp.bfReserved1 = 0;
412: bfhBmp.bfReserved2 = 0;
413: bfhBmp.bfOffBits = offset;
414:
415: WriteFile(hdlBmp,&bfhBmp,sizeof(BITMAPFILEHEADER),&dwdFileSize,NULL);
416:
417: WriteFile(hdlBmp,biBuf,sizeof(BITMAPINFOHEADER),&dwdFileSize,NULL);
418:
419: WriteFile(hdlBmp,lpBuf,dwdPixelsSize,&dwdFileSize,NULL);
420:
421: CloseHandle(hdlBmp);
422: return TRUE;
423: }
424:
425:
426:
427: LPBYTE Load_template(UINT& sizex,UINT& sizey)
428: {
429: TCHAR szFn[MAX_PATH];
430: LPBYTE lpTempBMP;
431: LPBITMAPINFO bInfo;
432:
433: if(!OpenFile(hAppWnd,szFn)) return NULL;
434:
435: if(!LoadBMP(szFn,bInfo,lpTempBMP)) return NULL;
436:
437: sizex = bInfo->bmiHeader.biWidth;
438: sizey = bInfo->bmiHeader.biHeight;
439: return lpTempBMP;
440: }