#include "stdafx.h"
//#include <gl/glut.h>
//#include <iostream>
#include <cmath>
#include <Windows.h>
using namespace std;
inline int round(float val) {return int(val+0.5);}
//inline void SetPixel(int x,int y){glBegin(GL_POINTS);glVertex2i(x,y);glEnd();}
void lineDDA(int x0,int xEnd,int y0,int yEnd);
int _tmain(int argc, _TCHAR* argv[])
{
lineDDA(100,400,200,400);//控制台窗口的左上角为(0,0)
lineDDA(100,400,100,400);
system("pause");
return 0;
}
void lineDDA(int x0,int xEnd,int y0,int yEnd)
{
HWND hWnd=GetConsoleWindow(); //获得控制台窗口的句柄
HDC hDC=GetWindowDC(hWnd);//通过窗口句柄得到该窗口的设备场境句柄
int dx,dy,steps,k;
float xIncrease,yIncrease,x=x0,y=y0;
dx=xEnd-x0;
dy=yEnd-y0;
if (fabs((float)dx)>fabs((float)dy))
steps=dx;
else
steps=dy;
xIncrease=(float)dx/(float)steps;
yIncrease=(float)dy/(float)steps;
SetPixel(hDC,round(x),round(y),RGB(255,0,0));
for (k=0;k<steps;++k)
{
x+=xIncrease;
y+=yIncrease;
SetPixel(hDC,round(x),round(y),RGB(255,0,0));
}
}
#include "stdafx.h"
#include<windows.h>
int main( void )
{
HWND hWnd=GetConsoleWindow();
// 获取一个可供画图的DC,我这里就直接用桌面算了
//HDC hdc = GetWindowDC( GetDesktopWindow() );
HDC hdc=GetWindowDC(hWnd);
// 创建红色、1像素宽度的实线画笔
HPEN hpen1 = CreatePen( PS_SOLID, 1, RGB(255,0,0) );
// 创建绿色、5像素宽度的破折画笔,如果你想创建其他种类的画笔请参阅MSDN
HPEN hpen2 = CreatePen( PS_DASH, 5, RGB(0,255,0) );
// 创建一个实体蓝色画刷
HBRUSH hbrush1 = CreateSolidBrush( RGB(0,0,255) );
// 创造一个透明的画刷,如果你想创建其他种类的画刷请参阅MSDN
HBRUSH hbrush2 = (HBRUSH)GetStockObject( NULL_BRUSH );
// 将hpen1和hbrush1选进HDC,并保存HDC原来的画笔和画刷
HPEN hpen_old = (HPEN)SelectObject( hdc, hpen1 );
HBRUSH hbrush_old = (HBRUSH)SelectObject( hdc, hbrush1 );
// 在(40,30)处画一个宽200像素,高50像素的矩形
Rectangle( hdc, 40, 30, 40+200, 30+50 );
// 换hpen1和hbrush1,然后在(40,100)处也画一个矩形,看看有何差别
SelectObject( hdc, hpen2 );
SelectObject( hdc, hbrush2 );
Rectangle( hdc, 40, 100, 40+200, 100+50 );
// 画个椭圆看看
Ellipse( hdc, 40, 200, 40+200, 200+50 );
// 画个(0,600)到(800,0)的直线看看
MoveToEx( hdc, 0, 600, NULL );
LineTo( hdc, 800, 0 );
// 在(700,500)处画个黄点,不过这个点只有一像素大小,你细细的看才能找到
SetPixel( hdc, 700, 500, RGB(255,255,0) );
// 恢复原来的画笔和画刷
SelectObject( hdc, hpen_old );
SelectObject( hdc, hbrush_old );
return 0;
}