windows编程之GDI基础--设备内容(二)

取得设备内容句柄:  

Windows提供了几种取得设备内容句柄的方法。如果在处理一个消息时取得了设备内容句柄,应该在退出窗口函数之前释放它(或者删除它)。一旦释放了句柄,它就不再有效了.

  在WM_PAINT消息中:

hdc = BeginPaint (hwnd, &ps) ;
        
//其它行程序
        
EndPaint (hwnd, &ps) ;

  ps为PAINTSTRUCT的结构体.在这个结构体中有一个RECT的结构体rcPaint定义.它获取一个无效矩形。无效矩形就是此刻需要重绘的区域,也可能是个裁剪区域.

  hdc传回的是设备内容的句柄.

  当BeginPaint成功执行后,rcPaint变为有效

  在非WM_PAINT消息中:

hdc = GetDC (hwnd) ;
        
//其它行程序
        
ReleaseDC (hwnd, hdc) ;

  这个设备内容适用于窗口句柄为hwnd的显示区域。这些呼叫与BeginPaint和EndPaint的组合之间的基本区别是,利用从GetDC传回的句柄可以在整个显示区域上绘图。但是, GetDC和ReleaseDC不使显示区域中任何可能的无效区域变成有效。

  以上4个都是取到显示区域的设备内容句柄.

  我们还可以取得适用于整个窗口的设备内容句柄,包括标题栏,状态栏,滚动条等等.

hdc = GetWindowDC (hwnd) ;
        
//其它行程序
        
ReleaseDC (hwnd, hdc) ;

  以上的六个函数都和显示器上面的某个特定的窗口有关,还有一组函数更加通用:

hdc = CreateDC (pszDriver, pszDevice, pszOutput, pData) ;
        
//其它行程序
        
DeleteDC (hdc) ;

  其实CreatDC什么类型的设备内容句柄都可以取到.也可以取到一个窗口设备内容的句柄.要取得整个屏幕的句柄:

hdc = CreateDC (TEXT ("DISPLAY"), NULL, NULL, NULL) ;
//或者
hdc = GetDC (NULL);

  有时只是需要取得关于某设备内容的一些信息而并不进行任何绘画,在这种情况下,您可以使用CreateIC来取得一个「信息内容」的句柄,其参数与CreateDC函数相同,但是不能用这个信息内容句柄往设备上写东西:

hdc = CreateIC (TEXT ("DISPLAY"), NULL, NULL, NULL) ;

  取得内存设备内容,可以将位图选进内存设备内容,然后使用GDI函数在位图上绘画:                         //暂时不知道怎么用

hdcMem = CreateCompatibleDC (hdc) ;
        
//其它行程序
        
DeleteDC (hdcMem) ;

 取得设备内容信息:

  一个设备内容通常是指一个实际显示设备,如视讯显示器和打印机。通常,您需要取得有关该设备的信息,包括显示器的大小(单位为图素或者实际长度单位)和色彩显示能力。您可以通过呼叫GetDeviceCaps(「取得设备功能」)函数来取得这些信息:

iValue = GetDeviceCaps (hdc, iIndex) ;
//参数iIndex取值为WINGDI.H表头文件中定义的29个标识符之一:
/* Device Parameters for GetDeviceCaps() */
#define DRIVERVERSION 0 /* Device driver version */
#define TECHNOLOGY 2 /* Device classification */
#define HORZSIZE 4 /* Horizontal size in millimeters */
#define VERTSIZE 6 /* Vertical size in millimeters */
#define HORZRES 8 /* Horizontal width in pixels */
#define VERTRES 10 /* Vertical height in pixels */
#define BITSPIXEL 12 /* Number of bits per pixel */
#define PLANES 14 /* Number of planes */
#define NUMBRUSHES 16 /* Number of brushes the device has */
#define NUMPENS 18 /* Number of pens the device has */
#define NUMMARKERS 20 /* Number of markers the device has */
#define NUMFONTS 22 /* Number of fonts the device has */
#define NUMCOLORS 24 /* Number of colors the device supports */
#define PDEVICESIZE 26 /* Size required for device descriptor */
#define CURVECAPS 28 /* Curve capabilities */
#define LINECAPS 30 /* Line capabilities */
#define POLYGONALCAPS 32 /* Polygonal capabilities */
#define TEXTCAPS 34 /* Text capabilities */
#define CLIPCAPS 36 /* Clipping capabilities */
#define RASTERCAPS 38 /* Bitblt capabilities */
#define ASPECTX 40 /* Length of the X leg */
#define ASPECTY 42 /* Length of the Y leg */
#define ASPECTXY 44 /* Length of the hypotenuse */

