Thursday, December 6, 2007

Windows Programming Questions

SECTION- GDI and DRAWING

1. What is the purpose of GDI?

It is a layer which enables applications to use graphics and formatted text on both the video display and the printer.
Windows-based applications do not access the graphics hardware directly. Instead, GDI interacts with device drivers on behalf of applications.

2. What is a device context (DC)?

In windows drawing operation is done on three types of devices ie; Screen, printer or metafile.
DC is a structure containing information about the drawing attributes of a device such as a display or a printer.
All drawing calls are made through a device-context object, which internally calls the Windows APIs for drawing lines, shapes, and text.
Device contexts allow device-independent drawing in Windows.(Drawing logic is same for all devices).

3. What are the types of windows system objects?

a. User objects - supports window management
Eg: Window, Window Position, Accelerator Keys, Cursor, Icon, Menu, Caret, DDE conversation

b. GDI objects - supports graphics
Eg: Bitmap, Brush, DC, Font, Palette, Pen, Regions

c. Kernel objects - support memory management, process execution, and interprocess communications (IPC).
eg:Acces Token,Files,File mapping,Heap,Desktop,Thread,Event,Mutex,Semaphore,Critical Section,Timer,Module

4. Explain the different device context classes.

a. CPaintDC
This can be used only when responding to a WM_PAINT message ie; OnPaint().
It calls CWnd::BeginPaint() in the constructor and CWnd::EndPaint at destruction time.
CView::OnDraw function is passed with a CPaintDC already prepared (via OnPrepareDC()).

b. CClientDC
Represents only the client area of a window.
Constructor calls GetDC() and destructor calls ReleaseDC()

c. CWindowDC
Represents entire screen area of a CWnd object (both client and nonclient areas).
Constructor calls GetWindowDC() destructor calls ReleaseDC().

d. CMetaFileDC
Represents Windows metafile, which contains a sequence of graphics device interface (GDI) commands that you can replay to create a desired image or text.
User is supposed to call OnPrepareDC() here.

5. Explain the steps to use a Graphic Object with a device context.

Define a graphic object on the stack frame. Initialize the object with the type-specific create function, such as CreatePen. Alternatively, initialize the object in the constructor. See the discussion of one-stage and two-stage creation, which provides example code.

Select the object into the current device context, saving the old graphic object that was selected before.

When done with the current graphic object, select the old graphic object back into the device context to restore its state.

Allow the frame-allocated graphic object to be deleted automatically when the scope is exited.

6. What is a mapping mode? Why is it needed?

A device context has the ability to maintain a coordinate system separate and distinct from the underlying device. The co-ordinate system of the device context is called “Logical Co-ordinates” where as the one of the device is called “Physical /Device Co-ordinates”. Mapping mode, view port origin and extents, maps the Logical co-ordinates to Physical one. Eg: One logical unit is equal to one pixel if the mapping mode of the DC is MM_TEXT. Check out for view port origin and extents. A detailed description of these details is out of the scope of this post (They are not asked generally)J.

7. How do you convert Logical Points to device points and vise versa?

The LPtoDP function converts logical coordinates into device coordinates. The DPtoLP function converts device coordinates into logical coordinates. The conversion depends on the mapping mode of the device context, the settings of the origins and extents for the window and viewport, and the world transformation. The ScreenToClient() function converts the screen coordinates of a specified point on the screen to client-area coordinates and ClientToScreen does the reverse.

8. What are co-ordinate spaces and transformations in GDI?

They are used to scale, rotate, translate, shear, and reflect graphics output by an application. A coordinate space is a planar space that locates two-dimensional objects by using two reference axes that are perpendicular to each other. There are four coordinate spaces: world, page, device, and physical device (client area, desktop, or page of printer paper).

A transformation is an algorithm that alters ("transforms") the size, orientation, and shape of objects. This includes Translation, Scaling, Rotation, Shear and Reflection . Transformations also transfer a graphics object from one coordinate space to another. Ultimately, the object appears on the physical device, which is usually a screen or printer.

8. When is CWnd::OnEraseBkgnd called?What is the significance of it?

It is called to prepare an invalidated region for painting. Usually it is overridden to avoid flickering. Flickering appears, because the complete control is erased and it needs some time to fill the control-area with some text/graphics or whatever. There is a really short moment, when nothing can bee seen, because the background was erased but nothing is written yet. That's why we don't erase the background of the control anymore. To do this, we have to override the OnEraseBkgnd() to return false. Over ride the parent’s OnEraseBkgnd() to avoid flicker fully using ExcludeClipRect() using the control’s rectangle as a parameter. Explore it and enjoy flicker free drawing.

9. How to load Bitmap as background for a dialog box in an MFC application?

In the OnInitDialog () of the Dialog Box perform the following steps
Declare a member variable m_bkGroundMemDC of type CDC
Create a CBitmap object and mention the resource
Select the bitmap into the m_bkGroundMemDC

In the OnPaint () of the Dialog Box perform the following steps .
Get the Dialog box DC
Do BitBlt or StretchBlt of the Dialog box DC with m_bkGroundMemDC as the source
Relaese Dialog box DC

No comments: