Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ drawing pixels question

Tags:

c++

pixel

winapi


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 :) "

like image 406
Neomex Avatar asked Jul 13 '26 05:07

Neomex


2 Answers

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.

like image 140
Daniel Mošmondor Avatar answered Jul 14 '26 18:07

Daniel Mošmondor


I suggest you to try OpenGL coupled with either GLFW or glut. With OpenGL you will be able to handle all the things that are related to graphics processing (2D/3D rendering), and the role of the GLFW library for instance is to add some functionalities like: window management, event management, timers, threads etc.

Personnal note, go for GLFW because glut isn't maintained anymore I think...

like image 39
Amokrane Chentir Avatar answered Jul 14 '26 17:07

Amokrane Chentir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!