#define LOGPIXELSX 88 /* Logical pixels/inch in X */
#define LOGPIXELSY 90 /* Logical pixels/inch in Y */

#define SIZEPALETTE 104 /* Number of entries in physical palette */
#define NUMRESERVED 106 /* Number of reserved entries in palette */
#define COLORRES 108 /* Actual color resolution */

 

除此之外,还有一个GetSystemMetrics的函数,在滚动条那篇博客中使用过,它主要是用于得到被定义的系统数据或者系统配置信息..

GetSystemMetrics (Index) ;
#define SM_CXSCREEN             0
#define SM_CYSCREEN             1
#define SM_CXVSCROLL            2
#define SM_CYHSCROLL            3
#define SM_CYCAPTION            4
#define SM_CXBORDER             5
#define SM_CYBORDER             6
#define SM_CXDLGFRAME           7
#define SM_CYDLGFRAME           8
#define SM_CYVTHUMB             9
#define SM_CXHTHUMB             10
#define SM_CXICON               11
#define SM_CYICON               12
#define SM_CXCURSOR             13
#define SM_CYCURSOR             14
#define SM_CYMENU               15
#define SM_CXFULLSCREEN         16
#define SM_CYFULLSCREEN         17
#define SM_CYKANJIWINDOW        18
#define SM_MOUSEPRESENT         19
#define SM_CYVSCROLL            20
#define SM_CXHSCROLL            21
#define SM_DEBUG                22
#define SM_SWAPBUTTON           23
#define SM_RESERVED1            24
#define SM_RESERVED2            25
#define SM_RESERVED3            26
#define SM_RESERVED4            27
#define SM_CXMIN                28
#define SM_CYMIN                29
#define SM_CXSIZE               30
#define SM_CYSIZE               31
#define SM_CXFRAME              32
#define SM_CYFRAME              33
#define SM_CXMINTRACK           34
#define SM_CYMINTRACK           35
#define SM_CXDOUBLECLK          36
#define SM_CYDOUBLECLK          37
#define SM_CXICONSPACING        38
#define SM_CYICONSPACING        39
#define SM_MENUDROPALIGNMENT    40
#define SM_PENWINDOWS           41
#define SM_DBCSENABLED          42
#define SM_CMOUSEBUTTONS        43

#if(WINVER >= 0x0400)
#define SM_CXFIXEDFRAME           SM_CXDLGFRAME  /* ;win40 name change */
#define SM_CYFIXEDFRAME           SM_CYDLGFRAME  /* ;win40 name change */
#define SM_CXSIZEFRAME            SM_CXFRAME     /* ;win40 name change */
#define SM_CYSIZEFRAME            SM_CYFRAME     /* ;win40 name change */

#define SM_SECURE               44
#define SM_CXEDGE               45
#define SM_CYEDGE               46
#define SM_CXMINSPACING         47
#define SM_CYMINSPACING         48
#define SM_CXSMICON             49
#define SM_CYSMICON             50
#define SM_CYSMCAPTION          51
#define SM_CXSMSIZE             52
#define SM_CYSMSIZE             53
#define SM_CXMENUSIZE           54
#define SM_CYMENUSIZE           55
#define SM_ARRANGE              56
#define SM_CXMINIMIZED          57
#define SM_CYMINIMIZED          58
#define SM_CXMAXTRACK           59
#define SM_CYMAXTRACK           60
#define SM_CXMAXIMIZED          61
#define SM_CYMAXIMIZED          62
#define SM_NETWORK              63
#define SM_CLEANBOOT            67
#define SM_CXDRAG               68
#define SM_CYDRAG               69
#endif /* WINVER >= 0x0400 */
#define SM_SHOWSOUNDS           70
#if(WINVER >= 0x0400)
#define SM_CXMENUCHECK          71   /* Use instead of GetMenuCheckMarkDimensions()! */
#define SM_CYMENUCHECK          72
#define SM_SLOWMACHINE          73
#define SM_MIDEASTENABLED       74
#endif /* WINVER >= 0x0400 */

