程式碼示例:
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
char arg[50]={0};
arg[0]= "\ " ";
strcpy(arg+1,argv[0]);
int len=int(strlen(arg));
arg[len]= "\ " ";
HWND hWnd=FindWindow(NULL,arg); //找到程式執行視窗的控制代碼
HDC hDC=GetDC(hWnd);//透過視窗控制代碼得到該視窗的裝置場境控制代碼
HPEN hPen,hOldPen; //畫筆
int i=0;
for(;i <500;++i)
SetPixel(hDC,10+i,10+i,0x0000ff);//用畫點的辦法畫一根線,最後一個引數是顏色(32位)
hPen=CreatePen(PS_SOLID,2,0x00ff00);//生成綠色畫筆
hOldPen=(HPEN)SelectObject(hDC,hPen);//把畫筆引入裝置場境
MoveToEx(hDC,20,50,NULL); //設定畫線起點
LineTo(hDC,520,550); //畫到終點
Arc(hDC,100,100,300,300,350,500,350,500);//畫圓
SelectObject(hDC,hOldPen);
ReleaseDC(hWnd,hDC);
//下面是對比,表明它確實是控制檯程式
printf( "hello console ");
system( "pause ");
return 0;
}
程式碼示例:
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
char arg[50]={0};
arg[0]= "\ " ";
strcpy(arg+1,argv[0]);
int len=int(strlen(arg));
arg[len]= "\ " ";
HWND hWnd=FindWindow(NULL,arg); //找到程式執行視窗的控制代碼
HDC hDC=GetDC(hWnd);//透過視窗控制代碼得到該視窗的裝置場境控制代碼
HPEN hPen,hOldPen; //畫筆
int i=0;
for(;i <500;++i)
SetPixel(hDC,10+i,10+i,0x0000ff);//用畫點的辦法畫一根線,最後一個引數是顏色(32位)
hPen=CreatePen(PS_SOLID,2,0x00ff00);//生成綠色畫筆
hOldPen=(HPEN)SelectObject(hDC,hPen);//把畫筆引入裝置場境
MoveToEx(hDC,20,50,NULL); //設定畫線起點
LineTo(hDC,520,550); //畫到終點
Arc(hDC,100,100,300,300,350,500,350,500);//畫圓
SelectObject(hDC,hOldPen);
ReleaseDC(hWnd,hDC);
//下面是對比,表明它確實是控制檯程式
printf( "hello console ");
system( "pause ");
return 0;
}