Department of Energy Argonne National Laboratory Office of Science NEWTON's Homepage NEWTON's Homepage
NEWTON, Ask A Scientist!
NEWTON Home Page NEWTON Teachers Visit Our Archives Ask A Question How To Ask A Question Question of the Week Our Expert Scientists Volunteer at NEWTON! Frequently Asked Questions Referencing NEWTON About NEWTON About Ask A Scientist Education At Argonne VB6 Routine
Name: Thoas
Status: Student
Age: 40s
Location: N/A
Country: N/A
Date: Around 1999


Question:
I have been through the advanced class of Visual Basic 6, looked in as many books as I could find, went to Microsoft's technet on the web and the best answer that I received was to not do it. Stupid! I am looking for a way in VB6 to auto-detect screen pixels (640 x 480) and color depth. There must be a way.



Replies:
First of all, I have to agree that attempting to change your code based what the user has set up as their screen resolution and pixel depth is not a profitable endeavor. There are more than 25 different resolutions you would have to account for.

That said, hopefully here is some help in getting what you need.

You need to call a windows function which is stored in a .dll, called GetDeviceCaps.

This is how it would look like in C.

// Find out how many colors their desktop is set to HDC
hdc = GetDC(NULL);
bpp = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL);
ReleaseDC(NULL, hdc);

width = GetDeviceCaps(hdc, HORZRES);
height = GetDeviceCaps(hdc, VERTRES);

For visual basic, you will still want to call the same GetDeviceCaps function.

From the MSDN Documentation:

Passing Properties- Properties must be passed by value. If an argument is declared with ByVal, you can pass the property directly. For example, you can determine the dimensions of the screen or printer in pixels with this procedure:
Declare Function GetDeviceCaps Lib "gdi32" Alias _
"GetDeviceCaps" (ByVal hdc As Long, _
ByVal nIndex As Long) As Long

You can also pass the hDC property of a form or the Printer object to this procedure to obtain the number of colors supported by the screen or the currently selected printer. For example:
Private Sub Form_Click ()
Const PLANES = 14, BITS = 12
   Print "Screen colors ";
   Print GetDeviceCaps(hDC, PLANES)* 2 ^ _
   GetDeviceCaps(hDC, BITS)
   Print "Printer colors ";
   Print GetDeviceCaps(Printer.hDC, PLANES) * _
   2 ^ GetDeviceCaps(Printer.hDC, BITS)
End Sub

Some constants that you will need:
Const HORZRES   =  8
Const VERTRES   =  10
Const BITSPIXEL =  12
Const PLANES    =  14
Const NULL          =  0

Also, you will need to declare the GetDC and ReleaseDC functions
Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long,
    ByVal hDC As Long) As Long

Dim hDC As Long
    hDC = GetDC(0)
    ReleaseDC 0, hDC
End Sub

I hope this helps you with your problem.

--Eric Tolman


Click here to return to the Computer Science Archives

NEWTON is an electronic community for Science, Math, and Computer Science K-12 Educators, sponsored and operated by Argonne National Laboratory's Educational Programs, Andrew Skipor, Ph.D., Head of Educational Programs.

For assistance with NEWTON contact a System Operator (help@newton.dep.anl.gov), or at Argonne's Educational Programs

NEWTON AND ASK A SCIENTIST
Educational Programs
Building 360
9700 S. Cass Ave.
Argonne, Illinois
60439-4845, USA
Update: June 2012
Weclome To Newton

Argonne National Laboratory