PROPBAR.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:15k
源码类别:

Windows编程

开发平台:

Visual C++

  1.     /****************************************************************************/
  2.     /*                                                                          */
  3.     /*                 Copyright (C) 1987-1996 Microsoft Corp.                */
  4.     /*                           All Rights Reserved                            */
  5.     /*                                                                          */
  6.     /****************************************************************************/
  7.     /****************************** Module Header *******************************
  8.     * Module Name: propbar.c
  9.     *
  10.     * Support for the Properties Bar.
  11.     *
  12.     * History:
  13.     *
  14.     ****************************************************************************/
  15.     
  16.     #include "imagedit.h"
  17.     #include "dialogs.h"
  18.  #include <windowsx.h>
  19.     
  20.     #define IMPOSSIBLEVALUE     0x7FFF
  21.     
  22.     STATICFN VOID NEAR PropBarProcessCommand(HWND hwnd, INT idCtrl,
  23.         INT NotifyCode);
  24.     
  25.     /*
  26.      * Cache variables.  These cache the values of some displayed fields.
  27.      * The field is updated only if the value changes, which avoids flicker.
  28.      * They are initialized to a large value so that the first "set" to
  29.      * any value will always cause the field to be updated.
  30.      */
  31.     static INT xSave = IMPOSSIBLEVALUE;
  32.     static INT ySave = IMPOSSIBLEVALUE;
  33.     static INT cxSave = IMPOSSIBLEVALUE;
  34.     static INT cySave = IMPOSSIBLEVALUE;
  35.     static INT xHotSpotSave = IMPOSSIBLEVALUE;
  36.     static INT yHotSpotSave = IMPOSSIBLEVALUE;
  37.     
  38.     
  39.     
  40.     /************************************************************************
  41.     * PropBarDlgProc
  42.     *
  43.     * This is the dialog procedure for the PropBar ribbon window.
  44.     *
  45.     * History:
  46.     *
  47.     ************************************************************************/
  48.     
  49.     DIALOGPROC PropBarDlgProc(
  50.         HWND hwnd,
  51.         UINT msg,
  52.         WPARAM wParam,
  53.         LPARAM lParam)
  54.     {
  55.         switch (msg) {
  56.             case WM_INITDIALOG:
  57.                 /*
  58.                  * Set this global right away.  Other routines that
  59.                  * might be called before CreateDialog returns depend
  60.                  * on this global.
  61.                  */
  62.                 ghwndPropBar = hwnd;
  63.     
  64.                 PropBarUpdate();
  65.     
  66.                 /*
  67.                  * Return TRUE so that the dialog manager does NOT
  68.                  * set the focus for me.  This prevents the PropBar
  69.                  * window from initially having the focus when the
  70.                  * editor is started.
  71.                  */
  72.                 return TRUE;
  73.     
  74.             case WM_PAINT:
  75.                 {
  76.                     HDC hdc;
  77.                     RECT rc;
  78.                     PAINTSTRUCT ps;
  79.                     HPEN hpenWindowFrame;
  80.     
  81.                     /*
  82.                      * Draw our border lines.
  83.                      */
  84.                     GetClientRect(hwnd, &rc);
  85.                     hdc = BeginPaint(hwnd, &ps);
  86.     
  87.                     SelectObject(hdc, GetStockObject(WHITE_PEN));
  88.                     MoveToEx(hdc, rc.left, rc.top, NULL);
  89.                     LineTo(hdc, rc.right, rc.top);
  90.     
  91.                     SelectObject(hdc, hpenDarkGray);
  92.                     MoveToEx(hdc, rc.left, (rc.top + gcyPropBar) - gcyBorder - 1, NULL);
  93.                     LineTo(hdc, rc.right, (rc.top + gcyPropBar) - gcyBorder - 1);
  94.     
  95.                     hpenWindowFrame = CreatePen(PS_SOLID, gcyBorder,
  96.                             GetSysColor(COLOR_WINDOWFRAME));
  97.                     SelectObject(hdc, hpenWindowFrame);
  98.                     MoveToEx(hdc, rc.left, (rc.top + gcyPropBar) - gcyBorder, NULL);
  99.                     LineTo(hdc, rc.right, (rc.top + gcyPropBar) - gcyBorder);
  100.     
  101.                     EndPaint(hwnd, &ps);
  102.                     DeleteObject(hpenWindowFrame);
  103.                 }
  104.     
  105.                 break;
  106.     
  107.             case WM_CTLCOLORBTN:
  108.             case WM_CTLCOLORDLG:
  109.             case WM_CTLCOLORLISTBOX:
  110.             case WM_CTLCOLORSTATIC:
  111.                 switch (GET_WM_CTLCOLOR_TYPE(wParam, lParam, msg)) {
  112.                     case CTLCOLOR_BTN:
  113.                     case CTLCOLOR_DLG:
  114.                     case CTLCOLOR_LISTBOX:
  115.                         return (BOOL)GetStockObject(LTGRAY_BRUSH);
  116.     
  117.                     case CTLCOLOR_STATIC:
  118.                         SetBkColor(GET_WM_CTLCOLOR_HDC(wParam, lParam, msg),
  119.                                 RGB_LIGHTGRAY);
  120.                         return (BOOL)GetStockObject(LTGRAY_BRUSH);
  121.                 }
  122.     
  123.                 return (BOOL)NULL;
  124.     
  125.             case WM_COMMAND:
  126.                 PropBarProcessCommand(hwnd,
  127.                         GET_WM_COMMAND_ID(wParam, lParam),
  128.                         GET_WM_COMMAND_CMD(wParam, lParam));
  129.                 break;
  130.     
  131.             case WM_DESTROY:
  132.                 /*
  133.                  * Null out the global window handle for the prop bar
  134.                  * for safety's sake.
  135.                  */
  136.                 ghwndPropBar = NULL;
  137.     
  138.                 break;
  139.     
  140.             default:
  141.                 return FALSE;
  142.         }
  143.     
  144.         return FALSE;
  145.     }
  146.     
  147.     
  148.     
  149.     /************************************************************************
  150.     * PropBarProcessCommand
  151.     *
  152.     *
  153.     * Arguments:
  154.     *   HWND hwnd        - The window handle.
  155.     *   INT idCtrl       - The id of the control the WM_COMMAND is for.
  156.     *   INT NotifyCode   - The control's notification code.
  157.     *
  158.     * History:
  159.     *
  160.     ************************************************************************/
  161.     
  162.     STATICFN VOID NEAR PropBarProcessCommand(
  163.         HWND hwnd,
  164.         INT idCtrl,
  165.         INT NotifyCode)
  166.     {
  167.         INT iSelect;
  168.         PIMAGEINFO pImage;
  169.     
  170.         switch (idCtrl) {
  171.             case DID_PROPBARIMAGE:
  172.                 if (NotifyCode == CBN_SELCHANGE) {
  173.                     if ((iSelect = (INT)SendDlgItemMessage(hwnd,
  174.                             DID_PROPBARIMAGE, CB_GETCURSEL, 0, 0L)) != CB_ERR) {
  175.                         /*
  176.                          * Get a pointer to the selected image (stored in the
  177.                          * listbox items data field).
  178.                          */
  179.                         pImage = (PIMAGEINFO)SendDlgItemMessage(hwnd,
  180.                                 DID_PROPBARIMAGE, CB_GETITEMDATA, iSelect, 0L);
  181.     
  182.                         /*
  183.                          * Open the image.  If it fails, be sure to set the
  184.                          * combobox selection back to the current image.
  185.                          */
  186.                         if (!ImageOpen(pImage)) {
  187.                             PropBarSetImage(gpImageCur);
  188.                             break;
  189.                         }
  190.     
  191.                         SetFocus(ghwndMain);
  192.                     }
  193.                 }
  194.     
  195.                 break;
  196.     
  197.             case IDOK:
  198.             case IDCANCEL:
  199.                 SetFocus(ghwndMain);
  200.                 break;
  201.         }
  202.     }
  203.     
  204.     
  205.     
  206.     /************************************************************************
  207.     * PropBarUpdate
  208.     *
  209.     * This function updates the Properties Bar for the selection of a
  210.     * new image or file.  It fills the Image combo with the names of
  211.     * the images in the current file and shows/hides the HotSpot display.
  212.     *
  213.     * History:
  214.     *
  215.     ************************************************************************/
  216.     
  217.     VOID PropBarUpdate(VOID)
  218.     {
  219.         HWND hwndCombo;
  220.         PIMAGEINFO pImage;
  221.         INT idLabel;
  222.         INT i;
  223.     
  224.         if (gpImageCur || gpszFileName) {
  225.             switch (giType) {
  226.                 case FT_BITMAP:
  227.                     idLabel = IDS_BITMAPIMAGELABEL;
  228.                     break;
  229.     
  230.                 case FT_ICON:
  231.                     idLabel = IDS_ICONIMAGELABEL;
  232.                     break;
  233.     
  234.                 case FT_CURSOR:
  235.                     idLabel = IDS_CURSORIMAGELABEL;
  236.                     break;
  237.             }
  238.         }
  239.         else {
  240.             idLabel = IDS_NULL;
  241.         }
  242.     
  243.         SetDlgItemText(ghwndPropBar, DID_PROPBARIMAGELABEL, ids(idLabel));
  244.     
  245.         /*
  246.          * Get the handle to the combo box and clear out all items.
  247.          */
  248.         hwndCombo = GetDlgItem(ghwndPropBar, DID_PROPBARIMAGE);
  249.         SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);
  250.     
  251.         /*
  252.          * Fill the combo box with the images.
  253.          */
  254.         for (pImage = gpImageHead; pImage; pImage = pImage->pImageNext) {
  255.             i = (INT)SendMessage(hwndCombo, CB_INSERTSTRING, (WPARAM)-1,
  256.                     pImage->pDevice ?
  257.                     (DWORD)(LPSTR)pImage->pDevice->szDesc :
  258.                     (DWORD)(LPSTR)ids(IDS_UNKNOWNIMAGEFORMAT));
  259.     
  260.             SendMessage(hwndCombo, CB_SETITEMDATA, i, (DWORD)(LPSTR)pImage);
  261.         }
  262.     
  263.         /*
  264.          * Select the current image.
  265.          */
  266.         PropBarSetImage(gpImageCur);
  267.     
  268.         /*
  269.          * Show/Hide the HotSpot info depending on whether this is
  270.          * a cursor or not.
  271.          */
  272.         if (giType == FT_CURSOR) {
  273.             if (gpImageCur)
  274.                 PropBarSetHotSpot(gpImageCur->iHotspotX, gpImageCur->iHotspotY);
  275.             else
  276.                 PropBarClearHotSpot();
  277.     
  278.             PropBarShowHotSpot(TRUE);
  279.         }
  280.         else {
  281.             PropBarShowHotSpot(FALSE);
  282.         }
  283.     }
  284.     
  285.     
  286.     
  287.     /************************************************************************
  288.     * PropBarSetImage
  289.     *
  290.     *
  291.     * History:
  292.     *
  293.     ************************************************************************/
  294.     
  295.     VOID PropBarSetImage(
  296.         PIMAGEINFO pImage)
  297.     {
  298.         if (pImage)
  299.             SendDlgItemMessage(ghwndPropBar, DID_PROPBARIMAGE, CB_SELECTSTRING,
  300.                     (WPARAM)-1, (DWORD)(LPSTR)pImage->pDevice->szDesc);
  301.         else
  302.             SendDlgItemMessage(ghwndPropBar, DID_PROPBARIMAGE, CB_SETCURSEL,
  303.                     (WPARAM)-1, 0);
  304.     }
  305.     
  306.     
  307.     
  308.     /************************************************************************
  309.     * PropBarSetPos
  310.     *
  311.     *
  312.     * Arguments:
  313.     *
  314.     * History:
  315.     *
  316.     ************************************************************************/
  317.     
  318.     VOID PropBarSetPos(
  319.         INT x,
  320.         INT y)
  321.     {
  322.         CHAR szBuf[CCHTEXTMAX];
  323.     
  324.         if (x != xSave || y != ySave) {
  325.             wsprintf(szBuf, "%d, %d", x, y);
  326.             SetDlgItemText(ghwndPropBar, DID_PROPBARPOS, szBuf);
  327.     
  328.             /*
  329.              *  Save them for the next time.
  330.              */
  331.             xSave = x;
  332.             ySave = y;
  333.         }
  334.     }
  335.     
  336.     
  337.     
  338.     /************************************************************************
  339.     * PropBarClearPos
  340.     *
  341.     *
  342.     * Arguments:
  343.     *
  344.     * History:
  345.     *
  346.     ************************************************************************/
  347.     
  348.     VOID PropBarClearPos(VOID)
  349.     {
  350.         SetDlgItemText(ghwndPropBar, DID_PROPBARPOS, "");
  351.     
  352.         /*
  353.          * Reset the cache variables so that the next "set" of
  354.          * the position is sure to update the display fields.
  355.          */
  356.         xSave = IMPOSSIBLEVALUE;
  357.         ySave = IMPOSSIBLEVALUE;
  358.     }
  359.     
  360.     
  361.     
  362.     /************************************************************************
  363.     * PropBarSetSize
  364.     *
  365.     *
  366.     * Arguments:
  367.     *
  368.     * History:
  369.     *
  370.     ************************************************************************/
  371.     
  372.     VOID PropBarSetSize(
  373.         POINT pt1,
  374.         POINT pt2)
  375.     {
  376.         CHAR szBuf[CCHTEXTMAX];
  377.         INT cx;
  378.         INT cy;
  379.     
  380.         NormalizePoints(&pt1, &pt2);
  381.         cx = ((pt2.x - pt1.x) / gZoomFactor) + 1;
  382.         cy = ((pt2.y - pt1.y) / gZoomFactor) + 1;
  383.     
  384.         if (cx != cxSave || cy != cySave) {
  385.             wsprintf(szBuf, "%dx%d", cx, cy);
  386.             SetDlgItemText(ghwndPropBar, DID_PROPBARSIZE, szBuf);
  387.     
  388.             /*
  389.              *  Save them for the next time.
  390.              */
  391.             cxSave = cx;
  392.             cySave = cy;
  393.         }
  394.     }
  395.     
  396.     
  397.     
  398.     /************************************************************************
  399.     * PropBarClearSize
  400.     *
  401.     *
  402.     * Arguments:
  403.     *
  404.     * History:
  405.     *
  406.     ************************************************************************/
  407.     
  408.     VOID PropBarClearSize(VOID)
  409.     {
  410.         SetDlgItemText(ghwndPropBar, DID_PROPBARSIZE, "");
  411.     
  412.         /*
  413.          * Reset the cache variables so that the next "set" of
  414.          * the position is sure to update the display fields.
  415.          */
  416.         cxSave = IMPOSSIBLEVALUE;
  417.         cySave = IMPOSSIBLEVALUE;
  418.     }
  419.     
  420.     
  421.     
  422.     /************************************************************************
  423.     * PropBarSetHotSpot
  424.     *
  425.     *
  426.     * Arguments:
  427.     *
  428.     * History:
  429.     *
  430.     ************************************************************************/
  431.     
  432.     VOID PropBarSetHotSpot(
  433.         INT xHotSpot,
  434.         INT yHotSpot)
  435.     {
  436.         CHAR szBuf[CCHTEXTMAX];
  437.     
  438.         if (xHotSpot != xHotSpotSave || yHotSpot != yHotSpotSave) {
  439.             wsprintf(szBuf, "%d, %d", xHotSpot, yHotSpot);
  440.             SetDlgItemText(ghwndPropBar, DID_PROPBARHOTSPOT, szBuf);
  441.     
  442.             /*
  443.              *  Save them for the next time.
  444.              */
  445.             xHotSpotSave = xHotSpot;
  446.             yHotSpotSave = yHotSpot;
  447.         }
  448.     }
  449.     
  450.     
  451.     
  452.     /************************************************************************
  453.     * PropBarClearHotSpot
  454.     *
  455.     *
  456.     * Arguments:
  457.     *
  458.     * History:
  459.     *
  460.     ************************************************************************/
  461.     
  462.     VOID PropBarClearHotSpot(VOID)
  463.     {
  464.         SetDlgItemText(ghwndPropBar, DID_PROPBARHOTSPOT, "");
  465.     
  466.         /*
  467.          * Reset the cache variables so that the next "set" of
  468.          * the hotspot is sure to update the display fields.
  469.          */
  470.         xHotSpotSave = IMPOSSIBLEVALUE;
  471.         yHotSpotSave = IMPOSSIBLEVALUE;
  472.     }
  473.     
  474.     
  475.     
  476.     /************************************************************************
  477.     * PropBarShowHotSpot
  478.     *
  479.     *
  480.     * Arguments:
  481.     *
  482.     * History:
  483.     *
  484.     ************************************************************************/
  485.     VOID PropBarShowHotSpot(
  486.         BOOL fShow)
  487.     {
  488.         ShowWindow(GetDlgItem(ghwndPropBar, DID_PROPBARHOTSPOTLABEL),
  489.                 fShow ? SW_SHOWNA : SW_HIDE);
  490.         ShowWindow(GetDlgItem(ghwndPropBar, DID_PROPBARHOTSPOT),
  491.                 fShow ? SW_SHOWNA : SW_HIDE);
  492.     }