how can I retrieve the number of colors/bits per pixel used/avaliable by the system 
Author Message
 how can I retrieve the number of colors/bits per pixel used/avaliable by the system

how can I retrieve the number of colors(or bits per pixel) used by the
system?

thanks,
Christoph



Tue, 16 Nov 2004 06:51:24 GMT  
 how can I retrieve the number of colors/bits per pixel used/avaliable by the system

Quote:
> how can I retrieve the number of colors(or bits per pixel) used by the
> system?

Use the GetDeviceCaps() API call on the desktop window's DC (Use
GetDesktopwindow()) with the BITSPIXEL flag.
Hops this helps,

    Mike

 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: Http://EDais.earlsoft.co.uk/




Tue, 16 Nov 2004 06:48:00 GMT  
 how can I retrieve the number of colors/bits per pixel used/avaliable by the system
Thank you,

unfortunately I did not ge{*filter*}work.
I've written the following code:

Option Explicit
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex
As Long) As Long

Sub test
 Dim dc as Long
 Dim BitsPerPixel As Long

 dc = GetDesktopWindow()
 BitsPerPixel = GetDeviceCaps(dc, 12)
End Sub

But BitsPerPixel contains 0.
And any other value I use for the flags parameter, always 0 is returned.
Do you have any idea?

thank you,
Christoph



Quote:
> > how can I retrieve the number of colors(or bits per pixel) used by the
> > system?

> Use the GetDeviceCaps() API call on the desktop window's DC (Use
> GetDesktopwindow()) with the BITSPIXEL flag.
> Hops this helps,

>     Mike

>  -- EDais --

>  - Microsoft Visual Basic MVP -
> WWW: http://www.*-*-*.com/





Tue, 16 Nov 2004 08:10:07 GMT  
 how can I retrieve the number of colors/bits per pixel used/avaliable by the system

Quote:
> unfortunately I did not ge{*filter*}work.
> I've written the following code:

<Snip>

Quote:
> But BitsPerPixel contains 0.
> And any other value I use for the flags parameter, always 0 is returned.
> Do you have any idea?

If you look at it's declare statement, GetDeviceCaps() takes an hDC, not a
hWnd - You need to use GetDC() on the desktop window's handle but remember
to release the DC when you're done:

'***
Private Sub Form_Load()
    Dim DeskWnd As Long, DeskDC As Long

    Const BITSPIXEL = 12 'Number of bits per pixel

    DeskWnd = GetDesktopWindow()
    DeskDC = GetDC(DeskWnd)
    MsgBox GetDeviceCaps(DeskDC, BITSPIXEL)
    ReleaseDC DeskWnd, DeskDC
End Sub
'***

Also, use the constant rather than putting in 12 - It makes for far more
readable code.  If you think adding constants to the header looks messy,
then you can put the constant in-line as I've demonstrated above if you're
only ever using it in one place.
Hope this helps,

    Mike

 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: http://www.*-*-*.com/




Tue, 16 Nov 2004 07:27:05 GMT  
 how can I retrieve the number of colors/bits per pixel used/avaliable by the system
On Fri, 31 May 2002 00:27:05 +0100, "Mike D Sutton"

Quote:

>...You need to use GetDC() on the desktop window's handle...

Passing 0 to GetDC implies the desktop/screen, so it can be simplified
a bit by eliminating the GetDesktopWindow call...

Public Function ColorDepth() As Long
    Dim DC As Long

    DC = GetDC(0)
    ColorDepth = GetDeviceCaps(DC, BITSPIXEL)
    ReleaseDC 0, DC
End Function

-Tom
(please post replies to the newsgroup)



Tue, 16 Nov 2004 09:22:44 GMT  
 how can I retrieve the number of colors/bits per pixel used/avaliable by the system

Quote:
> Passing 0 to GetDC implies the desktop/screen, so it can be simplified
> a bit by eliminating the GetDesktopWindow call...

<snip>

Again I find my method more immediately understandable and readable since
it's evident we've querying the desktop window's DC for it's bit-depth, but
it's up to the programmer I guess.

    Mike

 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: Http://EDais.earlsoft.co.uk/




Tue, 16 Nov 2004 09:33:15 GMT  
 how can I retrieve the number of colors/bits per pixel used/avaliable by the system
