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

Windows编程

开发平台:

Visual C++

  1.     /****************************************************************************/
  2.     /*                                                                          */
  3.     /*                 Copyright (C) 1987-1996 Microsoft Corp.                */
  4.     /*                           All Rights Reserved                            */
  5.     /*                                                                          */
  6.     /****************************************************************************/
  7.     /****************************** Module Header *******************************
  8.     * Module Name: viewwp.c
  9.     *
  10.     * Contains routines that handle the View window.
  11.     *
  12.     * History:
  13.     *
  14.     * 07/17/91 -  - Created.
  15.     *
  16.     ****************************************************************************/
  17.     
  18.     #include "imagedit.h"
  19.     #include "dialogs.h"
  20.  #include <windowsx.h>
  21.     
  22.     /*
  23.      * Style of the view window. */
  24.     #define VIEWSTYLE       (WS_POPUP | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU)
  25.     
  26.     
  27.     STATICFN VOID NEAR ViewChar(UINT uiChar);
  28.     
  29.     
  30.     static INT gViewBackMargin;     // Margin of background within view window.
  31.     
  32.     
  33.     
  34.     /****************************************************************************
  35.     * ViewCreate
  36.     *
  37.     * This function creates the View window.
  38.     *
  39.     * History:
  40.     *
  41.     ****************************************************************************/
  42.     
  43.     VOID ViewCreate(VOID)
  44.     {
  45.         INT x;
  46.         INT y;
  47.         INT cxDummy;
  48.         INT cyDummy;
  49.         RECT rc;
  50.         BOOL fMaximized;
  51.     
  52.         gViewBackMargin = GetSystemMetrics(SM_CXVSCROLL) / 2;
  53.     
  54.         /*
  55.          * Get the saved position of the Toolbox.  Note that we throw away
  56.          * the size fields, because we will calculate the required size
  57.          * later based on the image size.
  58.          */
  59.         if (!ReadWindowPos(szViewPos, &x, &y, &cxDummy, &cyDummy, &fMaximized)) {
  60.             /*
  61.              * The previous position of the View window couldn't be found.
  62.              * Position the window to the right side of the editor, just
  63.              * below the Toolbox.
  64.              */
  65.             if (ghwndToolbox) {
  66.                 GetWindowRect(ghwndToolbox, &rc);
  67.                 x = rc.left;
  68.                 y = rc.bottom + (2 * PALETTEMARGIN);
  69.             }
  70.             else {
  71.                 /*
  72.                  * Last resort.  Position it in the upper left corner
  73.                  * of the screen if the Toolbox cannot be found.  This
  74.                  * is unlikely because if the previous position of the
  75.                  * View window could not be found, this implies that
  76.                  * the editor has not been run before on this machine.
  77.                  * If this is true, the toolbox will come up by default
  78.                  * before this routine is called.  But just in case...
  79.                  */
  80.                 x = 2 * PALETTEMARGIN;
  81.                 y = 2 * PALETTEMARGIN;
  82.             }
  83.         }
  84.     
  85.         if (!(ghwndView = CreateWindow(szViewClass, NULL, VIEWSTYLE,
  86.                 x, y, 0, 0, ghwndMain, NULL, ghInst, NULL)))
  87.             return;
  88.     }
  89.     
  90.     
  91.     
  92.     /****************************************************************************
  93.     * ViewShow
  94.     *
  95.     * This function shows or hides the view window.
  96.     *
  97.     * History:
  98.     *
  99.     ****************************************************************************/
  100.     
  101.     VOID ViewShow(
  102.         BOOL fShow)
  103.     {
  104.         if (fShow) {
  105.             /*
  106.              * Only show it if there is an image to display!
  107.              */
  108.             if (gpImageCur)
  109.                 ShowWindow(ghwndView, SW_SHOWNA);
  110.         }
  111.         else {
  112.             ShowWindow(ghwndView, SW_HIDE);
  113.         }
  114.     }
  115.     
  116.     
  117.     
  118.     /****************************************************************************
  119.     * ViewUpdate
  120.     *
  121.     * This function updates the view window.  It should be called any time that
  122.     * the image changes (is drawn upon).
  123.     *
  124.     * History:
  125.     *
  126.     ****************************************************************************/
  127.     
  128.     VOID ViewUpdate(VOID)
  129.     {
  130.         InvalidateRect(ghwndView, NULL, TRUE);
  131.     
  132.         /*
  133.          * Update the workspace window also, because it must always
  134.          * match the state of the View window.
  135.          */
  136.         WorkUpdate();
  137.     }
  138.     
  139.     
  140.     
  141.     /****************************************************************************
  142.     * ViewReset
  143.     *
  144.     * This function resets the view window, sizing it to fit a new
  145.     * image.  It should be called any time that the current image
  146.     * is changed to another one.
  147.     *
  148.     * History:
  149.     *
  150.     ****************************************************************************/
  151.     
  152.     VOID ViewReset(VOID)
  153.     {
  154.         RECT rc;
  155.         RECT rcT;
  156.     
  157.         GetWindowRect(ghwndView, &rc);
  158.     
  159.         rcT.left = 0;
  160.         rcT.top = 0;
  161.         rcT.right = PALETTEMARGIN + gViewBackMargin +
  162.                 gpImageCur->cx + gViewBackMargin + PALETTEMARGIN;
  163.         rcT.bottom = PALETTEMARGIN + gViewBackMargin +
  164.                 gpImageCur->cy + gViewBackMargin + PALETTEMARGIN;
  165.         AdjustWindowRect(&rcT, VIEWSTYLE, FALSE);
  166.     
  167.         rc.right = rc.left + (rcT.right - rcT.left);
  168.         rc.bottom = rc.top + (rcT.bottom - rcT.top);
  169.         FitRectToScreen(&rc);
  170.     
  171.         SetWindowPos(ghwndView, NULL, rc.left, rc.top,
  172.                 rc.right - rc.left, rc.bottom - rc.top,
  173.                 SWP_NOACTIVATE | SWP_NOZORDER);
  174.     
  175.         /*
  176.          * If the user wants it, show the View window now.
  177.          */
  178.         if (gfShowView)
  179.             ViewShow(TRUE);
  180.     
  181.         ViewUpdate();
  182.     
  183.         /*
  184.          * Clear out the propbar size and position fields, because they
  185.          * probably show the wrong information now.
  186.          */
  187.         PropBarClearPos();
  188.         PropBarClearSize();
  189.     }
  190.     
  191.     
  192.     
  193.     /****************************************************************************
  194.     * ViewWndProc
  195.     *
  196.     * This is the window procedure for the view window.
  197.     *
  198.     * History:
  199.     *
  200.     ****************************************************************************/
  201.     
  202.     WINDOWPROC ViewWndProc(
  203.         HWND hwnd,
  204.         UINT msg,
  205.         WPARAM wParam,
  206.         LPARAM lParam)
  207.     {
  208.         switch (msg) {
  209.             case WM_CREATE:
  210.                 {
  211.                     HMENU hmenu = GetSystemMenu(hwnd, FALSE);
  212.     
  213.                     RemoveMenu(hmenu, 7, MF_BYPOSITION);    // Second separator.
  214.                     RemoveMenu(hmenu, 5, MF_BYPOSITION);    // First separator.
  215.     
  216.                     RemoveMenu(hmenu, SC_RESTORE, MF_BYCOMMAND);
  217.                     RemoveMenu(hmenu, SC_SIZE, MF_BYCOMMAND);
  218.                     RemoveMenu(hmenu, SC_MINIMIZE, MF_BYCOMMAND);
  219.                     RemoveMenu(hmenu, SC_MAXIMIZE, MF_BYCOMMAND);
  220.                     RemoveMenu(hmenu, SC_TASKLIST, MF_BYCOMMAND);
  221.                 }
  222.     
  223.                 return 0;
  224.     
  225.             case  WM_PAINT:
  226.                 {
  227.                     HDC hdc;
  228.                     PAINTSTRUCT ps;
  229.                     HBRUSH hbrOld;
  230.                     RECT rc;
  231.     
  232.                     hdc = BeginPaint(hwnd, &ps);
  233.     
  234.                     /*
  235.                      * The view window should not be showing if there
  236.                      * is not an image to view!
  237.                      */
  238.                     if (gpImageCur) {
  239.                         DrawMarginBorder(hwnd, hdc);
  240.     
  241.                         GetClientRect(hwnd, &rc);
  242.                         hbrOld = SelectObject(hdc, ghbrScreen);
  243.                         PatBlt(hdc, PALETTEMARGIN + 1, PALETTEMARGIN + 1,
  244.                                 rc.right - (PALETTEMARGIN * 2) - 2,
  245.                                 rc.bottom - (PALETTEMARGIN * 2) - 2,
  246.                                 PATCOPY);
  247.                         SelectObject(hdc, hbrOld);
  248.     
  249.                         BitBlt(hdc, PALETTEMARGIN + gViewBackMargin,
  250.                                 PALETTEMARGIN + gViewBackMargin,
  251.                                 gcxImage, gcyImage, ghdcImage, 0, 0, SRCCOPY);
  252.                     }
  253.     
  254.                     EndPaint(hwnd, &ps);
  255.                 }
  256.     
  257.                 break;
  258.     
  259.             case WM_ACTIVATE:
  260.                 if (GET_WM_ACTIVATE_STATE(wParam, lParam))
  261.                     gidCurrentDlg = DID_VIEW;
  262.     
  263.                 break;
  264.     
  265.             case WM_LBUTTONDOWN:
  266.                 SetScreenColor(gargbCurrent[giColorLeft]);
  267.                 break;
  268.     
  269.             case WM_CHAR:
  270.                 ViewChar(wParam);
  271.                 break;
  272.     
  273.             case WM_CLOSE:
  274.                 /*
  275.                  * The user closed the view window from the system menu.
  276.                  * Hide the window (we don't actually destroy it so
  277.                  * that it will appear in the same spot when they show
  278.                  * it again).
  279.                  */
  280.                 ViewShow(FALSE);
  281.                 gfShowView = FALSE;
  282.                 break;
  283.     
  284.             case WM_DESTROY:
  285.                 {
  286.                     RECT rc;
  287.     
  288.                     /*
  289.                      * Save the position of the toolbox.
  290.                      */
  291.                     GetWindowRect(hwnd, &rc);
  292.                     WriteWindowPos(&rc, FALSE, szViewPos);
  293.     
  294.                     /*
  295.                      * Null out the global window handle for the view window
  296.                      * for safety's sake.
  297.                      */
  298.                     ghwndView = NULL;
  299.                 }
  300.     
  301.                 break;
  302.     
  303.             default:
  304.                 return DefWindowProc(hwnd, msg, wParam, lParam);
  305.         }
  306.     
  307.         return 0;
  308.     }
  309.     
  310.     
  311.     
  312.     /************************************************************************
  313.     * ViewChar
  314.     *
  315.     * Handles WM_CHAR messages for the view window.  Currently this just
  316.     * includes the '+' and '-' keys, which are used to cycle through all
  317.     * the possible screen colors.
  318.     *
  319.     * Arguments:
  320.     *
  321.     * History:
  322.     *
  323.     ************************************************************************/
  324.     
  325.     STATICFN VOID NEAR ViewChar(
  326.         UINT uiChar)
  327.     {
  328.         INT i;
  329.         INT iNext;
  330.     
  331.         switch (uiChar) {
  332.             /*
  333.              * Advance to the next screen color.
  334.              */
  335.             case '+':
  336.                 iNext = 0;
  337.                 for (i = 0; i < 16; i++) {
  338.                     if (grgbScreen == gargbDefaultColor[i]) {
  339.                         iNext = i + 1;
  340.                         break;
  341.                     }
  342.                 }
  343.     
  344.                 if (iNext >= 16)
  345.                     iNext = 0;
  346.     
  347.                 SetScreenColor(gargbDefaultColor[iNext]);
  348.     
  349.                 break;
  350.     
  351.             /*
  352.              * Back up to the prior screen color.
  353.              */
  354.             case '-':
  355.                 iNext = 16 - 1;
  356.                 for (i = 0; i < 16; i++) {
  357.                     if (grgbScreen == gargbDefaultColor[i]) {
  358.                         iNext = i - 1;
  359.                         break;
  360.                     }
  361.                 }
  362.     
  363.                 if (iNext < 0)
  364.                     iNext = 16 - 1;
  365.     
  366.                 SetScreenColor(gargbDefaultColor[iNext]);
  367.     
  368.                 break;
  369.         }
  370.     }
  371.     
  372.     
  373.     
  374.     /****************************************************************************
  375.     * ViewSetPixel
  376.     *
  377.     * This function colors a pixel in the View window directly.  It is
  378.     * provided as an optimization when drawing a point.  The ghdcImage
  379.     * bitmap must be updated as well or the image on the screen will
  380.     * get out of synch with it.
  381.     *
  382.     * History:
  383.     *
  384.     ****************************************************************************/
  385.     
  386.     VOID ViewSetPixel(
  387.         INT x,
  388.         INT y,
  389.         INT nBrushSize)
  390.     {
  391.         HDC hDC;
  392.         HBRUSH hbrOld;
  393.         INT Size;
  394.         INT SizeX;
  395.         INT SizeY;
  396.     
  397.         hDC = GetDC(ghwndView);
  398.         hbrOld = SelectObject(hDC, ghbrDrawSolid);
  399.         SizeX = x - nBrushSize / 2;
  400.         SizeY = y - nBrushSize / 2;
  401.         PatBlt(hDC, PALETTEMARGIN + gViewBackMargin + (SizeX >= 0 ? SizeX : 0),
  402.                 PALETTEMARGIN + gViewBackMargin + (SizeY >=  0 ? SizeY :  0),
  403.                 ((Size = gcxImage - SizeX) >= nBrushSize ?
  404.                 nBrushSize : Size),
  405.                 ((Size = gcyImage - SizeY) >= nBrushSize ?
  406.                 nBrushSize : Size), PATCOPY);
  407.         SelectObject(hDC, hbrOld);
  408.         ReleaseDC(ghwndView, hDC);
  409.     }
  410.     
  411.     
  412.     
  413.     /****************************************************************************
  414.     * DrawMarginBorder
  415.     *
  416.     *
  417.     * History:
  418.     *
  419.     ****************************************************************************/
  420.     
  421.     VOID DrawMarginBorder(
  422.         HWND hwnd,
  423.         HDC hdc)
  424.     {
  425.         HBRUSH hbrOld;
  426.         HPEN hpenOld;
  427.         RECT rc;
  428.     
  429.         GetClientRect(hwnd, &rc);
  430.         hpenOld = SelectObject(hdc, GetStockObject(BLACK_PEN));
  431.         hbrOld = SelectObject(hdc, GetStockObject(NULL_BRUSH));
  432.         Rectangle(hdc, PALETTEMARGIN, PALETTEMARGIN,
  433.                 rc.right - PALETTEMARGIN,
  434.                 rc.bottom - PALETTEMARGIN);
  435.         SelectObject(hdc, hpenOld);
  436.         SelectObject(hdc, hbrOld);
  437.     }
  438.     
  439.     
  440.     
  441.     /****************************************************************************
  442.     * DrawSunkenRect
  443.     *
  444.     *
  445.     * History:
  446.     *
  447.     ****************************************************************************/
  448.     
  449.     VOID DrawSunkenRect(
  450.         PRECT prc,
  451.         HDC hdc)
  452.     {
  453.         HPEN hpenOld;
  454.     
  455.         hpenOld = SelectObject(hdc, hpenDarkGray);
  456.         MoveToEx(hdc, prc->left, prc->top, NULL);
  457.         LineTo(hdc, prc->right - 1, prc->top);
  458.         MoveToEx(hdc, prc->left, prc->top, NULL);
  459.         LineTo(hdc, prc->left, prc->bottom - 1);
  460.     
  461.         SelectObject(hdc, GetStockObject(WHITE_PEN));
  462.         MoveToEx(hdc, prc->left + 1, prc->bottom - 1, NULL);
  463.         LineTo(hdc, prc->right, prc->bottom - 1);
  464.         MoveToEx(hdc, prc->right - 1, prc->top + 1, NULL);
  465.         LineTo(hdc, prc->right - 1, prc->bottom - 1);
  466.     
  467.         SelectObject(hdc, hpenOld);
  468.     }