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

📄 test.cpp

📁 制作漂亮界面的方便有用的类
💻 CPP
字号:
// Test.cpp : implementation file
//
#include "stdafx.h"
#include "CanvasThread.h"
#include "Test.h"
#include "XBitmap.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTest

CTest::CTest()
: m_border(2)
{
	SetDoubleBuffer(true);
	DrawBackground(false);
	DrawBorder(true);
	SetBorder(SUNKEN);
	SetAutoFocus(false);
	selected = false;
	mouseX = mouseY = 0;

	m_bitmap_info.biSize = sizeof(BITMAPINFOHEADER);
	m_bitmap_info.biWidth = 50;
	m_bitmap_info.biHeight = -50;
	m_bitmap_info.biPlanes = 1;
	m_bitmap_info.biBitCount = 32;
	m_bitmap_info.biCompression = BI_RGB;
	m_bitmap_info.biSizeImage = 0;
	m_bitmap_info.biXPelsPerMeter = 0;
	m_bitmap_info.biYPelsPerMeter = 0;
	m_bitmap_info.biClrUsed = 0;
	m_bitmap_info.biClrImportant = 0;

	for(int i=0; i<10; i++)
	{
		poles.Add(Pole(rand()%150,rand()% 150));
	}
}

CTest::~CTest()
{
}

void CTest::Circle(CDC *dc, int x, int y, int radius)
{
	dc->Arc(x-radius,y-radius,x+radius,y+radius,x-radius,y-radius,x-radius,y-radius);
}

void CTest::DrawRect(CDC *dc, int x, int y, int w, int h, COLORREF color)
{
	CPen pen(PS_SOLID,1,color);
	CPen* oldPen = dc->SelectObject(&pen);

	dc->MoveTo(x,y);
	dc->LineTo(x+w,y);
	dc->LineTo(x+w,y+h);
	dc->LineTo(x,y+h);
	dc->LineTo(x,y);

	dc->SelectObject(oldPen);
}

void CTest::DrawRect(CDC *dc, RECT rect, COLORREF color)
{
	CPen pen(PS_SOLID,1,color);
	CPen* oldPen = dc->SelectObject(&pen);

	dc->MoveTo(rect.left,rect.top);
	dc->LineTo(rect.right-1,rect.top);
	dc->LineTo(rect.right-1,rect.bottom-1);
	dc->LineTo(rect.left,rect.bottom-1);
	dc->LineTo(rect.left,rect.top);

	dc->SelectObject(oldPen);
}

void CTest::Paint(CDC *dc)
{
	CRect rect;
	GetClientRect(rect);

	nico.DrawStretch(dc,0,0,GetWidth(), GetHeight());

	CPen pen(PS_SOLID,1, RGB(255,0,0));

	CPen* oldPen = dc->SelectObject(&pen);

	for(int i=0; i<poles.GetSize(); i++)
	{
		int x = poles[i].x;
		int y = poles[i].y;

		Circle(dc,x,y,5);
	}


	CPen pen2(PS_SOLID,1, RGB(0,0,0));
	dc->SelectObject(&pen2);

	dc->MoveTo(0, rect.Height() / 2);
	dc->LineTo(rect.Width(), rect.Height() / 2);
	dc->MoveTo(rect.Width()/2, 0);
	dc->LineTo(rect.Width()/2, rect.Height() );

	if(IsMouseIn())
	{
		CPen pen4(PS_SOLID, 1, RGB(50,150,150));
		dc->SelectObject(&pen4);

		dc->MoveTo(0, mouseY);
		dc->LineTo(rect.Width(), mouseY);
		dc->MoveTo(mouseX, 0);
		dc->LineTo(mouseX, rect.Height() );

	}	
	
	dc->SelectObject(oldPen);
}

BEGIN_MESSAGE_MAP(CTest, CCanvas)
	//{{AFX_MSG_MAP(CTest)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest message handlers

void CTest::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	for(int i=0;i<poles.GetSize(); i++)
	{
		if( point.x > (poles[i].x-5) && point.x < (poles[i].x+5)
			&& point.y > (poles[i].y-5) && point.y < (poles[i].y+5) )
		{
			SetCapture();
			selected = true;
			pole = i;
			break;
		}
	}
}

void CTest::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(!selected)
	{
		zeros.Add(Zero(point.x,point.y));
		Repaint();
	}
	else
	{
		selected = false;
		ReleaseCapture();
	}
}

void CTest::OnMouseMove(UINT nFlags, CPoint point) 
{
	mouseX = point.x;
	mouseY = point.y;

	// TODO: Add your message handler code here and/or call default
	if(selected)
	{
		poles[pole].x = point.x;
		poles[pole].y = point.y;
	}
	
	Repaint();

	CCanvas::OnMouseMove(nFlags, point);
}

void CTest::OnMouseExit()
{
	Repaint();
}

int CTest::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CCanvas::OnCreate(lpCreateStruct) == -1)
		return -1;
		
	return 0;
}

// Called each time the CCanvas is created
void CTest::PreSubclassWindow() 
{	
	ICONINFO info;
	HCURSOR cursor;

	HICON icon = AfxGetApp()->LoadIcon(IDI_ICON1);

	GetIconInfo(icon, &info);

	info.fIcon = FALSE;
	info.xHotspot = 30;
	info.yHotspot = 27;

	cursor = CreateIconIndirect(&info);
	
	SetCursor(cursor);
	BYTE* lp;
	CClientDC dc(this);

	hBmp = CreateDIBSection(dc.GetSafeHdc(), (LPBITMAPINFO)&m_bitmap_info, DIB_RGB_COLORS,(LPVOID*)&lp, NULL, 0);

	for(int h=0;h<50;h++)
	{
		for(int w=0;w<50;w++)
		{
			*(lp++) = 255-5*w;	// Blue
			*(lp++) = 3*w+2*h;	// Green
			*(lp++) = 255-5*h;	// Red
			*(lp++) = 0;	// Reserved
		}
	}

	nico.Attach(hBmp);
	
	CCanvas::PreSubclassWindow();
}

⌨️ 快捷键说明

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