Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1620118
  • 博文数量: 441
  • 博客积分: 20087
  • 博客等级: 上将
  • 技术积分: 3562
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-19 15:35
文章分类

全部博文(441)

文章存档

2014年(1)

2012年(1)

2011年(8)

2010年(16)

2009年(15)

2008年(152)

2007年(178)

2006年(70)

分类: C/C++

2006-11-22 14:44:36

Why we use DC ( device context) ?


    In a single-tasking environment such as MS-DOS, the name of the game when it comes to screen output is "anything goes." A running application is free to do just about whatever it wants whenever it wants, whether that involves drawing a line on the screen, reprogramming the adapter's color palette, or switching to another video mode. In a windowed, multitasking environment such as Windows, programs can't be afforded such freedom because the output from program A must be protected from the output of program B. First and foremost, this means that each program's output must be restricted to its own window. The GDI uses a simple mechanism to make sure every program that draws in a window plays by the rules. That mechanism is the device context.

    When a Windows program draws to a screen, a printer, or any other output device, it doesn't output pixels directly to the device. Instead, it draws to a logical "display surface" represented by a device context (DC). A device context is a data structure deep inside Windows that contains fields describing everything the GDI needs to know about the display surface, including the physical device with which it is associated and assorted state information. Before it draws anything on the screen, a Windows program acquires a device context handle from the GDI. It then passes that handle back to the GDI each time it calls a GDI output function. Without a valid device context handle, the GDI won't draw the first pixel. And through the device context, the GDI can make sure that everything the program draws is clipped to a particular area of the screen. Device contexts play a huge role in making the GDI device-independent because, given a handle to a device context, the same GDI functions can be used to draw to a diverse assortment of output devices.

    在单任务环境如MS-DOS中,应用程序可以自己的做它想做的事情,无论是在屏幕上画一条线,重新编写适配器的调色板,还是转换到另一种图像模式。而在窗口化多任务环境如Windows中,程序则失去了这种自由,因为程序A的输出必须与程序B的输出格开,首先这意味着各程序的输出必须限制在自己的窗口中。GDI(Graphic Device Interface,图形设备接口)使用单一的机制保证在窗口中画图的个程序遵循这一规则,这种机制我们称之为设备描述表(或者设备环境)。


    当Windows程序在屏幕、打印机或其他输出设备上画图时,它并不是将像素直接输出到设备上,而是将图绘制到由设备描述表(DC)表示的逻辑意义上的"显示平面"上去。设备描述表是Windows内在的一种数据结构,它包含GDI需要的所有关于显示平面情况的描述字段,包括相连的物理设备和各种各样的状态信息。在屏幕上画图之前,Windows程序从GDI获取设备描述表句柄(Device Context Handle),每一次调用一个GDI输出函数时它就会把这个句柄传回给GDI。如果没有有效的设备句柄,则GDI不会做任何的绘图动作。通过设备描述表,GDI可确保程序所画的任何图形都能剪贴到屏幕的特定区域。设备描述表在使GDI摆脱设备限制的过程中发挥了重要的作用。获得设备描述表的句柄之后,同一GDI函数可以向多种输出设备上画图。


阅读(1479) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~