Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

api - Drawing in a Win32 Console on C++?

What is the best way to draw things in the Console Window on the Win 32 platform using C++?

I know that you can draw simple art using symbols but is there a way of doing something more complex like circles or even bitmaps?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, it is possible.

Get the HWND of the console window using GetConsoleWindow and then draw in it.

#define _WIN32_WINNT 0x601
#include <windows.h>
#include <stdio.h>

int main() {
    // Get window handle to console, and device context
    HWND console_handle = GetConsoleWindow();
    HDC device_context = GetDC(console_handle);

    //Here's a 5 pixels wide RED line [from initial 0,0] to 300,300
    HPEN pen = CreatePen(PS_SOLID, 5, RGB(255, 0, 0));
    SelectObject(device_context, pen);
    LineTo(device_context, 300, 300);

    ReleaseDC(console_handle, device_context);

    getchar();

    return 0;
}

Note: GetConsoleWindow was introduced in Windows 2000. It's available when _WIN32_WINNT is set to 0x500 or greater.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...