Monday 28 November 2011

What is the GDI and how is it accessed? in C programming

What is the GDI and how is it accessed?

GDI stands for Graphic Device Interface. The GDI is a set of functions located in a dynamic link library (named GDI.EXE, in your Windows system directory) that are used to support device-independent graphics output on your screen or printer. Through the GDI, your program can be run on any PC that supports Windows. The GDI is implemented at a high level, so your programs are sheltered from the complexities of dealing with different output devices. You simply make GDI calls, and Windows works with your graphics or printer driver to ensure that the output is what you intended.

The gateway to the GDI is through something called a Device Context. A device context handle is simplya numeric representation of a device context (that is, a GDI-supported object). By using the device context handle, you can instruct Windows to manipulate and draw objects on-screen. For instance, the following portion of code is used to obtain a device context handle from Windows and draw a rectangle on-screen:

long FAR PASCAL _export WndProc (HWND hwnd, UINT message,
UINT wParam, LONG lParam)
{
HDC hdcOutput;
PAINTSTRUCT psPaint;
HPEN hpenDraw;
...
switch(message)
{
...
case WM_PAINT:
hdcOutput = BeginPaint(hwndMyWindow, &psPaint);
hpenDraw = CreatePen(PS_SOLID, 3, 0L);
SelectObject(hdcOutput, hpenDraw);
Rectangle(hdcOutput, 0, 0, 150, 150);
TextOut(hdcOutput, 200, 200,
“Just the FAQ’s, Ma’am...”, 24);
...
}
...
}

In the preceding program, the BeginPaint() function prepares the current window to be painted with graphics objects. The CreatePen() function creates a pen object of a specified style, width, and color. The pen is used to paint objects on-screen. The SelectObject() function selects the GDI object you want to work with. After these setup functions are called, the Rectangle() function is called to create a rectangle in the window, and the TextOut() function is called to print text to the window.

Cross Reference:

None

No comments:

Post a Comment