Wednesday, January 18, 2012

SetPixel() function on windows.h . How to use it??

If you are learning computer graphics, you may need to create graphical structure using pixels. When trying to implement this in program, you need the programming language that has facility to print a single pixel on the screen i.e. the language that supports pixel operation. Fortunately almost all Lagrange with GUI facility support pixel printing  Now what about C/C++? You cannot print a pixel in console window. You need some graphical mechanism for this. Fortunately windows user can use windows.h library for this.
windows.h provides a function SetPixel() to print a pixel at specified location of a window. The general form of the function is
SetPixel(HDC hdc, int x, int y, COLORREF& color);
where, x and y are coordinates of pixel to be display and color is the color of pixel. To print a pixel only writing SetPixel command and embedding windows.h is not sufficient. You need some extra linking operation for this. You must link the library libgdi32.a in your IDE's linker setting before using the SetPixel function. The following program draws a horizontal line to a window. If you have any confusion please ask me in the form of comment, I will reply you as soon as possible.

Note

To run this code in your machine with Code::blocks IDE, add a link library libgdi32.a (it is usually inside MinGW\lib )  in linker setting.

#include 
static HWND sHwnd;
static COLORREF redColor=RGB(255,0,0);
static COLORREF blueColor=RGB(0,0,255);
static COLORREF greenColor=RGB(0,255,0);

void SetWindowHandle(HWND hwnd)
{
    sHwnd=hwnd;
}
void setPixel(int x,int y,COLORREF& color=redColor)
{
    if(sHwnd==NULL)
    {
        MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR);
        exit(0);
    }
    HDC hdc=GetDC(sHwnd);
    SetPixel(hdc,x,y,color);
    ReleaseDC(sHwnd,hdc);
    return;
}
void drawLine()
{
    for(int i = 0; i < 100; i++)
        setPixel(10+i, 100, blueColor);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    switch(message)
    {
    case WM_PAINT:
        SetWindowHandle(hwnd);
        drawLine();
        break;
    case WM_CLOSE: // FAIL THROUGH to call DefWindowProc
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    default:
        break; // FAIL to call DefWindowProc //
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("Test");
    WNDCLASS wndclass;
    wndclass.style         = CS_HREDRAW|CS_VREDRAW ;
    wndclass.lpfnWndProc   = WndProc ;
    wndclass.cbClsExtra    = 0 ;
    wndclass.cbWndExtra    = 0 ;
    wndclass.hInstance     = hInstance ;
    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.lpszMenuName  = NULL ;
    wndclass.lpszClassName = szAppName ;
// Register the window //
    if(!RegisterClass(& wndclass))
    {
        MessageBox(NULL,"Registering the class failled","Error",MB_OK|MB_ICONERROR);
        exit(0);
    }
// CreateWindow //
    HWND hwnd=CreateWindow(szAppName,"SetPixel example - programming-technique.blogspot.com",
                           WS_OVERLAPPEDWINDOW,
                           CW_USEDEFAULT,
                           CW_USEDEFAULT,
                           CW_USEDEFAULT,
                           CW_USEDEFAULT,
                           NULL,
                           NULL,
                           hInstance,
                           NULL);
    if(!hwnd)
    {
        MessageBox(NULL,"Window Creation Failed!","Error",MB_OK);
        exit(0);
    }
// ShowWindow and UpdateWindow //
    ShowWindow(hwnd,iCmdShow);
    UpdateWindow(hwnd);
// Message Loop //
    MSG msg;
    while(GetMessage(& msg,NULL,0,0))
    {
        TranslateMessage(& msg);
        DispatchMessage(& msg);
    }
    return 0;
}

6 comments:

  1. Hey I can't get the SetPixel Function running using CodeBlocks 10.5 IDE with GNC compiler, can you give us a step by step tutorial on how to use it? thanks in advance.

    ReplyDelete
  2. I meant GCC compiler, can you give us a brief tutorial on how to get it working with codeblocks please? thank you for those great tutorials

    ReplyDelete
  3. Wow thank you very much i was not quit sure how to use SetPixel correctly.

    ReplyDelete
  4. Thank You Bibek!
    That piece of code is exactly what I need for my design.
    Made an empty project in VC Express, copied as main.c and it works perfectly.

    ReplyDelete
  5. Isn't there a similar function called as 'putpixel()' from graphics.h? What's the difference between 'setpixel()' and 'putpixel()'?

    ReplyDelete