resize_dlg.cpp
上传用户:yatsl7111
上传日期:2007-01-08
资源大小:1433k
文件大小:14k
源码类别:

图形图象

开发平台:

Visual C++

  1. /********************************************************************
  2. resize_dlg.cpp - ISee图像浏览器—图像处理模块图像重定义尺寸处理实现代码文件
  3.     版权所有(C) VCHelp-coPathway-ISee workgroup 2000 all member's
  4.     这一程序是自由软件,你可以遵照自由软件基金会出版的GNU 通用许可证
  5. 条款来修改和重新发布这一程序。或者用许可证的第二版,或者(根据你
  6. 的选择)用任何更新的版本。
  7.     发布这一程序的目的是希望它有用,但没有任何担保。甚至没有适合特定
  8. 目地的隐含的担保。更详细的情况请参阅GNU通用许可证。
  9.     你应该已经和程序一起收到一份GNU通用许可证(GPL)的副本。如果还没有,
  10. 写信给:
  11.     The Free Software Foundation, Inc.,  675  Mass Ave,  Cambridge,
  12.     MA02139,  USA
  13. 如果你在使用本软件时有什么问题或建议,用以下地址可以与我们取得联
  14. 系:
  15. http://isee.126.com
  16. http://www.vchelp.net
  17. 或:
  18. iseesoft@china.com
  19. 作者:临风
  20.    e-mail:ringphone@sina.com
  21.    功能实现:重定义图像尺寸
  22. 文件版本:
  23. Build 00617
  24. Date  2000-6-17
  25. ********************************************************************/
  26. #include "p_win.h"
  27. #include "commctrl.h"
  28. #include "math.h"
  29. #include "..publicgol_proc.h"
  30. #include "filter.h"
  31. #include "resize.h"
  32. #include "resource.h"
  33. BOOL CALLBACK WINAPI ResizeProc(HWND hWnd,UINT uMsg,UINT wParam,LONG lParam);
  34. void AutoSize_size(HWND,DWORD,DWORD,DWORD); //按比例缩放时宽高设定连动
  35. void AutoSize_per(HWND,DWORD);               //按比例缩放时选百分比宽高设定连动
  36. void SetMaxSize(HWND hWnd);
  37. void EnableBkg_Sel(HWND hWnd,BOOL flag);     //禁止/启用背景色设定
  38. void SelBkColor(HWND hWnd);
  39. void ColToStr(LPSTR str);
  40. void format_colstr(HWND hWnd);
  41. void Enable_SelPos(HWND hWnd,BOOL flag);
  42. float max_size,max_per;
  43. BOOL bAuto=TRUE; //图像按比例缩放
  44. BOOL bEditCanUpdate=TRUE; //允许响应EN_UPDATE消息,设置该标志防止死循环
  45. int rs_flag=RS_STRETCH;     //重定义尺寸图像处理方法(扩展,平铺,居中等)
  46. int rs_pos=RS_UL; //保持图像标志
  47. COLORREF rs_bgcolor=0x00ffffff; //背景色
  48. RECT rt;                         //背景色预览控件的RECT
  49. BOOL bEnable_SelPos=FALSE; //允许选择保持图像位置开关,默认为关
  50. HICON hPosIcon;
  51. //重定义尺寸
  52. int DoResize(LPIMAGEPROCSTR lpInfo)
  53. {
  54.    return DialogBox(lpInfo->hInst,MAKEINTRESOURCE(IDD_RESIZE),lpInfo->hParentWnd,(DLGPROC)ResizeProc);
  55. }
  56. //对话框处理循环
  57. #pragma argsused
  58. BOOL CALLBACK WINAPI ResizeProc(HWND hWnd,UINT uMsg,UINT wParam,LONG lParam)
  59. {
  60. switch(uMsg)
  61.    {
  62.     case WM_INITDIALOG:
  63.        //初始化
  64.          hbm=LoadBitmap(lpProcInfo->hInst,MAKEINTRESOURCE(IDB_BKG));
  65.          hPosIcon=LoadIcon(lpProcInfo->hInst,MAKEINTRESOURCE(IDI_POS));
  66.          lpProcInfo->result=PR_NULL;
  67.          char s[80];
  68.          wsprintf(s,"原 图 像 尺 寸:%d*%d",lpProcInfo->sImageInfo.width,lpProcInfo->sImageInfo.height);
  69.          SetDlgItemText(hWnd,IDC_RS_ORGSIZE,s);
  70.          SetMaxSize(hWnd); //根据可用内存分配新图像最大尺寸(内存的一半)
  71.          CheckDlgButton(hWnd,IDC_RS_AUTO,BST_CHECKED); //设定为按比例缩放
  72.          CheckDlgButton(hWnd,IDC_RS_STRETCH,BST_CHECKED);//设定为图像拉伸
  73.          //禁止设定背景色控件
  74.          EnableBkg_Sel(hWnd,FALSE);
  75.          SetDlgItemInt(hWnd,IDC_RS_WIDTH,lpProcInfo->sImageInfo.width,FALSE);
  76.          SetDlgItemInt(hWnd,IDC_RS_HEIGHT,lpProcInfo->sImageInfo.height,FALSE);
  77.          SetDlgItemInt(hWnd,IDC_RS_PER,100,FALSE);
  78.          //设定百分比上下限
  79.          SendMessage(GetDlgItem(hWnd,IDC_RS_PERUD),UDM_SETRANGE,0,MAKELONG(max_per*100,1));
  80.     GetWindowRect(GetDlgItem(hWnd,IDC_RS_PREVBKGCOLOR),&rt);
  81. rt.right=rt.right-rt.left-2;
  82.     rt.left=0;
  83.    rt.bottom=rt.bottom-rt.top-2;
  84.     rt.top=0;
  85.          SendMessage(GetDlgItem(hWnd,IDC_RS_BKGCOLOR),EM_LIMITTEXT,7,0);
  86.          break;
  87.       case WM_PAINT:
  88.       {
  89.        PaintDlgBk(hWnd);
  90.          PAINTSTRUCT ps;
  91.          BeginPaint(GetDlgItem(hWnd,IDC_RS_PREVBKGCOLOR),&ps);
  92.    FillRect(ps.hdc,&rt,CreateSolidBrush(rs_bgcolor));
  93.     EndPaint(hWnd,&ps);
  94.       }
  95.          break;
  96.       case WM_DRAWITEM:
  97.        if((UINT)wParam==IDC_RS_PREVBKGCOLOR)
  98.           break;
  99.        if(bEnable_SelPos)
  100.          {
  101.             LPDRAWITEMSTRUCT lpds=(LPDRAWITEMSTRUCT)lParam;
  102.             if((int)wParam==rs_pos)
  103.             {
  104.              DrawEdge(lpds->hDC,&(lpds->rcItem),BDR_SUNKENINNER,BF_RECT);
  105.                DrawIconEx(lpds->hDC,4,4,hPosIcon,16,16,0,NULL,DI_NORMAL);
  106.             }
  107.             else
  108.           DrawEdge(lpds->hDC,&(lpds->rcItem),BDR_RAISEDINNER,BF_RECT);
  109.          }
  110.        break;
  111.       case WM_CTLCOLORSTATIC:
  112.          SetBkColor((HDC)wParam,0x00ffbf80);        return (BOOL)GetStockObject(NULL_BRUSH);
  113.       case WM_LBUTTONUP:
  114.          break;
  115.       case WM_MOUSEMOVE:
  116. break;
  117.       case WM_COMMAND:
  118.       {
  119.          int id=LOWORD(wParam);
  120.        switch(id)
  121.          {
  122.           case IDOK:
  123.             {
  124.              int w,h;
  125.                w=GetDlgItemInt(hWnd,IDC_RS_WIDTH,NULL,FALSE);
  126.                h=GetDlgItemInt(hWnd,IDC_RS_HEIGHT,NULL,FALSE);
  127.                if((w*h)>max_size)
  128.                {
  129.                 Errmsg("图像尺寸已大于允许的最大尺寸!");
  130.                   return FALSE;
  131.                }
  132.                EndDialog(hWnd,Resize(w,h,rs_flag,rs_pos,rs_bgcolor));
  133.                return TRUE;
  134.             }
  135.             case IDCANCEL:
  136.              EndDialog(hWnd,PROCERR_FALSE);
  137.                return FALSE;
  138.             case IDHELP:
  139.              ShowCopyright();
  140.                return FALSE;
  141.             case IDC_RS_AUTO:
  142.              if(IsDlgButtonChecked(hWnd,IDC_RS_AUTO)==BST_CHECKED)
  143.                 bAuto=TRUE;
  144.                else
  145.                 bAuto=FALSE;
  146.                break;
  147.             case IDC_RS_STRETCH:
  148.              rs_flag=RS_STRETCH;
  149.                EnableBkg_Sel(hWnd,FALSE);
  150.                Enable_SelPos(hWnd,FALSE);
  151.                break;
  152.             case IDC_RS_CENTER:
  153.              rs_flag=RS_CENTER;
  154.                EnableBkg_Sel(hWnd,TRUE);
  155.                Enable_SelPos(hWnd,FALSE);
  156.                break;
  157.             case IDC_RS_TILED:
  158.              rs_flag=RS_TILE;
  159.                EnableBkg_Sel(hWnd,FALSE);
  160.                Enable_SelPos(hWnd,FALSE);
  161.                break;
  162.             case IDC_RS_KEEPED:
  163.              rs_flag=RS_KEEP;
  164.                EnableBkg_Sel(hWnd,TRUE);
  165.                Enable_SelPos(hWnd,TRUE);
  166.                break;
  167.             case IDC_RS_SELBKGCOLOR:
  168.              SelBkColor(hWnd);
  169.                break;
  170.             case IDC_RS_KEEP_UL:
  171.              rs_pos=RS_UL;
  172.                Enable_SelPos(hWnd,TRUE);
  173.                break;
  174.             case IDC_RS_KEEP_UM:
  175.              rs_pos=RS_UM;
  176.                Enable_SelPos(hWnd,TRUE);
  177.                break;
  178.             case IDC_RS_KEEP_UR:
  179.              rs_pos=RS_UR;
  180.                Enable_SelPos(hWnd,TRUE);
  181.                break;
  182.             case IDC_RS_KEEP_ML:
  183.              rs_pos=RS_ML;
  184.                Enable_SelPos(hWnd,TRUE);
  185.                break;
  186.             case IDC_RS_KEEP_MM:
  187.              rs_pos=RS_MM;
  188.                Enable_SelPos(hWnd,TRUE);
  189.                break;
  190.             case IDC_RS_KEEP_MR:
  191.              rs_pos=RS_MR;
  192.                Enable_SelPos(hWnd,TRUE);
  193.                break;
  194.             case IDC_RS_KEEP_DL:
  195.              rs_pos=RS_DL;
  196.                Enable_SelPos(hWnd,TRUE);
  197.                break;
  198.             case IDC_RS_KEEP_DM:
  199.              rs_pos=RS_DM;
  200.                Enable_SelPos(hWnd,TRUE);
  201.                break;
  202.             case IDC_RS_KEEP_DR:
  203.              rs_pos=RS_DR;
  204.                Enable_SelPos(hWnd,TRUE);
  205.                break;
  206.          }
  207.          switch(HIWORD(wParam))
  208.          {
  209.           case EN_UPDATE:
  210.              switch(id)
  211.                {
  212.                 case IDC_RS_WIDTH:
  213.                      if(bEditCanUpdate)
  214.                      AutoSize_size(hWnd,GetDlgItemInt(hWnd,id,NULL,FALSE),IDC_RS_WIDTH,IDC_RS_HEIGHT);
  215.                      break;
  216.                   case IDC_RS_HEIGHT:
  217.                    if(bEditCanUpdate)
  218.                    AutoSize_size(hWnd,GetDlgItemInt(hWnd,id,NULL,FALSE),IDC_RS_HEIGHT,IDC_RS_WIDTH);
  219.                      break;
  220.                 case IDC_RS_PER:
  221.                    if(bEditCanUpdate)
  222.                    AutoSize_per(hWnd,GetDlgItemInt(hWnd,id,NULL,FALSE));
  223.                      break;
  224.                   case IDC_RS_BKGCOLOR:
  225.                    if(bEditCanUpdate)
  226.                       format_colstr(hWnd);
  227.                    break;
  228.                }
  229.              break;
  230.          }
  231.          break;
  232.       }
  233.       case WM_CLOSE:
  234. //       Clear();
  235. DeleteObject(hbm);
  236.          DestroyIcon(hPosIcon);
  237.          EndDialog(hWnd,PROCERR_FALSE);
  238.          return FALSE;
  239.    }
  240.    return FALSE;
  241. }
  242. void SetMaxSize(HWND hWnd)
  243. {
  244. MEMORYSTATUS mem;
  245.    mem.dwLength=sizeof(MEMORYSTATUS);
  246.    GlobalMemoryStatus(&mem);
  247.    max_size=(mem.dwAvailPageFile/2)/(lpProcInfo->sImageInfo.bitperpix/8);
  248.    max_per=sqrt((float)max_size/(float)(lpProcInfo->sImageInfo.width*lpProcInfo->sImageInfo.height));
  249.    char s[80];
  250.    wsprintf(s,"可缩放最大尺寸:%d*%d",(DWORD)(lpProcInfo->sImageInfo.width*max_per),(DWORD)(lpProcInfo->sImageInfo.height*max_per));
  251.    SetDlgItemText(hWnd,IDC_RS_MAXSIZE,s);
  252. }
  253. //按比例缩放时宽高设定连动
  254. void AutoSize_size(HWND hWnd,DWORD size,DWORD this_id,DWORD other_id)
  255. {
  256. if(size<8)
  257.     return;
  258.    if(size>max_size)
  259.    {
  260.       bEditCanUpdate=FALSE;
  261.     SetDlgItemInt(hWnd,this_id,max_size/8,FALSE);
  262.       SetDlgItemInt(hWnd,other_id,8,FALSE);
  263.       bEditCanUpdate=TRUE;
  264.       return;
  265.    }
  266.    if(bAuto)
  267.    {
  268.     float n,per;
  269.    if(this_id == IDC_RS_WIDTH)
  270.     {
  271.     n = (float)size*((float)lpProcInfo->sImageInfo.height/(float)lpProcInfo->sImageInfo.width);
  272.       per=100*(float)size/(float)lpProcInfo->sImageInfo.width;
  273.     }
  274.    else
  275.     {
  276.     n = (float)size*((float)lpProcInfo->sImageInfo.width/(float)lpProcInfo->sImageInfo.height);
  277.       per=100*(float)size/(float)lpProcInfo->sImageInfo.height;
  278.     }
  279.       if(n<8)
  280.        n=8;
  281. bEditCanUpdate=FALSE;
  282.       if(per>max_per*100)
  283.       {
  284. SetDlgItemInt(hWnd,IDC_RS_WIDTH,(DWORD)(lpProcInfo->sImageInfo.width*(float)max_per),FALSE);
  285.          SetDlgItemInt(hWnd,IDC_RS_HEIGHT,(DWORD)(lpProcInfo->sImageInfo.height*(float)max_per),FALSE);
  286.          SetDlgItemInt(hWnd,IDC_RS_PER,(DWORD)(max_per*100),FALSE);
  287.       }
  288.       else
  289.       {
  290.        SetDlgItemInt(hWnd,IDC_RS_PER,(DWORD)per,FALSE);
  291.     SetDlgItemInt(hWnd,other_id,(DWORD)n,FALSE);
  292.       }
  293.     bEditCanUpdate=TRUE;
  294.    }
  295. }
  296. //按比例缩放时选百分比宽高设定连动
  297. void AutoSize_per(HWND hWnd,DWORD size)
  298. {
  299. float w,h;
  300.    if(size>max_per*100)
  301.    {
  302.       SetDlgItemInt(hWnd,IDC_RS_PER,max_per*100,FALSE);
  303.       return;
  304.    }
  305.    if(bAuto)
  306.    {
  307.    w = (float)lpProcInfo->sImageInfo.width*(float)size/(float)100;
  308.     h = (float)lpProcInfo->sImageInfo.height*(float)size/(float)100;
  309.    if(w<8)
  310.     w=8;
  311.    if(h<8)
  312.     h=8;
  313.    bEditCanUpdate=FALSE;
  314.     SetDlgItemInt(hWnd,IDC_RS_WIDTH,(DWORD)w,FALSE);
  315.    SetDlgItemInt(hWnd,IDC_RS_HEIGHT,(DWORD)h,FALSE);
  316.     bEditCanUpdate=TRUE;
  317.    }
  318. }
  319. //禁止/启用背景色设定
  320. void EnableBkg_Sel(HWND hWnd,BOOL flag)
  321. {
  322.    HWND hwnd=GetDlgItem(hWnd,IDC_RS_PREVBKGCOLOR);
  323. EnableWindow(hwnd,flag);
  324.    EnableWindow(GetDlgItem(hWnd,IDC_RS_BKGCOLOR),flag);
  325.    EnableWindow(GetDlgItem(hWnd,IDC_RS_SELBKGCOLOR),flag);
  326.    HDC hdc=GetDC(hwnd);
  327.    FillRect(hdc,&rt,CreateSolidBrush(rs_bgcolor));
  328.    ReleaseDC(hwnd,hdc);
  329.    InvalidateRect(hwnd,&rt,TRUE);
  330. }
  331. void Enable_SelPos(HWND hWnd,BOOL flag)
  332. {
  333.    bEnable_SelPos=flag;
  334.    RECT rct,rwt;
  335.    GetWindowRect(GetDlgItem(hWnd,IDC_RS_KEEP_UL),&rct);
  336.    GetWindowRect(hWnd,&rwt);
  337.    int w=(rct.right-rct.left)*3+10;
  338.    rct.left=rct.left-rwt.left-GetSystemMetrics(SM_CXSIZEFRAME)-5;
  339.    rct.top=rct.top-rwt.top-GetSystemMetrics(SM_CYCAPTION)-5;
  340.    rct.right=rct.left+w;
  341.    rct.bottom=rct.top+w;
  342.    InvalidateRect(hWnd,&rct,TRUE);
  343. }
  344. void SelBkColor(HWND hWnd)
  345. {
  346. CHOOSECOLOR cc;
  347.    COLORREF rf[16];
  348.    cc.lStructSize=sizeof(CHOOSECOLOR);
  349.    cc.hwndOwner=hWnd;
  350.    cc.hInstance=NULL;
  351.    cc.rgbResult=rs_bgcolor;
  352.    cc.lpCustColors=rf;
  353.    cc.Flags=CC_FULLOPEN | CC_RGBINIT;
  354.    cc.lCustData=NULL;
  355.    cc.lpfnHook=NULL;
  356.    cc.lpTemplateName=NULL;
  357.    if(ChooseColor(&cc)!=0)
  358.    {
  359.       rs_bgcolor=cc.rgbResult;
  360.       char temp[10];
  361.       ColToStr(temp);
  362.       SetDlgItemText(hWnd,IDC_RS_BKGCOLOR,temp);
  363.       HWND hwnd=GetDlgItem(hWnd,IDC_RS_PREVBKGCOLOR);
  364. HDC hdc=GetDC(hwnd);
  365.    FillRect(hdc,&rt,CreateSolidBrush(rs_bgcolor));
  366.     ReleaseDC(hwnd,hdc);
  367.    }
  368. }
  369. void ColToStr(LPSTR str)
  370. {
  371.    char s[3];
  372.    BYTE x;
  373.    wsprintf(str,"#");
  374.    x=(BYTE)((rs_bgcolor<<24)>>24);
  375.    if(x<0x10)
  376.     wsprintf(s,"0%x",x);
  377.    else
  378.     wsprintf(s,"%x",x);
  379.    strcat(str,s);
  380.    x=(BYTE)((rs_bgcolor<<16)>>24);
  381.    if(x<0x10)
  382.     wsprintf(s,"0%x",x);
  383.    else
  384.     wsprintf(s,"%x",x);
  385.    strcat(str,s);
  386.    x=(BYTE)((rs_bgcolor<<8)>>24);
  387.    if(x<0x10)
  388.     wsprintf(s,"0%x",x);
  389.    else
  390.     wsprintf(s,"%x",x);
  391.    strcat(str,s);
  392.    for(int i=1;i<7;i++)
  393.    {
  394.     if(str[i]>0x60)
  395.     str[i]-=0x20;
  396.    }
  397. }
  398. void format_colstr(HWND hWnd)
  399. {
  400. char str[8];
  401.    DWORD cr[6]={0,0,0,0,0,0};
  402.    ZeroMemory(str,8);
  403.    GetDlgItemText(hWnd,IDC_RS_BKGCOLOR,str,8);
  404.    BOOL bOK=TRUE;
  405.    if(str[0]!='#')
  406.    {
  407.     str[0]='#';
  408.       bOK=FALSE;
  409.    }
  410. for(int i=1;i<7;i++)
  411.    {
  412.     if(str[i]>0x60 && str[i]<'g')
  413.        str[i]-=0x20;
  414.       else if((str[i]<'0' || (str[i]>'9' && str[i]<'A') || (str[i]>'F' && str[i]<'a') || str[i]>'f') && str[i]!='')
  415.       {
  416.        str[i]='0';
  417.          cr[i-1]=0;
  418.          bOK=FALSE;
  419.       }
  420.       if(str[i]=='')
  421.        cr[i-1]=0;
  422.       else if(str[i]>0x40 && str[i]<'G')
  423.        cr[i-1]=(DWORD)(str[i]-0x37);
  424.       else
  425.        cr[i-1]=(DWORD)(str[i]-0x30);
  426.    }
  427.    if(!bOK)
  428.    {
  429.     bEditCanUpdate=FALSE;
  430.    SetDlgItemText(hWnd,IDC_RS_BKGCOLOR,str);
  431.     bEditCanUpdate=TRUE;
  432.    }
  433.    rs_bgcolor=(COLORREF)((cr[4]<<20) | (cr[5]<<16) | (cr[2]<<12) | (cr[3] << 8) | (cr[0]<<4) | cr[1]);
  434. HWND hwnd=GetDlgItem(hWnd,IDC_RS_PREVBKGCOLOR);
  435. HDC hdc=GetDC(hwnd);
  436.    FillRect(hdc,&rt,CreateSolidBrush(rs_bgcolor));
  437.    ReleaseDC(hwnd,hdc);
  438. }