#include #include"resource.h" #include"bmp_io.h" #include"hensu.h" #include"clip_board.h" #include"noudo.h" #include #include #include"nichishori.h" LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); ATOM InitApp(HINSTANCE); BOOL InitInstance(HINSTANCE,int); LRESULT CALLBACK MyDlgProc(HWND hDlg,UINT msg,WPARAM wp,LPARAM lp); char szClassName[]="template"; int iWidth,iHeight; HINSTANCE hInst; int multi=10; int WINAPI WinMain(HINSTANCE hCurInst,HINSTANCE hPrevInst, LPSTR lpsCmdLine,int nCmdShow) { MSG msg; BOOL bRet; hInst=hCurInst; if(!InitApp(hCurInst)) return FALSE; if(!InitInstance(hCurInst,nCmdShow)) return FALSE; while((bRet=GetMessage(&msg,NULL,0,0))!=0){ if(bRet==-1){ MessageBox(NULL,"GetMessageエラー","Error",MB_OK); break; }else{ TranslateMessage(&msg); DispatchMessage(&msg); } } return (int)msg.wParam; } ATOM InitApp(HINSTANCE hInst) { WNDCLASSEX wc; wc.cbSize=sizeof(WNDCLASSEX); wc.style=CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc=WndProc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hInstance=hInst; wc.hIcon=(HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 0,0, LR_DEFAULTSIZE|LR_SHARED); wc.hCursor=(HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0,0, LR_DEFAULTSIZE|LR_SHARED); wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName="MYMENU"; wc.lpszClassName=(LPCTSTR)szClassName; wc.hIconSm=(HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 0,0, LR_DEFAULTSIZE|LR_SHARED); return RegisterClassEx(&wc); } BOOL InitInstance(HINSTANCE hInst,int nCmdShow) { HWND hWnd; hWnd=CreateWindow(szClassName, "第6章 見やすい画像を作る", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 840, 630, NULL, NULL, hInst, NULL); if(!hWnd) return FALSE; ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp) { static BMP_IO mybmp; static LPBITMAPINFO lpbiInfo; static LPDWORD lpPixel,lpPixel2,lpPixel3; HDROP hDrop; char szFileName[256]; HMENU hMenu; MENUITEMINFO mi; INITCOMMONCONTROLSEX ic; static int wy; static double w,h; static long hist[256]; int i; char text[64]; HPEN hPen,hOldPen; HDC hdc; PAINTSTRUCT ps; switch(msg){ case WM_CREATE: DragAcceptFiles(hWnd,TRUE); ic.dwSize=sizeof(INITCOMMONCONTROLSEX); ic.dwICC=ICC_UPDOWN_CLASS; InitCommonControlsEx(&ic); wy=275; w=1; h=1; break; case WM_DROPFILES: hDrop=(HDROP)wp; DragQueryFile(hDrop,0,szFileName,sizeof(szFileName)); DragFinish(hDrop); if(!mybmp.bmp_in(szFileName,hWnd)) break; lpbiInfo=&mybmp.biInfo; lpPixel=mybmp.lpPixel; iWidth=mybmp.iWidth; iHeight=mybmp.iHeight; lpPixel2=(LPDWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,2*iWidth*iHeight*4); lpPixel3=lpPixel2+iWidth*iHeight; CopyMemory(lpPixel2,lpPixel,iWidth*iHeight*4); CopyMemory(lpPixel3,lpPixel,iWidth*iHeight*4); w=1; h=1; histgram(lpPixel3,hist); InvalidateRect(hWnd,NULL,TRUE); break; case WM_PAINT: hdc=BeginPaint(hWnd,&ps); for(i=0;i<256;i++){ MoveToEx(hdc,10,300+i,NULL); LineTo(hdc,10+hist[i]/2,300+i); } if(wy>=300 && wy<300+256){ sprintf(text,"濃度値%d: %d個(%lf%%)", wy-300,hist[wy-300],(double)hist[wy-300]/(iWidth*iHeight)*100.0); TextOut(hdc,300,230,text,(int)strlen(text)); } hPen=CreatePen(PS_SOLID,1,RGB(255,0,0)); hOldPen=(HPEN)SelectObject(hdc,hPen); MoveToEx(hdc,5,300,NULL); LineTo(hdc,5,556); MoveToEx(hdc,0,wy,NULL); LineTo(hdc,2048,wy); SelectObject(hdc,hOldPen); DeleteObject(hPen); StretchDIBits(hdc,0,0,(int)(w*iWidth),(int)(h*iHeight), 0,0,iWidth,iHeight,lpPixel3,lpbiInfo, DIB_RGB_COLORS,SRCCOPY); EndPaint(hWnd,&ps); break; case WM_COMMAND: switch(LOWORD(wp)){ case IDM_COPY: CopyDIB(hWnd,lpPixel3); break; case IDM_CM: if(DialogBox(hInst,"MYDLG",hWnd,(DLGPROC)MyDlgProc)==IDOK){ amplify(lpPixel,lpPixel2,(double)multi/10); CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); } histgram(lpPixel3,hist); break; case IDM_CA: expand(lpPixel,lpPixel2); CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); histgram(lpPixel3,hist); break; case IDM_CH: plane(lpPixel,lpPixel2); CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); histgram(lpPixel3,hist); break; case IDM_RED: to_color(lpPixel3,lpPixel2,1); break; case IDM_GREEN: to_color(lpPixel3,lpPixel2,2); break; case IDM_BLUE: to_color(lpPixel3,lpPixel2,3); break; case IDM_BEFORE_COLOR: CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); break; case IDM_ORIGINAL: CopyMemory(lpPixel3,lpPixel,iWidth*iHeight*4); histgram(lpPixel3,hist); break; case IDM_ZOOM1: w=1; h=1; break; case IDM_ZOOM2: w=2; h=2; break; case IDM_ZOOM3: w=3; h=3; break; case IDM_ZOOM4: w=4; h=4; break; case IDM_ZOOM02: w=1.0/2; h=1.0/2; break; case IDM_ZOOM03: w=1.0/3; h=1.0/3; break; case IDM_ZOOM04: w=1.0/4; h=1.0/4; break; } InvalidateRect(hWnd,NULL,TRUE); break; case WM_INITMENU: hMenu=GetMenu(hWnd); mi.cbSize=sizeof(MENUITEMINFO); mi.fMask=MIIM_STATE; if(w==1){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM1,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM1,FALSE,&mi); } if(w==2){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM2,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM2,FALSE,&mi); } if(w==3){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM3,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM3,FALSE,&mi); } if(w==4){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM4,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM4,FALSE,&mi); } if(w==1.0/2){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM02,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM02,FALSE,&mi); } if(w==1.0/3){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM03,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM03,FALSE,&mi); } if(w==1.0/4){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM04,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM04,FALSE,&mi); } DrawMenuBar(hWnd); break; case WM_KEYDOWN: switch(LOWORD(wp)){ case VK_UP: wy--; break; case VK_DOWN: wy++; break; } InvalidateRect(hWnd,NULL,TRUE); break; case WM_CLOSE: HeapFree(GetProcessHeap(),0,lpPixel2); DestroyWindow(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,msg,wp,lp); } return 0; } LRESULT CALLBACK MyDlgProc(HWND hDlg,UINT msg,WPARAM wp,LPARAM lp) { static HWND hEdit,hStatic,hUpdown; static int nNo; char szNo[16]; switch(msg){ case WM_INITDIALOG: nNo=multi; hEdit=GetDlgItem(hDlg,IDC_EDIT1); hStatic=GetDlgItem(hDlg,IDC_STATIC1); hUpdown=GetDlgItem(hDlg,IDC_SPIN1); SendMessage(hUpdown,UDM_SETBUDDY,(WPARAM)hEdit,0); SendMessage(hUpdown,UDM_SETRANGE32,(WPARAM)0,(LPARAM)2550); SendMessage(hUpdown,UDM_SETPOS,0,(LPARAM)MAKELONG((short)nNo,0)); sprintf(szNo,"%lf 倍",(double)nNo/10); SetWindowText(hStatic,szNo); return TRUE; case WM_COMMAND: switch(LOWORD(wp)){ case IDOK: multi=nNo; EndDialog(hDlg,IDOK); return TRUE; case IDC_CANCEL: EndDialog(hDlg,IDC_CANCEL); return TRUE; } if((HWND)lp==hEdit && HIWORD(wp)==EN_CHANGE){ nNo=(int)SendMessage(hUpdown,UDM_GETPOS,0,0); sprintf(szNo,"%lf 倍",(double)nNo/10); SetWindowText(hStatic,szNo); return TRUE; } return FALSE; } return FALSE; }