Search code examples
c++winapipixel

C++ drawing pixels question



How to make window, or more like clipping region , where I'll be able to draw pixels? It might use WinApi, however I don't want my project to look like winapi one, so it will have

int main(){}

instead of

int WINAPI WinMain(HINSTANCE ...

I have found an example, where I'm able to draw in console window

int main()
{
    COLORREF color = RGB(255,0,0); // COLORREF to hold the color info

    SetConsoleTitle("Pixel In Console?"); // Set text of the console so you can find the window

    HWND hwnd = FindWindow(NULL, "Pixel In Console?"); // Get the HWND
    HDC hdc = GetDC(hwnd); // Get the DC from that HWND

    for( int i = 0 ; i < 50 ; i++ )
    {
    SetPixel(hdc, 5+i, 12, color); // SetPixel(HDC hdc, int x, int y, COLORREF color)
    }
        ReleaseDC(hwnd, hdc); // Release the DC
    DeleteDC(hdc); // Delete the DC
    system("pause");
    return(0);
}

but instead of console, I want to draw on my selected region, which will hold focus ( when user clicks on it, etc ).

It would be also great to be able to handle simple keyboard/mouse events for this program, but it isn't my primary target here, maybe some other third party libraries will help with it.

I hope I've explained clearly what I want to do, but English isn't my native language, so I'm very sorry for any missunderstandings.

I will be thankfull for any help.


As I'm using this site first time, I'm sorry for little spam or messages in wrong places, as I'm not sure where to post my next messages :-) So what i wanted to write, is:

" Otherwise, how does Allegro/SDL create window? They use assembler calls or shell ones? I'll be much happier, when I'll be able to create window from scratch, no matter how much work does it take :) "


Solution

  • You won't like this - in Windows, you have to create a window, then override WM_PAINT message, then draw what you have to draw when you are called from the system. That's old-school way of doing things, and it isn't so bad.

    Some interesting and relevant links:

    http://www.winprog.org/tutorial/bitmaps.html

    http://www.codeproject.com/Articles/66250/BeginPaint-EndPaint-or-GetDC-ReleaseDC.aspx

    If you are really into avoiding all that, try popcap. Learning curve involved there maybe steeper, so you probably really want to stick with GDI and HWND no matter how hard and confusing it might look in the beginning.