⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colorshow.cpp

📁 是一个较好的缩放工具
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   {
    CPaintDC dc(this); // device context for painting
    if(bmp.m_hObject != NULL)
       { /* has image */
        CDC memDC;
        memDC.CreateCompatibleDC(&dc);
        memDC.SelectObject(&bmp);

        CRect r;
        GetClientRect(&r);
        CBitmap target;
        target.CreateCompatibleBitmap(&dc, r.Width(), r.Height());
        dc.SelectObject(&target);

        CSize sz = GetSize();
        dc.StretchBlt(0, 0, sz.cx, sz.cy, &memDC, 0, 0, dx, dy, SRCCOPY);

        if(!select)
           { /* show pixel */
            CPen fiducial(PS_DOT, 0, RGB(0, 0, 0));
            CArray<CPoint, CPoint&> points;
            // The lines will first be computed in logical points, then converted to
            // device points. Then they will be drawn. Each pair of entries in the
            // array are a MoveTo/LineTo pair
            
            //                                       + 0 (dx/2, 0)
            //                                       |
            //                                       |
            //                                       |
            //                                       |
            //                                       |
            //                                       | 1  (dx/2, dy/2)
            //                               10,8+---+---+9,12
            //                                   |       |
            // 4+--------------------------------+5     6+--------------------------------+7
            //                                   |       |
            //                              11,14+---+---+15,13
            //                                       | 2
            //                                       |
            //                                       |
            //                                       |
            //                                       |
            //                                       |
            //                                       + 3

            points.Add(CPoint(      dx / 2,       0     ));     // 0 +.5x
            points.Add(CPoint(        dx/2,  dy / 2     ));     // 1 +.5x
            points.Add(CPoint(      dx / 2, (dy / 2) + 1));     // 2 +.5x
            points.Add(CPoint(      dx / 2,  dy + 1     ));     // 3 +.5x
            points.Add(CPoint(           0,  dy / 2     ));     // 4 +.5y
            points.Add(CPoint(      dx / 2,  dy / 2     ));     // 5 +.5y
            points.Add(CPoint((dx / 2) + 1,  dy / 2     ));     // 6 +.5y
            points.Add(CPoint(      dx + 1,  dy / 2     ));     // 7 +.5y
            points.Add(CPoint(      dx / 2, (dy / 2)    ));     // 8
            points.Add(CPoint((dx / 2) + 1, (dy / 2)    ));     // 9
            points.Add(CPoint(      dx / 2, (dy / 2)    ));     //10
            points.Add(CPoint(      dx / 2, (dy / 2) + 1));     //11
            points.Add(CPoint((dx / 2) + 1, (dy / 2)    ));     //12
            points.Add(CPoint((dx / 2) + 1, (dy / 2) + 1));     //13
            points.Add(CPoint(      dx / 2, (dy / 2) + 1));     //14
            points.Add(CPoint((dx / 2) + 1, (dy / 2) + 1));     //15

            // We will need the 'half' to adjust the fiducial lines after we've
            // converted to device space, otherwise we can't get them to look
            // like what we want.
            CPoint half(1, 1);

            int save = dc.SaveDC();
            PrepareDC(dc);

            // Convert coordinates to device space
            dc.LPtoDP(points.GetData(), points.GetSize());
            dc.LPtoDP(&half);
            dc.RestoreDC(save);
            // The coordinates are now in device coordinates. Adjust for
            // half-coordinates for the vertical and horizontal lines
            half.x /= 2;
            half.y /= 2;
            points[0].x += half.x;
            points[1].x += half.x;
            points[2].x += half.x;
            points[3].x += half.x;
            points[4].y += half.y;
            points[5].y += half.y;
            points[6].y += half.y;
            points[7].y += half.y;

            dc.SelectObject(&fiducial);
            // For each pair of points, do a LineTo/Moveto to
            // draw each of the lines of the desired display
            for(int i = 0; i < points.GetSize(); i += 2)
               { /* draw each line */
                dc.MoveTo(points[i]);
                dc.LineTo(points[i + 1]);
               } /* draw each line */
           } /* show pixel */

        // If there is a selection, draw the outline
        if(!selection.IsRectEmpty())
           { /* has selection */
            CPen outline(PS_DOT, 0, RGB(0, 0, 0));
            
            CRect sel = selection;
            int save = dc.SaveDC();
            PrepareDC(dc);
            dc.SelectObject(&outline);
            dc.SelectStockObject(HOLLOW_BRUSH);
            dc.Rectangle(&sel);
            dc.RestoreDC(save);
           } /* has selection */
       } /* has image */
        
    // Do not call CStatic::OnPaint() for painting messages
   }

