Jump to content
EleTD.com
Sign in to follow this  
Guest GoSu.SpeeD

[C/++] Draw a Square (SetPixel)

Recommended Posts

Guest GoSu.SpeeD

Here is an example i coded for a friend to show him how to draw a square on your desktop.

/*  Example of using setpixel to draw a square on your desktop 
    By GoSu.SpeeD/Akimoko/Shoxin/Encrypt. and many other aliases
    
    compile: gcc.exe square.c -o square.exe -lgdi32
*/

#include <windows.h>

int main()
{
    int x = 729; // X co-ord. to start at
    int y = 507; // Y co-ord. to start at
    
    int r = 255; // RGB VALUE FOR WHITE
    int g = 255; // RGB VALUE FOR WHITE
    int b = 255; // RGB VALUE FOR WHITE
    
    int XCount = 0;
    int YCount = 0;
    
    HDC Hdc = GetDC(HWND_DESKTOP); // Retrieve a handle for our desktop
    sleep(5000); // Give our user time to minimize and switch to the desktop

    while(1)
    {
        SetPixel(Hdc, x++, y, RGB (r, g, b)); // Increasing pixel length every loop
        sleep(1);
        XCount++;
        
        if (XCount == 100) // When we have a line 100 pixels in width
        {
            while(1)
            {
                SetPixel(Hdc, x, y++, RGB (r, g, b));
                sleep(1);
                YCount++;
                
                if (YCount == 100) // When we have a line with 100 pixels in height
                {
                    while(1)
                    {      
                        SetPixel(Hdc, x--, y, RGB (r, g, b)); // Going back toward the original X Co-ord
                        sleep(1);
                        XCount--;
                        
                        if (XCount == 0) // When our we return to the same X Co-ord
                        {
                            while(1)
                            {      
                                SetPixel(Hdc, x, y--, RGB (r, g, b));
                                sleep(1);
                                YCount--;
                                
                                if (YCount == 0) // Complete the Square
                                {
                                    sleep(5000); // wait 5 seconds so the user can see the results
                                    ReleaseDC (HWND_DESKTOP, Hdc); // Release Device Content
                                    return 0;
                                }
                            }
                        }
                    }
                }
            }
        }
    }        
}

Share this post


Link to post
Sign in to follow this  

×
×
  • Create New...