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

📄 ~gridstatic.~cpp

📁 实时监控
💻 ~CPP
字号:
 // GridStatic.cpp : implementation file
//

#include "stdafx.h"
#include "../Resource.h"
#include "GridStatic.h"
#include "bmp_file.cpp"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CGridStatic

CGridStatic::CGridStatic(): index(0), r(8), c(8), drawing_grid(false)
{
	mask.alloc(r);
}

CGridStatic::~CGridStatic()
{
}


BEGIN_MESSAGE_MAP(CGridStatic, CStatic)
	//{{AFX_MSG_MAP(CGridStatic)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_WM_ENABLE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGridStatic message handlers

bool CGridStatic::init()
{
//	set_grid(8,8);

	CRect rc;
	GetWindowRect(rc);
	int w = rc.Width()/c;
	int h = rc.Height()/r;

	rc.right = rc.left + w*c;
	rc.bottom = rc.top + h*r;
	MoveWindow(rc);

	//bf.load_from_file(_T("sunflower.bmp"));
	bf.load_from_res(IDB_SUNFLOWER);
//	bf.v_invert();

	ModifyStyle(0, SS_NOTIFY, 0);

	bmp.LoadBitmap(IDB_SUNFLOWER);

	return true;
}

void CGridStatic::destroy()
{
}

void CGridStatic::draw_text(CDC& dc, LPCTSTR lpsz)
{
	CFont font;
	font.CreatePointFont(80, _T("Arial"), &dc);
	CFont* old = dc.SelectObject(&font);
	dc.SetBkMode(TRANSPARENT);
	dc.SetTextColor(RGB(255,255,255));
	dc.TextOut(2,2,lpsz);
	dc.SetTextColor(RGB(0,0,0));
	dc.TextOut(1,1,lpsz);
	dc.SelectObject(old);
}

#include "memdc.h"

// DrawBitmap	- Draws a bitmap (DDB & DIB section) onto a device
// pDC		- Pointer to a device context
// hBitmap	- Handle of the bitmap
// hPal		- Handle of a logical palette associated with the bitmap
// xDest	- x-coordinate of the upper-left corner of the destination rect
// yDest	- y-coordinate of the upper-left corner of the destination rect
void DrawBitmap( CDC *pDC, HBITMAP hBitmap, HPALETTE hPal, int xDest, int yDest,
				int wDest, int hDest)
{
	// Get logical coordinates
	BITMAP bm;
	::GetObject( hBitmap, sizeof( bm ), &bm );
	CPoint size( bm.bmWidth, bm.bmHeight );
	pDC->DPtoLP(&size);
	
	CPoint org(0,0);
	pDC->DPtoLP(&org);
	
	// Create a memory DC compatible with the destination DC
	CDC memDC;
	memDC.CreateCompatibleDC( pDC );
	memDC.SetMapMode( pDC->GetMapMode() );
	
	//memDC.SelectObject( &bitmap );
	HBITMAP hBmOld = (HBITMAP)::SelectObject( memDC.m_hDC, hBitmap );
	
	
	// Select and realize the palette
	if( hPal && pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
	{
		SelectPalette( pDC->GetSafeHdc(), hPal, FALSE );
		pDC->RealizePalette();
	}
	else
	{
		pDC->SetStretchBltMode(COLORONCOLOR);
	}
	//pDC->StretchBlt(xDest, yDest, wDest, hDest, &memDC, org.x, org.y,
	//	size.x, size.y, SRCCOPY);
	
	pDC->StretchBlt(xDest, yDest, wDest, hDest, &memDC, org.x, org.y,
		size.x, size.y, SRCCOPY);

	::SelectObject( memDC.m_hDC, hBmOld );
}

void CGridStatic::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CRect rc;
	GetClientRect(rc);

	CMemDC memdc(&dc, rc);
	//bf.draw(&memdc, rc);
	BITMAP bm;
	bmp.GetBitmap(&bm);
	DrawBitmap(&memdc, bmp, NULL, 0, 0, rc.Width(), rc.Height());

	draw_frames(memdc, rc);
	darken_frame(memdc, rc);

	if( drawing_grid )
		if( !text.IsEmpty() )
			draw_text(memdc, text);
	
	memdc.FrameRect(rc, &CBrush(RGB(255,255,255)));
}