#if (WINVER >= 0x0500) || (_WIN32_WINNT >= 0x0400)
#define SM_MOUSEWHEELPRESENT    75
#endif
#if(WINVER >= 0x0500)
#define SM_XVIRTUALSCREEN       76
#define SM_YVIRTUALSCREEN       77
#define SM_CXVIRTUALSCREEN      78
#define SM_CYVIRTUALSCREEN      79
#define SM_CMONITORS            80
#define SM_SAMEDISPLAYFORMAT    81
#endif /* WINVER >= 0x0500 */
#if(_WIN32_WINNT >= 0x0500)
#define SM_IMMENABLED           82
#endif /* _WIN32_WINNT >= 0x0500 */
#if(_WIN32_WINNT >= 0x0501)
#define SM_CXFOCUSBORDER        83
#define SM_CYFOCUSBORDER        84
#endif /* _WIN32_WINNT >= 0x0501 */

#if(_WIN32_WINNT >= 0x0501)
#define SM_TABLETPC             86
#define SM_MEDIACENTER          87
#define SM_STARTER              88
#define SM_SERVERR2             89
#endif /* _WIN32_WINNT >= 0x0501 */

#if(_WIN32_WINNT >= 0x0600)
#define SM_MOUSEHORIZONTALWHEELPRESENT    91
#define SM_CXPADDEDBORDER       92
#endif /* _WIN32_WINNT >= 0x0600 */

#if(WINVER >= 0x0601)

#define SM_DIGITIZER            94
#define SM_MAXIMUMTOUCHES       95
#endif /* WINVER >= 0x0601 */

#if (WINVER < 0x0500) && (!defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0400))
#define SM_CMETRICS             76
#elif WINVER == 0x500
#define SM_CMETRICS             83
#elif WINVER == 0x501
#define SM_CMETRICS             91
#elif WINVER == 0x600
#define SM_CMETRICS             93
#else
#define SM_CMETRICS             97
#endif

#if(WINVER >= 0x0500)
#define SM_REMOTESESSION        0x1000


#if(_WIN32_WINNT >= 0x0501)
#define SM_SHUTTINGDOWN         0x2000
#endif /* _WIN32_WINNT >= 0x0501 */

#if(WINVER >= 0x0501)
#define SM_REMOTECONTROL        0x2001
#endif /* WINVER >= 0x0501 */

#if(WINVER >= 0x0501)
#define SM_CARETBLINKINGENABLED 0x2002
#endif /* WINVER >= 0x0501 */

#if(WINVER >= 0x0602)
#define SM_CONVERTIBLESLATEMODE 0x2003
#define SM_SYSTEMDOCKED         0x2004
#endif /* WINVER >= 0x0602 */

#endif /* WINVER >= 0x0500 */

具体的每个宏的意思请看百度百科,有中文定义,我不想粘贴过来了.重复工作很蛋疼的.

http://baike.baidu.com/link?url=qeOYGTHX2Gsc8prjTwJzRYglEjbclkeweUXr5w_bITzRrBBKvXlpKV3zb97x6IMaoaeK7dSPgsPDKzGSopuKcq

取回显示屏的色彩信息:

现在的计算机通常用32位来表示3原色,但是高8位不用,从低往高用8位分别表示红,绿,蓝三原色.

可以使用:

RGB (255, 255, 0);

来定义颜色,他是从低位到高位来表示的,三个参数顺序是红,绿,蓝.实际上是:0X0000FFFF.

我们可以通过GetDeviceCaps来获取一些关于颜色的信息:

iPlanes = GetDeviceCaps (hdc, PLANES) ; //传回色彩平面的数目.
iBitsPixel = GetDeviceCaps (hdc, BITSPIXEL) ;//传回每个像素的色彩位数.
iColors = GetDeviceCaps (hdc, NUMCOLORS) ;//传回显示卡可以表示的色彩数.

色彩平面和色彩位数必然有一个是1.第三个取得的是系统的保留色彩,其他的色彩是通过调色盘调出来的.

还有一些其他的函数:

取到一个RGB定义的颜色的不同三原色值:GetRValue、GetGValue和GetBValue.

在混色中获得最接近的纯色:

crPureColor = GetNearestColor (hdc, crColor) ;

关于文章里面的代码传到github上面,这次主要是GetDeviceCaps:

https://github.com/shangbo/windows_api/tree/master/GetDviceCaps

原文地址:https://www.cnblogs.com/SoulReaper/p/3321755.html