Building Graphical User Interfaces (GUIs)
Using the Microsoft Foundation Classes (MFC)


All of these examples assume that Microsoft Visual C++ 2010 or 2012 is the compiler being used.



Link to Sketcher Part 2


If you have not already created the Sketcher project then click back on your browser and click the link Creating Projects in Visual Studio for GUI applications and follow the instructions for creating the project for exercise 5.

In the file projectname.cpp find the function InitInstance. You should see the following lines toward the end of the function:
	// The one and only window has been initialized, so show and update it
	m_pMainWnd->SetWindowText("Sketcher-John Doe");
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

Change "John Doe" in the text in the call to SetWindowText to your name.

Exercise 5: Sketcher (Part 1) - a drawing program

Drawing Basics

Before you begin writing a graphics program it will help to have a little background on the organization and structure of the underlying graphics system on a PC. When you are drawing on the screen or printing a drawing your code is never interacting directly with the hardware (monitor or printer). There are instead a couple of operating system layers of code between your graphics program code and the hardware device.

Graphics Device Interface (GDI) - This is a device independent layer that is part of the operating system which allows you to do drawing in a window without having to worry about which hardware (screen or printer) a drawing is to be displayed on or how the hardware displays the graphics.

Device Context (DC) - This is a data structure defined by Windows that allows you to send drawing commands to a GDI. It is the actual drawing interface you will use to create your graphics.

FYI: Mapping Modes - There are a number of, so called, mapping modes for a DC all of which define the directions of the X and Y axes and the units used (pixels, inches, etc.) You will only need to be concerned with the MM_TEXT mode which uses pixels as the units and sets position (0,0) in the upper left corner of the window client area with X increasing to the right and Y increasing down. This, fortunately is the default mapping mode so you do not have to set anything in your program to handle this.

OnDraw() - If you look in the SketcherView.cpp class file you will find a function called OnDraw(). This is where you handle all of the drawing in an application. This function gets called every time there is a WM_PAINT message sent to your application. This will happen when the OS determines that you need to repaint the screen, which may happen as a result of the window being resized, another window being moved over the application window, or any number of other events. What this implies is you can’t just draw in a window and forget it, expecting everything to remain the same. Events change things.

Take a look at the OnDraw() function now:

	void CSketcherView::OnDraw(CDC *pDC);

(Note: when the View class is created this is defined as OnDraw(CDC* /*pDC*/) so you can give the Device Context pointer any name you want. For now just uncomment and use the default pDC name.)

Drawing in a Nutshell


Note: In each of the code samples given below the pointer variable pDC is the pointer to the Device Context which is passed into the OnDraw() function.

Set Window Title
  1. In Sketcher.cpp locate the function InitInstance().
  2. At the bottom of the function just after the lines...
    	m_pMainWnd->ShowWindow(SW_SHOW);
    	m_pMainWnd->UpdateWindow();
    
    Add the line...
    	m_pMainWnd->SetWindowTextA("Sketcher - John Doe");
    
    Substitute your name for John Doe.

Drawing Reference

Below is a brief outline of all of the drawing functions you will need for this exercise. To get started try experimenting with a few of these just to see what happens. Put all of the drawing code in the OnDraw() function found in SketcherView.cpp

Setting the Current Position Drawing Lines Drawing Rectangles Drawing Ellipses Drawing Arcs Drawing and Filling in Color