BOOL CGridStatic::OnEraseBkgnd(CDC* pDC) 
{
	return TRUE;
}

void CGridStatic::set_index(int _index)
{
	index = _index;
	text.Format(_T("Ch %02d"), index);
	update();
}

void CGridStatic::update()
{
	RedrawWindow();
}

void CGridStatic::set_grid(int rows, int cols)
{
	r = rows;
	c = cols;
	drawing_grid = true;
	if( !mask )
		mask.alloc(rows);

	update();

}

void CGridStatic::clear_grid()
{
	drawing_grid = false;
	mask.remove_all();
	update();
}

// 填充
void CGridStatic::darken_grid(CDC& dc, CRect rc)
{
	DWORD pixel;
	for( int i=rc.top; i<rc.bottom; i++ )
		for( int j=rc.left; j<rc.right; j++ )
		{
			pixel = dc.GetPixel(CPoint(j, i));
			dc.SetPixel(CPoint(j,i), (~pixel & 0x00ffffff));
		}
}

// 画黑格
void CGridStatic::darken_frame(CDC& dc, CRect rc)
{
	if( !drawing_grid )
		return;
	
	for(int i=0; i<r; i++ )
	{
		for( int j=0; j<c; j++ )
		{
			if( mask.is_set(j, i) )
			{
				int left = rc.left + j*(rc.Width()/r);
				int top = rc.top + i*(rc.Height()/c);
				int right = left + rc.Width()/r;
				int bottom = top + rc.Height()/c;
				
				dc.MoveTo(left, top);
				
				dc.SelectStockObject(BLACK_PEN);
				dc.LineTo(right, top);
				dc.LineTo(right, bottom);
				dc.LineTo(left, bottom);
				dc.LineTo(left, top);
				darken_grid(dc, CRect(left, top, right, bottom));
			}
		}
	}
}

void CGridStatic::draw_frames(CDC& dc, CRect rc)
{
	if( !IsWindow(m_hWnd) )
		return;
	if( c == 0 ||
		r == 0 )
		return;
	if( !drawing_grid )
		return;

	dc.SelectStockObject(WHITE_PEN);

	int w = rc.Width()/(c);
	int h = rc.Height()/(r);
	for( int i=rc.left+w; i<=rc.right-w; i+= w )
	{
		dc.MoveTo(i, rc.top);
		dc.LineTo(i, rc.bottom);
	}
	for( i=rc.top+h; i<=rc.bottom-h; i+= h )
	{
		dc.MoveTo(rc.left, i);
		dc.LineTo(rc.right, i);
	}

}

void CGridStatic::calc_mask(CPoint pt, int& x, int& y)
{
	CRect rc;
	GetClientRect(rc);

	x = ((pt.x-rc.left)*10/(rc.Width()/c)/10);
	y = ((pt.y-rc.top)*10/(rc.Height()/r)/10);
}

void CGridStatic::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if( drawing_grid )
	{
		int x, y;
		calc_mask(point, x, y);
		mask.toggle(x,y);

		update();
	}

	CStatic::OnLButtonDown(nFlags, point);
}

void CGridStatic::OnRButtonDown(UINT nFlags, CPoint point) 
{
	SHORT k = GetAsyncKeyState(VK_CONTROL);
	if( k )
	{
		mask.select_all();
	}
	else
	{
		mask.remove_all();
	}
	update();
	CStatic::OnRButtonDown(nFlags, point);
}

byte* CGridStatic::mask_data()
{
	return mask;
}

void CGridStatic::OnEnable(BOOL bEnable)
{

}

⌨️ 快捷键说明

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