Drawing the Map and Cities for Program 3

1. Create a Bit Map resource in the Program 3 project and load the P3-FlightMap.bmp provided in the zip file in the resource using the Visual Studio resource editor.

2. In your Prog3Dlg.h file declare two public variables:

CBitmap m_MapBitmap;

CDC m_DCMemory;

3. In the onInitDialog() function set up the bitmap image. In the call to LoadBitmapA make sure the ID given is the same as the one for the Bit Map resource you created.

// Set up the bitmap map image

CDC *thisDC = this->GetDC();

m_MapBitmap.LoadBitmapA(IDB_BITMAP2);

m_DCMemory.CreateCompatibleDC(thisDC);

m_DCMemory.SelectObject(&m_MapBitmap);

4. In the OnPaint() function of the dialog class paint the image using BitBlt.

//======Do Painting here ======

CDC *thisDC = this->GetDC();// Get the window DC

// Draw the map bitmap

thisDC->BitBlt(m_MapRect.left, m_MapRect.top,

m_MapRect.Width(), m_MapRect.Height(),

&m_DCMemory, 0, 0, SRCCOPY);

I created a variable in Prog3Dlg.h called CRect m_MapRect;

Through experimentation I found where the upper left corner (left, top) should be. The width and height are given by the pixel size of the bit map which is read from the CityData03.xml file.

5. Call your appropriate function to then draw the cities over the map. For example:

// Have the flight sim draw all cities on top of the bitmap

m_TheSim->drawCities(thisDC, 415, 25);

m_TheSim is the instance of my FlightSim.

thisDC is the CDC for the Prog3Dlg. It is obtained by calling

CDC *thisDC = this->GetDC();// Get the window DC

The two ints are the X pixel location and Y pixel location of the upper left corner of the map image.

6. For program 3 the call to getCityData in my CityMap object has two additional arguments which returns the pixel offset of the city from the upper left corner of the map. Below is the sample code for drawing a dot representing a city. I am using a CityMap object to hold the vector of cities. Each city knows its pixel offset onto the map (obtained with getCityXPixLocation and getCityYPixLocation in my CityMap object). In the function drawCities this is the code to do the drawing. The args passed in are the CDC to draw into and the X,Y offset in pixels of the top left corner of the map image when it is drawn in the client area of the window.

void CityMap::drawCities(CDC *dc, int xPos, int yPos)

{

CRect textRect;

int X, Y;

// For all cities

for(vector<City *>::iterator itr=m_vCities.begin();

itr != m_vCities.end(); itr++)

{

X = (*itr)->getCityXPixLocation() + xPos;

Y = (*itr)->getCityYPixLocation() + yPos;

// Draw the city circle 5 pix wide and 5 pix high

dc->Ellipse(X-2, Y-2, X+3, Y+3);

// Add the label

textRect.left = (int)(X+6);

textRect.top = (int)(Y);

textRect.right = textRect.left + 35;

textRect.bottom = textRect.top + 18;

// Standard window background

dc->SetBkMode(TRANSPARENT);

// Use OPAQUE for solid background

// Args: Char array giving city symbol, #chars in the

//array, address of the textRect, default value 0

dc->DrawText((*itr)->getCitySymbol(), 3, &textRect, 0);

}

}