The Timezone Application - I


We have started the timezone application in the last article. Just to revise, we have drawn two lines, intersecting each other at the cursor position. 

Add the following data members to the Form1 class.

 

Point hstart, hend, vstart, vend ;
Image img ;
Label l ;
 

The hstart and hend objects would contain starting and ending coordinates of the horizontal line. The vstart and vend objects would contain starting and ending coordinates of the vertical line. The Image object would be used to load and draw the world map image. The label would be popped up to show the day of week, time and city name. 
Add the Load event handler. Add the initialization code as shown below. 

 

private void Form1_Load ( object sender, System.EventArgs e )
{

img = Image.FromFile ( "World.gif" ) ;
hstart = new Point ( 0, ClientSize.Height / 2 ) ;
hend = new Point ( ClientSize.Width, ClientSize.Height / 2 ) ;
vstart = new Point ( ClientSize.Width / 2, 0 ) ;
vend = new Point ( ClientSize.Width / 2, ClientSize.Height ) ;

}


Here, we have loaded the ‘world.gif’ file containing the map image. The Point objects are initialized with the proper values. 
Add the Paint event handler wherein we would draw the image on the form. We would also draw the lines using the Point objects initialized just now.

 

private void Form1_Paint ( object sender, PaintEventArgs e )
{

Graphics g = e.Graphics ;
g.DrawImage ( img, 0, 0, img.Width, img.Height ) ;

Pen p = new Pen ( Brushes.Red ) ;
g.DrawLine ( p, hstart, hend ) ;
g.DrawLine ( p, vstart, vend ) ;

}


Run the program, you would see the output as shown in the following figure. 

.

You must have noticed the grayed maximized box of the form. We have set the MaximizeBox property of the form to false so that the size of the form won’t change. We have hidden the cursor by adding the following statement to the Load event handler

 

Cursor.Dispose( ) ;


The lines should move as the mouse moves. We have already written the code to draw new lines and erase previous ones in the Form1_MouseMove( ) handler. Update that code as shown below. 

 

private void Form1_MouseMove ( object sender, MouseEventArgs e )
{

Graphics g = CreateGraphics( ) ;
hend.Y = hstart.Y = e.Y ;
vend.X = vstart.X = e.X ;
g.DrawImage ( img, 0, 0, img.Width, img.Height ) ;
Pen p = new Pen ( Brushes.Red ) ;
g.DrawLine ( p, hstart, hend ) ;
g.DrawLine ( p, vstart, vend ) ;

}


Here, we have updated the coordinates stored in the Point objects according to the mouse position and drawn lines at new position. 
We must keep checking the mouse position. If the mouse is placed over a city that is present in our city database, we must display the label carrying day, time and city. In our program the application data is the name of the city, its coordinates and the time at that place with respect to the Indian Standard Time. To keep things simple we have stored this data in an array of objects. The objects are of type location. location is a user-defined class. We would first declare this class.

 

class location
{

public int hours, minutes ;
public string city ;
public int x, y ;

public location ( int h, int m, String c, int xx, int yy )
{

hours = h ;
minutes = m ;
city = c ;
x = xx ;
y = yy ;

}

}


The location class contains data members to hold hours, minutes, city name and x-y coordinates of the city on the map. The data members are initialized through constructor.
Populate the array in the Form1 class as shown below. This is only a part of the array. 

 

location[ ] loc =
{

new location ( 0, 0, "NewDelhi", 245, 90 ),
new location ( 0, 0, "Calcutta", 254, 99 ),
new location ( 0, 0, "Mumbai", 240, 107),
new location ( 0, 0, "Nagpur", 245, 100),
new location ( 0, 0,"Chennai", 245, 110),
new location ( -5, -30, "Lisbon", 160, 79),
new location ( -4, -30, "Madrid", 165, 74),
new location ( -3, -30, "Athens", 192, 84),
new location ( -3, -30, "Cairo", 202, 91),


} ;