1、毕业设计 (论文 )外文资料翻译 外文出处 : Advanced MFC Programming 附 件: A.外文翻译 -原文部分 B.外文翻译 -译文部分 指导教师评 语 : 签名: 年 月 日 (用外文写 ) 附录 A.外文 翻译 -原文部分 (写明出处 ) Advanced MFC Programming Chapter 8: DC, Pen, Brush and Palette 8.0 Device Context & GDI Objects Starting from this chapter, we are going to study topics on GDI (Graphi
2、cs Device Interface) programming. Situation GDI is a standard interface between the programmer and physical devices. It provides many functions that can be used to output various objects to the hardware (e.g. a display or a printer). GDI is very important because, as a programmer, we may want our ap
3、plications to be compatible with as many peripherals as possible. For example, almost every application need to write to display, and many applications also support printer output. The problem here is that since a program should be able to run on different types of devices, it is impossible to let t
4、he programmer know the details of every device and write code to support it beforehand. The solution is to introduce GDI between the hardware and the programmer. Because it is a standard interface, the programmer doesnt have to have any knowledge on the hardware in order to operate it. As long as th
5、e hardware supports standard GDI, the application should be able to execute correctly. Device Context As a programmer, we do not output directly to hardware such as display or printer. Instead, we output to an object that will further realize our intention. This object is called device context (DC),
6、 it is a Windows object that contains the detailed information about hardware. When we call a standard GDI function, the DC implements it according to hardware attributes and configuration. Suppose we want to put a pixel at specific logical coordinates on the display. If we do not have GDI, we need
7、the following information of the display in order to implement this simple operation: 1 Video memory configuration. We need this information in order to convert logical coordinates to physical buffer address. 2 Device type. If the device is a palette device, we need to convert a RGB combination to a
8、n index to the color table and use it to specify a color. If the device is a non-palette device, we can use the RGB combination directly to specify a color. Because the actual devices are different form one type to another, it is impossible for us to gather enough information to support all the devi
9、ces in the world. So instead of handling it by the programmer, GDI functions let us use logical coordinates and RGB color directly, the conversion will be implemented by the device driver. GDI Objects In Windows , GDI objects are tools that can be used together with device context to perform various
10、 drawings. They are designed for the convenience of programmers. The following is a list of some commonly used GDI objects: GDI objects Usage Pen Used to draw line, curve, the border of a rectangle, ellipse, polygon, etc. Brush Used to fill the interior of a rectangle, ellipse, polygon with a specif
11、ied pattern Font Used to manage a variety of fonts that can be used to output text Palette Used to manage colors on a palette device Bitmap Used to manage image creating, drawing, manipulating, etc The above GDI objects, along with device context, are all managed through handles. We can use the hand
12、le of an object to identify or access it. Besides the handles, every GDI object has a corresponding MFC class. The following is a list of their handle types and classes: Object Handle Type MFC Class Object Handle Type MFC Class Device Context HDC CDC, CClientDC, CWindowDC, etc. Pen HPEN CPen Brush H
13、BRUSH CBrush Font HFONT CFont Palette HPALETTE CPalette Bitmap HBITMAP CBitmap Obtaining DC As a programmer, most of the time we need to output to a specific window rather than the whole screen. A DC can be obtained from any window in the system, and can be used to call GDI functions. There are many
14、 ways to obtain DC from a window, the following is an incomplete list: 1 Call function CWnd:GetDC(). This function will return a CDC type pointer that can be used to perform drawing operations within the window. 2 Declare CClientDC type variable and pass a CWnd type pointer to its constructor. Class
15、 CClientDC is designed to perform drawing operations in the client area of a window. 3 Declare CWndowDC type variable and pass a CWnd type pointer to its constructor. Class CWindowDC is designed to perform drawing operations in the whole window (including client area and non-client area). 4 In MFC,
16、certain member functions are designed to update applications interface (i.e. CView: OnDraw (). These functions will automatically be call ed when a window needs to be updated. For this kind of functions, the device context will be passed through one of functions parameters. Using DC with GDI Objects
17、 Before calling any function to perform drawing, we must make sure that an appropriate GDI object is being selected by the DC. For example, if we want to draw a red line with a width of 2, we must select a solid red pen whose width is 2. The following steps show how to use DC together with GDI objec
18、ts: 1 Obtain or create a DC that can be used to perform drawing operations on the target window. 2 Create or obtain an appropriate GDI (pen, brush, font) object. 3 Select the GDI object into the DC, use a pointer to store the old GDI object. 4 Perform drawing operations. 5 Select the old GDI object into the DC, this will select the new GDI object out of the DC. Destroy