?? colorshow.cpp
字號:
// ColorShow.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "ColorShow.h"
#include "Picker.h"
#include "Shower.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
DECLARE_MESSAGE(UWM_POINT)
DECLARE_MESSAGE(UWM_PICKER)
/////////////////////////////////////////////////////////////////////////////
// CColorShow
CColorShow::CColorShow()
{
dx = 16;
dy = 16;
dropper = ::LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_PICKER));
selection.SetRectEmpty();
}
CColorShow::~CColorShow()
{
}
BEGIN_MESSAGE_MAP(CColorShow, CStatic)
//{{AFX_MSG_MAP(CColorShow)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorShow message handlers
/****************************************************************************
* CColorShow::StartPick
* Inputs:
* CPoint pt: Current point
* Result: void
*
* Effect:
* Starts the pick operation. Initiates capture mode, changes the
* cursor to the pickup cursor, and indicates by setting select to
* FALSE that we are in pickup mode, not selection mode.
****************************************************************************/
void CColorShow::StartPick(CPoint pt)
{
SetCapture();
::SetCursor(dropper);
select = FALSE;
} // CColorShow::StartPick
/****************************************************************************
* CColorShow::OnLButtonDown
* Inputs:
* UINT nFlags:
* CPoint point:
* Result: void
*
* Effect:
* Starts a selection operation. Sets capture mode and indicates that
* the mode is "select" mode, which influences how mouse-moves are handled
****************************************************************************/
void CColorShow::OnLButtonDown(UINT nFlags, CPoint point)
{
SetCapture();
CRect r;
GetClientRect(&r);
previous = anchor = point;
select = TRUE;
CStatic::OnLButtonDown(nFlags, point);
}
/****************************************************************************
* CColorShow::MakeSelection
* Result: CRect
* A normalized rectangle for the current selection
****************************************************************************/
CRect CColorShow::MakeSelection()
{
CRect r;
r.left = previous.x;
r.top = previous.y;
r.right = anchor.x;
r.bottom = anchor.y;
r.NormalizeRect();
return r;
} // CColorShow::MakeSelection
/****************************************************************************
* CColorShow::DrawSelection
* Inputs:
* CDC & dc: DC to use
* Result: void
*
* Effect:
* Draws a focus rectangle.
****************************************************************************/
void CColorShow::DrawSelection(CDC & dc)
{
CRect r = MakeSelection();
dc.DrawFocusRect(&r);
} // CColorShow::DrawSelection
/****************************************************************************
* CColorShow::OnMouseMove
* Inputs:
* UINT nFlags: ignored
* CPoint point: The point where the mouse is
* Result: void
*
* Effect:
* The point is in client coordinates, but because we are in mouse
* capture mode, these could well be outside the client area of the window.
* If the mouse was clicked in the window, indicating a sub-selection is
* being done, a selection rectangle is rubber-banded. If it is in
* pickup mode, with the picker activated, the coordinates are converted
* to screen coordinates and the image is recaptured.
****************************************************************************/
void CColorShow::OnMouseMove(UINT nFlags, CPoint point)
{
if(GetCapture())
{ /* has capture */
if(select)
{ /* selection */
CClientDC dc(this);
DrawSelection(dc);
previous = point;
DrawSelection(dc);
} /* selection */
else
{ /* pickup */
where = point;
ClientToScreen(&where);
RecomputeImage();
} /* pickup */
} /* has capture */
else
{ /* no capture */
// Simply report the pixel under the point. This is the pixel
// under the cursor as it moves in the exploded selection
CClientDC sourceDC(this);
color = sourceDC.GetPixel(point);
GetParent()->SendMessage(UWM_POINT, 0, (LPARAM)color);
} /* no capture */
CStatic::OnMouseMove(nFlags, point);
}
/****************************************************************************
* CColorShow::OnLButtonUp
* Inputs:
* UINT nFlags: ignored
* UINT CPoint: Point at which the button is release
* Result: void
*
* Effect:
* If we are the dragging mode, releases capture. If it was an internal
* selection, creates a logical selection rectangle which is later
* converted to device space to do the drawing. If an external search,
* notifies the parent window that dragging has ended.
****************************************************************************/
void CColorShow::OnLButtonUp(UINT nFlags, CPoint point)
{
if(GetCapture() != NULL)
{ /* had capture */
ReleaseCapture();
if(select)
{ /* selection */
CClientDC dc(this);
DrawSelection(dc); // erase the selection
CRect r = MakeSelection(); // create a selection rectangle
selection = MapSelection(r);
Invalidate();
} /* selection */
else
{ /* pickup */
GetParent()->SendMessage(UWM_PICKER, (WPARAM)FALSE); // indicate release
selection.SetRectEmpty();
} /* pickup */
} /* had capture */
CStatic::OnLButtonUp(nFlags, point);
}
/****************************************************************************
* CColorShow::GetSize
* Result: CSize
* The size to use for computations
****************************************************************************/
CSize CColorShow::GetSize()
{
CRect r;
GetClientRect(&r);
CSize sz(r.Width() + 1, r.Height() + 1);
return sz;
} // CColorShow::GetSize
/****************************************************************************
* CColorShow::PrepareDC
* Inputs:
* CDC & dc: DC to prepare
* Result: void
*
* Effect:
* Sets the mapping mode
****************************************************************************/
void CColorShow::PrepareDC(CDC & dc)
{
CSize sz = GetSize();
dc.SetMapMode(MM_ISOTROPIC);
dc.SetWindowExt(dx, dy); // Note: Win32 Programming p. 267 says window-before-viewport
dc.SetViewportExt(sz.cx, sz.cy);
} // CColorShow::PrepareDC
/****************************************************************************
* CColorShow::MapSelection
* Inputs:
* const CRect & r: Selection rectangle in exploded view
* Result: CRect
* The representation as coordinates relative to the bitmap
****************************************************************************/
CRect CColorShow::MapSelection(const CRect & r)
{
CRect nr = r;
CSize sz = GetSize();
CClientDC dc(this);
dc.SetMapMode(MM_ISOTROPIC);
dc.SetWindowExt(dx, dy); // Note: Win32 Programming p. 267 says window-before-viewport
dc.SetViewportExt(sz.cx, sz.cy);
dc.DPtoLP(&nr);
return nr;
} // CColorShow::MapSelection
/****************************************************************************
* CColorShow::OnPaint
* Result: void
*
* Effect:
* Draws the image. This includes the centroid box during picking, and
* the selection box after a selection has been made. Note the use of
* scaling to convert between these coordinate systems.
****************************************************************************/
void CColorShow::OnPaint()
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -