参考: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));
}
}