Drawing in Windows
Windows programming is primarily with graphical elements and they run in a desktop graphical environment. Windows subsystem provides UI controls to display information to the users. These are drawn by the graphical subsystems so programmers do not need to draw anything on their own. Now programmers might require to draw some figures or display images to the screen.
An about dialog box is a small example where programmers display a logo or some drawings. However, this is imperative for a game, graphics editor, or animation editor where drawing on the window comes as a basic requirement. Drawing something on a window, the programmer must obtain the device context of the window. All the drawing and painting are done on the device context handle.
A device context (DC) is a data structure that contains information of the drawing attributes of an output device such as a display card or a printer and used in Windows graphical programming practices.
A device context is a structure that defines a set of graphic objects and their associated attributes, and the graphic modes that affect output. The graphic objects include a pen for line drawing, a brush for painting and filling, a bitmap for copying or scrolling parts of the screen, a palette for defining the set of available colors, a region for clipping and other operations, and a path for painting and drawing operations.
Drawing on devices
We can draw something on the window on the WM_PAINT event. WM_PAINT event is sent by the Win32 GUI framework at the time of drawing of the window or when a part of the window requires an update.
Programmer should obtain the device context using BeginPaint() API call and then drawing can be done using various GDI calls. At the end of the GDI calls programmers should call EndPaint(). More details : BeginPaint & EndPaint
Additional drawing on the window can be done on the need basis. GetDC and GetWindowDC call returns the device context of the client Window area and device context of the entire window. GDI drawing routines can be called to draw and paintings on the window. ReleaseDC function should be called at the end of all drawing steps and this call will release the device context handle from the program. More details : GetDC & GetWindowDC
Drawing on the printer works in the same way as drawing on the window. However, the way the programmer should obtain the device context is different for printer. PrintDlg() is the API to receive the device context of a given printer in the system. This function pops up the printer dialog on the screen and the user selects the printer name for the printing. This function takes a structure object of PRINTDLG as reference and returns the DC value in PRINTDLG.hDC. More details : Printer DC vs Screen DC
Drawing on the device context is done with the help of Graphics Device Interface (GDI) objects. A set of graphics objects is defined in the Windows graphics subsystem which acts as the utility tools for drawing on the device context.
- Pen Object - A pen is a drawing tool used to draw lines and outlines of different shapes. Know more: Pen Object
- Brush Object - A brush object is used to draw the filling of the inner area of any closed shapes. Know more: Brush Object
- Font Object - Fonts are small pictures of text characters. A font ojcent defines how the test strings will appear on the device. Know more: Font Object
- Bitmap object - A bitmap object contains a picture or photo. A picture can be displayed on the device using a bitmap. Know more: Bitmap Object
- Palette Object - A Palette object is a drawing tool to hold a collection of colors for the drawing on the device. Know more: Palette Object
- Region Object - Region object is a object that tells the area or region on the drawing area on the device.
Low layer device interfacing
Device Context is an abstraction in windows to access any output hardware like graphic adapters or printers which display text and graphical drawings. Windows Operating system returns a handle to the device context of the device. Device context is a structure in kernel mode of the operating system where it stores the attributes and other properties of the device. Hardware may vary in many aspects but device context is common for all for any particular device type. Hardware vendors supply device-dependent device drives or Miniport drivers for their hardware which takes care of the lower layer access and manageability of the hardware. The upper layer is the Win32 APIs to access this hardware from the application layer. Windows provides a set of APIs to access a type of device through this device context. Lower layer access and manageability may vary from hardware to hardware and from vendor to vendor but methods and steps via windows API are the same for accessing the same type of device. It creates a uniform access point for device hardware.
About our authors: Team EQA
MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.
This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...
Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.
DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.
LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.
We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...
Learn on Youtube