控制台也是窗口,同样也有自己的DC,也可以画线的,用spy++看看控制台窗口的类名字,用GetWindow得到窗口句柄,获取它的DC,就可以画线。只是好像控制台窗口的重画是个问题。

解决方案 »

  1.   

    获取控制台窗体句柄不需要那么麻烦,win2k以后版本ms已经提供了直接获得控制台窗体句柄的apiGetConsoleWindowThe GetConsoleWindow function retrieves the window handle used by the console associated with the calling process.
    HWND GetConsoleWindow(void);Parameters
    This function has no parameters. 
    Return Values
    The return value is a handle to the window used by the console associated with the calling process or NULL if there is no such associated console.Res
    To compile an application that uses this function, define the _WIN32_WINNT macro as 0x0500 or later. For more information, see Using the Windows Headers.Requirements
    Client: Requires Windows XP or Windows 2000 Professional.
    Server: Requires Windows Server 2003 or Windows 2000 Server.
    Header: Declared in Wincon.h; include Windows.h.
    Library: Use Kernel32.lib.
      

  2.   

    #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=GetWindowDC(hWnd);//通过窗口句柄得到该窗口的设备场境句柄
    HPEN hPen,hOldPen; //画笔
    int i=0;for(;i<500;++i)hPen=CreatePen(PS_SOLID,2,0x00ff00);//生成绿色画笔
    hOldPen=(HPEN)SelectObject(hDC,hPen);//把画笔引入设备场境
    MoveToEx(hDC,20,50,NULL); //设置画线起点
    LineTo(hDC,520,550); //画到终点Arc(hDC,200,200,300,300,350,500,350,500);//画圆SelectObject(hDC,hOldPen);
    ReleaseDC(hWnd,hDC);return 0;
    }