📄 fasthittest.cpp
字号:
// FastHitTest.cpp: implementation of the FastHitTest class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FastHitTest.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
FastHitTest::FastHitTest(const Region* pRegion, Graphics* pGraphics)
{
initColor();
pRegion->GetBounds(&m_Bounds,pGraphics);
m_RegionBuffer = new Bitmap(m_Bounds.Width,m_Bounds.Height,PixelFormat32bppRGB);
Graphics g(m_RegionBuffer);
SolidBrush backgroundBrush(m_BackgroundColor);
g.FillRectangle(&backgroundBrush,0,0,m_Bounds.Width,m_Bounds.Height);
SolidBrush foregroundBrush(m_ForegroundColor);
Region *r = pRegion->Clone();
r->Translate(-1 * m_Bounds.GetLeft(), -1 * m_Bounds.GetTop());
g.FillRegion(&foregroundBrush,r);
delete r;
Lock();
}
FastHitTest::FastHitTest(const GraphicsPath* pPath,BOOL includeEdge)
{
initColor();
init(pPath,includeEdge);
Lock();
}
FastHitTest::FastHitTest(const Point *pPoint, int num,BOOL includeEdge)
{
// debug required
initColor();
Point *p = NULL;
int n = num;
if( n == 1 || (n == 2 && pPoint[0].X == pPoint[1].X && pPoint[0].Y == pPoint[1].Y) )
{
m_Bounds.X = pPoint->X ;
m_Bounds.Y = pPoint->Y ;
m_Bounds.Width = 1 ;
m_Bounds.Height = 1 ;
m_RegionBuffer = new Bitmap(m_Bounds.Width,m_Bounds.Height,PixelFormat32bppRGB);
m_RegionBuffer->SetPixel(0,0,m_ForegroundColor);
}
else if( n >= 2 )
{
if(n == 2)
{
p = new Point[3];
p[0] = pPoint[0];
p[1] = pPoint[1];
p[2] = pPoint[0];
n = 3;
}
GraphicsPath pPath(FillModeWinding);
pPath.AddPolygon(p,n);
init(&pPath,includeEdge);
}
Lock();
}
void FastHitTest::initColor()
{
m_ForegroundColor = Color(255,255,0,255);
m_BackgroundColor = Color(255,0,0,255);
}
void FastHitTest::init(const GraphicsPath* pPath,BOOL includeEdge)
{
pPath->GetBounds(&m_Bounds);
m_RegionBuffer = new Bitmap(m_Bounds.Width,m_Bounds.Height,PixelFormat32bppRGB);
Graphics g(m_RegionBuffer);
SolidBrush backgroundBrush(m_BackgroundColor);
g.FillRectangle(&backgroundBrush,0,0,m_Bounds.Width,m_Bounds.Height);
SolidBrush foregroundBrush(m_ForegroundColor);
Pen foregroundPen(&foregroundBrush);
GraphicsPath *path = pPath->Clone();
Matrix mat;
mat.Translate((float)(-1.0 * m_Bounds.GetLeft()), (float)(-1 * m_Bounds.GetTop()));
path->Transform(&mat);
g.FillPath(&foregroundBrush,path);
//confirm required
if (includeEdge) {
g.DrawPath(&foregroundPen,path);
}
delete path;
}
void FastHitTest::Lock()
{
Rect rect(0,0,m_Bounds.Width,m_Bounds.Height);
if( m_RegionBuffer )
{
m_RegionBuffer->LockBits(&rect,
ImageLockModeRead,
m_RegionBuffer->GetPixelFormat(),
&m_Data);
}
}
FastHitTest::~FastHitTest()
{
m_RegionBuffer->UnlockBits(&m_Data);
if (m_RegionBuffer!=NULL) {
delete m_RegionBuffer;
}
}
BOOL FastHitTest::IsVisible(int x, int y)
{
if( !m_RegionBuffer )
{
return FALSE;
}
if ( x < m_Bounds.GetLeft() || x >= m_Bounds.GetRight()
|| y < m_Bounds.GetTop() || y >= m_Bounds.GetBottom() )
{
return FALSE;
}
UINT *p = (UINT*)m_Data.Scan0 + m_Data.Stride * (y-m_Bounds.GetTop() ) / 4 + (x - m_Bounds.GetLeft());
if ( *p == m_ForegroundColor.GetValue() ) {
return TRUE;
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -