-1

How can I set a pixel in application screen?

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


I can't do it with graphics.h.

  • Which operating system? – Roger Lipscombe Mar 07 '16 at 10:03
  • @Roger Lipscombe I'm using Windows 8... –  Mar 07 '16 at 10:03
  • And which UI framework? Plain ol' Win32? Windows Forms, WPF? – Roger Lipscombe Mar 07 '16 at 10:05
  • @Roger Lipscombe I'm using Console Application because UI doesn't works, in Code::Blocks. –  Mar 07 '16 at 10:07
  • You can't set individual pixels in Windows console applications. Well, you _can_, but it's a hack: http://stackoverflow.com/q/12378642/8446 – Roger Lipscombe Mar 07 '16 at 10:09
  • @rogerLipscombe The problem in answer is because including `iostream` returns one error because it doesn't exists here! –  Mar 07 '16 at 10:12
  • How do you expect to be able to do anything at all if you can't use the OS libraries? – Neijwiert Mar 07 '16 at 10:16
  • @Neijwiert I just said graphics.h doesn't works. Pretty simple. NOTHING OF GRAPHICS WORKS HERE. –  Mar 07 '16 at 10:18
  • Why not? Download the SDK maybe....???? – Neijwiert Mar 07 '16 at 10:20
  • Besides doing UI in C++ is entirely OS dependent. If you want to be flexible you should use QT for example. – Neijwiert Mar 07 '16 at 10:22
  • @Neijwiert But it's C! It'll probaly will say: iostream doesn't exists; but, okay. I'll try here. Thxh –  Mar 07 '16 at 10:22
  • I don't wanna do it in C++! But... C... –  Mar 07 '16 at 10:23
  • Oh sorry having too many windows open. Also this might help: http://forums.codeblocks.org/index.php?topic=7208.15 – Neijwiert Mar 07 '16 at 10:24
  • @Neijwiert Right! I changed to C++ now, ... but the SDK is very heavy for my net. 7GB... and said the download will delay XXX hours or days. –  Mar 07 '16 at 10:37
  • Impossible. ;) Thanks for the help. C & C++ languages doesn't has capacity to do that. –  Mar 07 '16 at 10:38
  • 1
    @Errorever: You could not be more wrong. Both C and C++ are *perfectly* able to do graphics. You need to use some kind of platform / third-party library, though, because the *standard* library of C and C++ is only meant to provide *basic* functionality, for handling data. That is why Roger's *first* question was "which operating system", because that limits your choice of libraries, and his second question was *which* of the available libraries. – DevSolar Mar 07 '16 at 11:58
  • @DevSolar But cannot it be done without libraries? The "own" libraries includes inexistent sources in its code, returning errors which avoid the program to run. And there's no function to edit screen rendering! –  Mar 07 '16 at 13:41
  • 1
    @Errorever: I think you should take a step back from whatever it is you are trying to achieve, and first become more familiar with the language and toolchain you are intending to use. Your questions / remarks are all over the place, and none of it is actually related to graphics output. These are basic language / toolchain issues. – DevSolar Mar 07 '16 at 13:44
  • Related: [What is an undefined reference error and how do I fix it.](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – DevSolar Mar 07 '16 at 13:46
  • @DevSolar Yes. I think I should do that now to solve my problems each time more, but I was needing of "graphics", sincerally. Let's start by string, int and array... –  Mar 07 '16 at 13:50
  • @Errorever: I'm sorry but that is the way it works, *especially* with your first (couple of) languages: You can't directly use them to "get results", you need to go through a somewhat lengthy learning process. It gets (much) easier with each additional programming language, to the point where you can "get results" in a new language in a matter of hours. And you will *still* encounter roadblocks that will require you to take a break, pick up The Book on the language, and learn things anew. – DevSolar Mar 07 '16 at 13:54
  • @Errorever: "graphics" is a operating system specific thing. You have to use the APIs provided by the operating system for graphics and use those. `graphics.h` is the header for the DOS graphics library that comes with DOS targeting compilers. If you want to target Windows, then you have to use the functions Windows offers you for this. – datenwolf Mar 07 '16 at 14:27

1 Answers1

3

Use the function HWND WINAPI GetConsoleWindow(void); to retrieve the handle of the console window, then you can paint in it as with any standard window.
Get the window DC with HDC GetDC(HWND hWnd);then use

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

to set pixel color.
This is a working sample, original borrowed by T J Betsworth @ https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=10861&lngWId=3, modified to be compiled in plain C using GetConsoleWindow():

/**************************************
 * 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;
}
Frankie_C
  • 4,764
  • 1
  • 13
  • 30
  • Unfortunately it doesn't works. I changed the program compiler, but it's silencious. I probably know the error. –  Mar 07 '16 at 11:04
  • See the working sample that I added to the answer. – Frankie_C Mar 07 '16 at 11:53
  • There's the following error: `36|undefined reference to `GetConsoleWindow'|`. Thanks for that big help, man! Do you know how can the error be fixed? –  Mar 07 '16 at 13:36
  • Although, the error just says `GetConsoleWindow` doesn't exists in the context. Where can I get it? –  Mar 07 '16 at 13:43
  • @Errorever: By linking with the right API stub library. In your case `kernel32.lib` (curious why you don't link to it in the first place). – datenwolf Mar 07 '16 at 14:24
  • @datenwolf I tried, ... but still get the error. I'll try more... –  Mar 07 '16 at 15:08