Yeah,
this does work now. Thank you.
(Sure, it's better to use constants)



Quote:
> > unfortunately I did not ge{*filter*}work.
> > I've written the following code:

> <Snip>

> > But BitsPerPixel contains 0.
> > And any other value I use for the flags parameter, always 0 is returned.
> > Do you have any idea?

> If you look at it's declare statement, GetDeviceCaps() takes an hDC, not a
> hWnd - You need to use GetDC() on the desktop window's handle but remember
> to release the DC when you're done:

> '***
> Private Sub Form_Load()
>     Dim DeskWnd As Long, DeskDC As Long

>     Const BITSPIXEL = 12 'Number of bits per pixel

>     DeskWnd = GetDesktopWindow()
>     DeskDC = GetDC(DeskWnd)
>     MsgBox GetDeviceCaps(DeskDC, BITSPIXEL)
>     ReleaseDC DeskWnd, DeskDC
> End Sub
> '***

> Also, use the constant rather than putting in 12 - It makes for far more
> readable code.  If you think adding constants to the header looks messy,
> then you can put the constant in-line as I've demonstrated above if you're
> only ever using it in one place.
> Hope this helps,

>     Mike

>  -- EDais --

>  - Microsoft Visual Basic MVP -
> WWW: http://www.*-*-*.com/





Tue, 16 Nov 2004 08:42:12 GMT  
 how can I retrieve the number of colors/bits per pixel used/avaliable by the system

Quote:
> > Passing 0 to GetDC implies the desktop/screen, so it can be simplified
> > a bit by eliminating the GetDesktopWindow call...
> Again I find my method more immediately understandable and readable since
> it's evident we've querying the desktop window's DC for it's bit-depth,
but
> it's up to the programmer I guess.

For completeness, here is the VBnet version (Randy Birch's site).  Which
avoids the first API call altogether and just passes in hdc (which implies
me.hdc).

http://www.mvps.org/vbnet/index.html?code/screen/displaysettings.htm

This made me curious so I wrote a small test.  Just paste this in to a form
and you can see the results in the immediate window when the form loads:

'************
Option Explicit
Private Declare Function GetDesktopWindow Lib "User32" () As Long
Private Declare Function GetDC Lib "User32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "User32" (ByVal hWnd As Long, ByVal
hDC As Long) As Long

Private Sub Form_Load()
Debug.Print "me hdc =" & Me.hDC
Debug.Print "hdc = " & hDC
Debug.Print "form1.hdc = " & Form1.hDC

Dim deskHwnd As Long
Dim deskHDC As Long

deskHwnd = GetDesktopWindow()
deskHDC = GetDC(deskHwnd)
Debug.Print "api desktop hdc = " & hDC
Call ReleaseDC(deskHwnd, deskHDC)

End Sub
'*************

Notice that all hdc values are identical.  So my conclusion is that the only
time you would need to call an API for this is if you wanted to try to
support multiple monitors with different resolutions via
EnumDisplayMonitors() or some such.  Just use hDC - it works!

Also, according to the docs, Tom is correct - passing 0& to GetDC should
return the Screen DC:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/...
_4esj.asp

However the DC I get when I pass a 0& is different from the DC I get from
the API when I pass GetDeskTopWindow().  This has bitten me in the past and
I wonder if some graphics guru out there could explain it to me???

To illustrate, the above sample code returns a different DC value if I use
GetDC(0) - try it!

FWIW,
Ray Mercer
MS-MVP Visual Basic



Sun, 21 Nov 2004 10:07:20 GMT  
 how can I retrieve the number of colors/bits per pixel used/avaliable by the system
<snip>

Quote:
> Notice that all hdc values are identical.  So my conclusion is that the
only
> time you would need to call an API for this is if you wanted to try to
> support multiple monitors with different resolutions via
> EnumDisplayMonitors() or some such.  Just use hDC - it works!

Interesting, I'd never seen that one before - Another 'undocumented' feature
of VB, huh? ;)

Quote:
> Also, according to the docs, Tom is correct - passing 0& to GetDC should
> return the Screen DC:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/...

Quote:
> _4esj.asp

I've had problems with GetDC(0) in the past and stayed away from it ever
since (I most likely just made a mistake elsewhere in the code or something!
;) but as long as it works I don't mind typing the extra line and I still
maintain it makes more readable code.  The one thing that you must avoid is
this:

'***
DeskDC = GetDC(GetDesktopWindow())
ReleaseDC GetDesktopWindow(), DeskDC
'***

Since as you mentioned the desktop window handle changes so could result in
unreleased desktop handles.

Quote:
> However the DC I get when I pass a 0& is different from the DC I get from
> the API when I pass GetDeskTopWindow().  This has bitten me in the past
and
> I wonder if some graphics guru out there could explain it to me???

The multiple desktop window DC's came up a while back (Years ago.. - I think
I spend too much time here :) and I believe it was explained as there being
a pool of available DC handles available to the desktop and it just cycles
through them.  I wrote a quick test app and it looks that there are 8
handles which are cycled through presumably when the desktop requires a
redraw.
If you want the app then I'll post it here

    Mike

 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: Http://EDais.earlsoft.co.uk/




Sun, 21 Nov 2004 10:47:20 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Save BMP as 1 or 4 bits per pixel

2. (XP icons) 32-bits-per-pixel Icons in a Image control

3. hDC: bits-per-pixel based on display?

4. How to change True color pixel to 256 color pixel, @Thanks

5. Retrieving the color of pixels of certain coordinates in a picture box

6. Determining color of Pixel/Using C++ class CRgn

7. per pixel alpha blending

8. per pixel alpha blending

9. ANN: New Driving DirectX article: Per-Pixel Lighting

10. painting per pixel

11. API to get Twips per pixel ?

12. Pixels Per Inch (Final Leg Of Shading)

 

 
Powered by phpBB® Forum Software