Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Hãy viết mã cho các mục trình đơn. Chúng tôi sẽ làm mục Draw đầu tiên, như trong Liệt kê 11,24. Mục trình đơn này rút ra hai dòng, một hình chữ nhật và hình elip. Đầu tiên chúng ta tạo ra một đồ họa đối tượng sử dụng phương pháp Form.CreateGraphics và gọi DrawLine, DrawRectangle, và phương pháp FillEllipse. | This document was created by an unregistered ChmMagic please go to http www.bisenter.com to register it. Thanks Let s write code for the menu items. We ll do the Draw Items first as inListing 11.24. This menu item draws two lines a rectangle and an ellipse. First we create a Graphics object using the Form.CreateGraphics method and call the DrawLine DrawRectangle and FillEllipse methods. See Chapter 3 for more on these methods. Listing 11.24 Drawing graphics items private void DrawItems_Click object sender System.EventArgs e Create a Graphics object Graphics g this.CreateGraphics g.Clear this.BackColor Draw graphics items g.DrawLine Pens.Blue 10 10 10 100 g.DrawLine Pens.Blue 10 10 100 10 g.DrawRectangle Pens.Yellow 20 20 200 200 g.FillEllipse Brushes.Gray 40 40 100 100 Dispose of object g.Dispose Figure 11.14 shows the output from Listing 11.24. Figure 11.14. Drawing simple graphics items This document was created by an unregistered ChmMagic please go to http www.bisenter.com to register it. Thanks Now let s write code for Print Graphics Items. We want to print the output shown inFigure 11.14. We create aPrintDocument object add a PrintPage event handler and call thePrint method. The PrintPage event handler draws the graphics items. Listing 11.25 contains two methods. The PrintGraphicsItems_Click method is a menu click event handler that creates cPrintDocument object sets its PrintPage event and calls the Print method. The second method PrintGraphicsItemsHandler simply calls the draw and fill methods of PrintPageEventArgs.Graphics. Listing 11.25 Printing graphics items private void PrintGraphicsItems_Click object sender System.EventArgs e Create a PrintDocument object PrintDocument pd new PrintDocument Add PrintPage event handler pd.PrintPage new PrintPageEventHandler this.PrintGraphicsItemsHandler Print pd.Print private void PrintGraphicsItemsHandler object sender PrintPageEventArgs ppeArgs Create a printer Graphics object Graphics g ppeArgs.Graphics Draw graphics