代碼示例:
#include<windows.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
intmain(intargc,char*argv[])
{
chararg[50]={0};
arg[0]='\"';
strcpy(arg+1,argv[0]);
intlen=int(strlen(arg));
arg[len]='\"';
HWNDhWnd=FindWindow(NULL,arg);//找到程序運(yùn)行窗口的句柄
HDChDC=GetDC(hWnd);//通過(guò)窗口句柄得到該窗口的設(shè)備場(chǎng)境句柄
HPENhPen,hOldPen;//畫筆
inti=0;
for(;i<500;++i)
SetPixel(hDC,10+i,10+i,0x0000ff);//用畫點(diǎn)的辦法畫一根線,最后一個(gè)參數(shù)是顏色(32位)
hPen=CreatePen(PS_SOLID,2,0x00ff00);//生成綠色畫筆
hOldPen=(HPEN)SelectObject(hDC,hPen);//把畫筆引入設(shè)備場(chǎng)境
MoveToEx(hDC,20,50,NULL);//設(shè)置畫線起點(diǎn)
LineTo(hDC,520,550);//畫到終點(diǎn)
Arc(hDC,100,100,300,300,350,500,350,500);//畫圓
SelectObject(hDC,hOldPen);
ReleaseDC(hWnd,hDC);
//下面是對(duì)比,表明它確實(shí)是控制臺(tái)程序
printf("helloconsole");
system("pause");
return0;
}