获取图形对象的属性及选入新的图形对象

来源:互联网 发布:手机版收音机不用网络 编辑:程序博客网 时间:2024/06/06 23:55
 
  1. /*
  2.     2:获取图形对象的属性及选入新的图形对象
  3.     应用程序可以使用函数GetCurrentObject和GetObject来获得图形对象的属性。前者
  4.     用于返回唯一标识刚刚被选入到DC中的图形对象的句柄,后者会返回一个描述图形对象
  5.     属性的结构体。
  6.     下面的例子演示了程序如何获得画刷的属性并通过与其相关的信息来决定是否有必要重新
  7.     选入一个新的画刷。
  8. */
  9.         HDC hdc;                     // display DC handle 
  10.         HBRUSH hbrushNew, hbrushOld; // brush handles 
  11.         HBRUSH hbrush;               // brush handle 
  12.         LOGBRUSH lb;                 // logical-brush structure 
  13.         //通过此函数获得当前画刷的句柄
  14.         hbrush = GetCurrentObject(hdc, OBJ_BRUSH); 
  15.         //根据画刷的句柄来获取与其相关的信息,放之于一个结构体中
  16.         GetObject(hbrush, sizeof(LOGBRUSH), &lb); 
  17.         // If the current brush is not a solid-black brush, 
  18.         // replace it with the solid-black stock brush. 
  19.         if ((lb.lbStyle != BS_SOLID) 
  20.             || (lb.lbColor != 0x000000)) 
  21.         { 
  22.             hbrushNew = GetStockObject(BLACK_BRUSH); 
  23.             hbrushOld = SelectObject(hdc, hbrushNew); 
  24.         } 
  25.         // Perform painting operations with the white brush. 
  26.         // After completing the last painting operation with the new 
  27.         // brush, the application should select the original brush back 
  28.         // into the device context and delete the new brush. 
  29.         // In this example, hbrushNew contains a handle to a stock object. 
  30.         // It is not necessary (but it is not harmful) to call 
  31.         // DeleteObject on a stock object. If hbrushNew contained a handle 
  32.         // to a brush created by a function such as CreateBrushIndirect, 
  33.         // it would be necessary to call DeleteObject. 
  34.         //用完新选入的图形对象后的常规处理,重新选入默认的并删除新创建的。
  35.         SelectObject(hdc, hbrushOld); //重新选入默认的对象
  36.         DeleteObject(hbrushNew); //删除新对象,释放GDI堆空间
原创粉丝点击