C++ drawing pixels question

ぐ巨炮叔叔 提交于 2019-12-04 19:08:42

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.

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...

Long story short: You should use a graphics framework / widgeting toolkit in order to create a window, and start from there.

Then, depending on what framework you use, you'll then create the window and modify the pixels according to its reference.

I recommend SDL (small, probably exactly what you need), Allegro (similar) or Qt (largeish).

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

Problem with your code: I tried out your code and i found that the line seemed to appear in some instance of its execution, but sometimes, when I execute it, the red line isn't found! From what I understand it is because you kept these 3 statements:

// Set text of the console so you can find the window
SetConsoleTitle("Pixel In Console?"); 
HWND hwnd = FindWindow(NULL, "Pixel In Console?"); // Get the HWND
HDC hdc = GetDC(hwnd); // Get the DC from that HWND

But Windows only updates your window name after a little time, so if you make these statements execute one after the other, the HWND hwnd = FindWindow(NULL, "Pixel In Console?"); statement cant find such a window, because Windows takes time to update the title as done by : SetConsoleTitle("Pixel In Console?");

Solution: You can either keep a Sleep(int); (include windows.h) or use:

HWND hwnd = FindWindowA("ConsoleWindowClass",NULL); // Get the HWND

instead of

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

Here is an example program:

#include <windows.h>
#include<conio.h>`
#include<iostream.h>`

POINT p;    //structure with coordinates to mouse location`

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

    HWND hwnd = FindWindowA("ConsoleWindowClass",NULL); // Get the HWND
    HDC hdc = GetDC(hwnd); // Get the DC from that HWND

    for( int i = 0 ; i < 400 ; i++ )
    {
        for(int j=0;j<50;j++)
            SetPixel(hdc, i, j, color);
    }
    while(1)
    {
          GetCursorPos(&p);
          ScreenToClient(hwnd,&p);
          cout<<p.x<<' '<<p.y;
          clrscr();
          if(GetAsyncKeyState(1)) //checks if left mouse button is pressed
          {
                //fool around with these functions:
                SetPixel(hdc,p.x,p.y,color); 
                //LineTo(hdc,p.x,p.y);
                //Rectangle(hdc,200,200,p.x,p.y);
                Sleep(10);
          }
    }
    ReleaseDC(hwnd, hdc); // Release the DC
    DeleteDC(hdc); // Delete the DC
    system("pause");
}

This is my first answer. I hope I helped and it took a lot of searching to find these functions besides I think I learned more from your script than I showed you :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!