獲取圖形對象的屬性及選入新的圖形對象

 
  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堆空間
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章