/****************************************************************************
*                         CColorShow::RecomputeImage
* Result: void
*       
* Effect: 
*       Recomputes the image. Figures out where it is on the screen
*       and captures the screen bitmap.
****************************************************************************/

void CColorShow::RecomputeImage()
    {
     CPoint pt;
     pt.x = where.x - dx / 2;
     pt.y = where.y - dy / 2;

     CDC sourceDC ;
     sourceDC.Attach(::GetWindowDC(NULL)); // get entire window

     CDC memDC;

     color = sourceDC.GetPixel(where);
     GetParent()->SendMessage(UWM_POINT, (WPARAM)MAKELONG(where.x, where.y), (LPARAM)color);
     memDC.CreateCompatibleDC(&sourceDC);

     if(bmp.m_hObject == NULL)
        bmp.CreateCompatibleBitmap(&sourceDC, dx, dy);

     int save = memDC.SaveDC();

     memDC.SelectObject(&bmp);
     memDC.BitBlt(0, 0, dx, dy, &sourceDC, pt.x, pt.y, SRCCOPY);

     memDC.RestoreDC(save);
     ::ReleaseDC(NULL, sourceDC.Detach());
     Invalidate();
    } // CColorShow::RecomputeImage

/****************************************************************************
*                             CColorShow::SetSize
* Inputs:
*       int n: Desired size to set
* Result: void
*       
* Effect: 
*       Sets the size of the capture area.
****************************************************************************/

void CColorShow::SetSize(int n)
    {
     dx = n;
     dy = n;
     if(bmp.m_hObject != NULL)
        bmp.DeleteObject();
     RecomputeImage(); 
    } // CColorShow::SetSize

/****************************************************************************
*                            CColorShow::AdjustPos
* Inputs:
*       int dx: Change in x
*       int dy: Change in y
* Result: void
*       
* Effect: 
*       Computes new x,y to "nudge" the image around.
****************************************************************************/

void CColorShow::AdjustPos(int dx, int dy)
    {
     where.x += dx;
     where.y += dy;
     RecomputeImage();
    } // CColorShow::AdjustPos

/****************************************************************************
*                              CColorShow::Copy
* Result: void
*       
* Effect: 
*       Copies the real bitmap (not the expanded image) to the clipboard
****************************************************************************/

void CColorShow::Copy()
    {
     if(bmp.m_hObject == NULL)
        return;

     CDC sourceDC;
     sourceDC.CreateCompatibleDC(NULL);
     sourceDC.SelectObject(&bmp);
     CDC targetDC;
     targetDC.CreateCompatibleDC(&sourceDC);

     CBitmap local;
     // Create a newbitmap which is the correct bitmap.
     // If there is no selection, it is the entire bitmap capture
     // If there is a selection, the selection is extracted.

     if(selection.IsRectEmpty())
        { /* local copy */
         local.CreateCompatibleBitmap(&sourceDC, dx, dy);
         targetDC.SelectObject(&local);
         targetDC.BitBlt(0, 0, dx, dy, &sourceDC, 0, 0, SRCCOPY);
        } /* local copy */
     else
        { /* subselection */
         local.CreateCompatibleBitmap(&sourceDC, selection.Width(), selection.Height());
         targetDC.SelectObject(&local);
         targetDC.BitBlt(0, 0, selection.Width(), selection.Height(), &sourceDC, selection.left, selection.top, SRCCOPY);
        } /* subselection */
     // Store it in the clipboard.
     OpenClipboard();
     ::EmptyClipboard();
     ::SetClipboardData(CF_BITMAP, local.m_hObject);
     local.Detach();
     ::CloseClipboard();
    } // CColorShow::Copy

/****************************************************************************
*                              CColorShow::Show
* Result: void
*       
* Effect: 
*       Shows a window that illustrates the source of the bitmap and
*       if there is a selection, a smaller rectangle showing the selection
*       is also shown.
****************************************************************************/

void CColorShow::Show()
    {
     CShower * shower = new CShower;
     CRect r;
     r.left = where.x - dx / 2;
     r.top = where.y - dy / 2;
     r.right = r.left + dx;
     r.bottom = r.top + dy;
     // Note that we have to register a class so we can create
     // a popup window. We can't use NULL for a classname
     // or there is an assertion error.
     LPCTSTR classname = AfxRegisterWndClass(0);

     shower->CreateEx(WS_EX_TRANSPARENT,   // extended style
                     classname,// classname
                     NULL,// caption
                     WS_POPUP | WS_VISIBLE,
                     r,
                     this,
                     0,
                     &selection);
    } // CColorShow::Show

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -