画直线

参考:http://www.cnblogs.com/zhangjun1130/archive/2010/12/01/1893278.html

#include <windows.h>
#include <windowsx.h>

//函数声明
HWND hwnd;
BOOL InitWindow(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void StraightLine(int x0, int y0, int x1, int y1, HDC hdc ,int tickness);
void Drew_x(HDC hdc, int temp, int i, int tickness);
void Drew_y(HDC hdc, int temp, int i, int tickness);
//*******************************************************************

//函数:WinMain()

//功能:Win32应用程序入口函数。创建主窗口,处理消息循环

//*******************************************************************

int PASCAL WinMain(HINSTANCE hInstance, //当前实例句柄

	HINSTANCE hPrevInstance, //前一个实例句柄

	LPSTR lpCmdLine, //命令行字符

	int nCmdShow) //窗口显示方式

{

	MSG msg;
	//创建主窗口

	if (!InitWindow(hInstance, nCmdShow))

		return FALSE;

	//进入消息循环:

	//从该应用程序的消息队列中检取消息,送到消息处理过程,

	//当检取到WM_QUIT消息时,退出消息循环。
	HDC hdc = GetDC(hwnd);
	while (GetMessage(&msg, NULL, 0, 0))

	{
		
		StraightLine(10,100, 30, 10, hdc, 3);

		TranslateMessage(&msg);

		DispatchMessage(&msg);

	}

	//程序结束

	return msg.wParam;

}

//******************************************************************

//函数:InitWindow()

//功能:创建窗口。

//******************************************************************

static BOOL InitWindow(HINSTANCE hInstance, int nCmdShow)

{

	WNDCLASS wc; //窗口类结构

	//填充窗口类结构

	wc.style = CS_VREDRAW | CS_HREDRAW;

	wc.lpfnWndProc = (WNDPROC)WinProc;

	wc.cbClsExtra = 0;

	wc.cbWndExtra = 0;

	wc.hInstance = hInstance;

	wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);

	wc.hCursor = LoadCursor(NULL, IDC_ARROW);

	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

	wc.lpszMenuName = NULL;

	wc.lpszClassName = L"EasyWin";

	//注册窗口类

	RegisterClass(&wc);



	//创建主窗口

	hwnd = CreateWindow(

		L"EasyWin", //窗口类名称

		L"一个基本的Win32程序", //窗口标题

		WS_OVERLAPPEDWINDOW, //窗口风格,定义为普通型

		100, //窗口位置的x坐标

		100, //窗口位置的y坐标

		400, //窗口的宽度

		300, //窗口的高度

		NULL, //父窗口句柄

		NULL, //菜单句柄

		hInstance, //应用程序实例句柄

		NULL); //窗口创建数据指针

	if (!hwnd) return FALSE;

	//显示并更新窗口
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);
	return TRUE;

}

//******************************************************************

//函数:WinProc()

//功能:处理主窗口消息

//******************************************************************

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

	//调用缺省消息处理过程

	return DefWindowProc(hWnd, message, wParam, lParam);

}


void StraightLine(int x0, int y0, int x1, int y1,HDC hdc, int tickness)
{
	//斜率不存在的时候
	if (x0 == x1)
	{
		int m, n;
		if (y0 > y1)
		{
			m = y0;
			n = y1;
		}
		else
		{
			m = y1;
			n = y0;
		}
		for (int i = n; i <= m; ++i)
		{
			Drew_x(hdc, x0, i, tickness);
		}
	}
	//斜率存在
	double k = ((double)y1 - (double)y0) / ((double)x1 - (double)x0);
	int a = abs(x1 - x0);
	int b = abs(y1 - y0);
	//x长
	if (a > b)
	{
		int l, s;
		double y;
		int temp;
		if (x1 > x0)
		{
			l = x1;
			s = x0;
			y = y0;
		}
		else
		{
			l = x0; 
			s = x1;
			y = y1;
		}
		for (int i = s; i <= l; ++i)
		{
			y += k;
			temp = y;
			Drew_y(hdc, i, temp, tickness);
		}
	}

	else
	{
		int l, s;
		double x;
		int temp;
		if (y1 > y0)
		{
			l = y1;
			s = y0;
			x = x0;
		}
		else
		{
			l = y0;
			s = y1;
			x = x1;
		}
		k = 1 / k;
		for (int i = s; i <= l; ++i)
		{
			x += k;
			temp = x;
			Drew_x(hdc, temp, i, tickness);
		}
	}
	return;
}


void Drew_y(HDC hdc, int i, int temp, int tickness)
{
	SetPixel(hdc, i, temp, RGB(0, 0, 0));
	int l, r;
	if (tickness % 2 == 0)
	{
		l = tickness / 2;
		r = l - 1;
	}
	else
	{
		l = r = tickness / 2;
	}
	//改变y
	//先上边
	for (int j = 1; j <= l; ++j)
	{
		SetPixel(hdc, i, temp - j, RGB(0, 0, 0));
	}
	//再下边
	for (int j = 1; j <= r; ++j)
	{
		SetPixel(hdc, i, temp + j, RGB(0, 0, 0));
	}
}

void Drew_x(HDC hdc, int temp, int i, int tickness)
{
	SetPixel(hdc, temp, i, RGB(0, 0, 0));
	int l, r;
	if (tickness % 2 == 0)
	{
		l = tickness / 2;
		r = l - 1;
	}
	else
	{
		l = r = tickness / 2;
	}
	//改变x
	//先左边
	for (int j = 1; j <= l; ++j)
	{
		SetPixel(hdc, temp - j, i, RGB(0, 0, 0));
	}
	//再右边
	for (int j = 1; j <= r; ++j)
	{
		SetPixel(hdc, temp + j, i, RGB(0, 0, 0));
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值