如何在没有<graphics.h>库的情况下设置像素?

时间:2016-03-07 10:02:11

标签: c

如何在应用程序屏幕中设置像素?

E.g: setpixel(x,y,rgb)

我无法使用图形。 h

1 个答案:

答案 0 :(得分:3)

使用函数HWND WINAPI GetConsoleWindow(void);检索控制台窗口的句柄,然后您可以像使用任何标准窗口一样在其中绘画。
使用HDC GetDC(HWND hWnd);获取窗口DC,然后使用

COLORREF SetPixel(HDC hdc, int X, int Y, COLORREF crColor);

设置像素颜色 这是一个工作样本,原文由T J Betsworth @ https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=10861&lngWId=3借用,修改为使用GetConsoleWindow()以纯 C 编译:

/**************************************
 * Name: Console SetPixel
 * Description:using SetPixel function with
 * the gdi link for graphical use in a console window.
 * By: T J Betsworth (from psc cd)
 *
 * Assumes:Requires win32 console project.
 * Modifyied by Frankie_C to be compiled under plain C
**************************************/
#include <stdlib.h>
#include <windows.h>
#include <math.h>

int main(void)
{
    int i, x, y;
    SetConsoleTitle("Console SetPixel");
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord = { 95, 40 };
    SMALL_RECT rec = { 0, 0, 0, 0 };
    SetConsoleScreenBufferSize(hout, coord);
    rec.Right = coord.X - 1;
    rec.Bottom = coord.Y - 1;
    SetConsoleWindowInfo(hout, TRUE, &rec);
    CONSOLE_CURSOR_INFO cci;
    cci.dwSize = 25;
    cci.bVisible = FALSE;
    SetConsoleCursorInfo(hout, &cci);
    COORD coord1;
    coord1.X = 0;
    coord1.Y = 0;
    DWORD wr;
    FillConsoleOutputAttribute(hout, 255, 95 * 40, coord1, &wr);

    //Here we use GetConsoleWindow
    HWND hWnd = GetConsoleWindow();
    HDC hdc = GetDC(hWnd);
    for (i = 0; i < 9000; i++)
    {
        x = rand() % 480;
        y = rand() % 480;
        SetPixel(hdc, x, y, RGB(rand() % 255, rand() % 255, rand() % 255));
    }
    for (i = 0; i < 1257; i++)
    {
        x = (int)(200 * cos(6.28 * i / 1257) + 240);
        y = (int)(200 * sin(6.28 * i / 1257) + 240);
        SetPixel(hdc, x, y, RGB(255, 0, 0));
    }
    for (x = 0; x < 760; x += 3)
    {
        y = 240;
        SetPixel(hdc, x, y, RGB(255, 0, 0));
    }
    for (y = 0; y < 480; y += 3)
    {
        x = 240;
        SetPixel(hdc, x, y, RGB(255, 0, 0));
    }
    for (x = 44; x < 437; x++)
    {
        y = (int)(76.4 * sin(x / 76.4) * cos(x / 76.4) + 240);
        SetPixel(hdc, x, y, RGB(0, 0, 255));
    }
    for (y = 44; y < 437; y++)
    {
        x = (int)(75.8 * sin(y / 75.8) * cos(y / 75.8) + 240);
        SetPixel(hdc, x, y, RGB(0, 0, 255));
    }
    HFONT font;
    font = CreateFont(30, 0, 90, 0, 0, TRUE, 0, 0, 0, 0, 0, 0, 0, "Dotum");
    SelectObject(hdc, font);
    SetBkColor(hdc, RGB(255, 255, 255));
    SetTextColor(hdc, RGB(0, 0, 255));
    TextOut(hdc, 555, 100, "Console", 7);
    TextOut(hdc, 557, 140, "SetPixel", 8);
    DeleteObject(font);
    ReleaseDC(hWnd, hdc);
    DeleteDC(hdc);
    COORD coord2 = { 62, 29 };
    SetConsoleCursorPosition(hout, coord2);
    SetConsoleTextAttribute(hout, 249);
    system("PAUSE");
    return 0;
}