Solved

Detect the resolution and change it.

Posted on 2000-04-03
27
210 Views
Last Modified: 2010-05-02
MY application is meant for 800*600 resolution. If the user is in any other resolution, then all my icons,pictures everything looks ugly.
So what i want is before starting the application, i will check the resolution and if it's not 800*600, then give a error message with ok and cancel options.
If the user clicks ok, then i want to change the res to 800*600 directly without going to display settings.
how to do it?
thanks in advacne.
mskannan
0
Comment
Question by:mskannan
  • 8
  • 8
  • 6
  • +3
27 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 2679346
STOP right there.
"If the user is in any other resolution, then all my icons,pictures everything looks ugly."

Fine. Fix your application, *NOT* your user's resolution.

Wake up to the real world.... In this day and age, people have monitors of size 17" and up. Forcing them to have 800x600 resolution is ridiculous.

Sorry..... but that honestly is the best advice anyone can give you.

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2679879
To create resolution-independant application, see this:
http://support.microsoft.com/support/kb/articles/Q182/0/70.ASP?LNG=ENG&SA=ALLKB&FR=0
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 2680130
Add a command button and a listbox to a form and copy this code:
Option Explicit

Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32

Private Type DEVMODE
    dmDeviceName As String * CCHDEVICENAME
    dmSpecVersion As Integer
    dmDriverVersion As Integer
    dmSize As Integer
    dmDriverExtra As Integer
    dmFields As Long
    dmOrientation As Integer
    dmPaperSize As Integer
    dmPaperLength As Integer
    dmPaperWidth As Integer
    dmScale As Integer
    dmCopies As Integer
    dmDefaultSource As Integer
    dmPrintQuality As Integer
    dmColor As Integer
    dmDuplex As Integer
    dmYResolution As Integer
    dmTTOption As Integer
    dmCollate As Integer
    dmFormName As String * CCHFORMNAME
    dmUnusedPadding As Integer
    dmBitsPerPel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
End Type

Private Const DM_BITSPERPEL = &H40000
Private Const DM_PELSWIDTH = &H80000
Private Const DM_PELSHEIGHT = &H100000
Private Const DM_DISPLAYFLAGS = &H200000
Private Const DM_DISPLAYFREQUENCY = &H400000

Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpInitData As DEVMODE, ByVal dwFlags As Long) As Long
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (lpszDeviceName As Any, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long

Private Const BITSPIXEL = 12

' Flags for ChangeDisplaySettings
Private Const CDS_UPDATEREGISTRY = &H1
Private Const CDS_TEST = &H2
Private Const CDS_FULLSCREEN = &H4
Private Const CDS_GLOBAL = &H8
Private Const CDS_SET_PRIMARY = &H10
Private Const CDS_RESET = &H40000000
Private Const CDS_SETRECT = &H20000000
Private Const CDS_NORESET = &H10000000

' Return values for ChangeDisplaySettings
Private Const DISP_CHANGE_SUCCESSFUL = 0
Private Const DISP_CHANGE_RESTART = 1
Private Const DISP_CHANGE_FAILED = -1
Private Const DISP_CHANGE_BADMODE = -2
Private Const DISP_CHANGE_NOTUPDATED = -3
Private Const DISP_CHANGE_BADFLAGS = -4
Private Const DISP_CHANGE_BADPARAM = -5

Private Const EWX_LOGOFF = 0
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4

Private D() As DEVMODE
Private lNumModes As Long

Private Sub Command1_Click()
Dim l As Long
Dim Flags As Long
Dim x As Long

    x = List1.ListIndex
    D(x).dmFields = DM_BITSPERPEL Or DM_PELSWIDTH Or DM_PELSHEIGHT
    Flags = CDS_UPDATEREGISTRY
    l = ChangeDisplaySettings(D(x), Flags)
    Select Case l
        Case DISP_CHANGE_RESTART
            l = MsgBox("This change will not take effect until you reboot the system. Reboot now?", vbYesNo)
            If l = vbYes Then
                Flags = 0
                l = ExitWindowsEx(EWX_REBOOT, Flags)
            End If
        Case DISP_CHANGE_SUCCESSFUL
        Case Else
            MsgBox "Error changing resolution! Returned: " & l
    End Select
End Sub

Private Sub Form_Load()
Dim l As Long
Dim lMaxModes As Long
Dim lBits As Long
Dim lWidth As Long
Dim lHeight As Long

    lBits = GetDeviceCaps(hdc, BITSPIXEL)
    lWidth = Screen.Width \ Screen.TwipsPerPixelX
    lHeight = Screen.Height \ Screen.TwipsPerPixelY
    lMaxModes = 8
    ReDim D(0 To lMaxModes) As DEVMODE
    lNumModes = 0
    l = EnumDisplaySettings(ByVal 0, lNumModes, D(lNumModes))
    Do While l
        List1.AddItem D(lNumModes).dmPelsWidth & "x" & D(lNumModes).dmPelsHeight & "x" & D(lNumModes).dmBitsPerPel
        If lBits = D(lNumModes).dmBitsPerPel And _
            lWidth = D(lNumModes).dmPelsWidth And _
            lHeight = D(lNumModes).dmPelsHeight Then
            List1.ListIndex = List1.NewIndex
        End If
        lNumModes = lNumModes + 1
        If lNumModes > lMaxModes Then
            lMaxModes = lMaxModes + 8
            ReDim Preserve D(0 To lMaxModes) As DEVMODE
        End If
        l = EnumDisplaySettings(ByVal 0, lNumModes, D(lNumModes))
    Loop
    lNumModes = lNumModes - 1
End Sub
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2680166
This Q comes up all the time. You should NOT automatically resize. Set your program to run and look good at a known rez and leave it at that. The users are going to higher rez to get more on the screen, it's incorrect to automatically glom up more space.

M
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2680195
Exactly Exactly ex*ACT*ly..

emoreau, your code will undoubtedly work but I wish you hadn't told mskannan..

Being *ABLE* to do something does not mean you *SHOULD* do it....
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2680211
caraf_g,

If you look at the comment before my answer, I gave a link to a correct way of handling this situation!
0
 
LVL 6

Expert Comment

by:MELeBlanc
ID: 2680956
I guess everyone here went with the assumption that mskannan was forcing them to a smaller resolution?

Where I presently work, we still have a BOAT load of users that are still using 640X480 on their 17" monitors.  Unfortunately, with the complexity of the applications they're asking for.. sometimes this just doesn't give enough "Desktop Real Estate" to give them what they need.

I don't force the 800X600 resolution on them (which _IS_ the company destop standard), but I nudge them in that direction..  ;-)

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2681765
Geez, yes, you're right! It never even entered my mind that someone might actually be trying to run the program in 640x480... how quaint <G>
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2682339
Well sonny, I 'member the early daze of windoze. Yep, back in them daze we only had 640x480x16 colors and we were lucky to have that! All our DOS friends were stuck at 80x25 monochrome and 640x480 was *something* yessiree Bob! (Loud "ptui" sound followed by *gong* as glob of chaw hits spittoon)

(grin)

M
0
 
LVL 4

Expert Comment

by:wileecoy
ID: 2682455
emoreau,

No matter what they say - Great Answer.

Both suggestions work.  He has two options.  He can pick what he wants.

Although I agree that the MS article is the better choice as changing the resolution asks for problems, I feel it is better to answer the question that was asked.

Just my thoughts, insignificant, though they may be <g>
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2682850
Yes... fond memories. mark, I'm old enough to remember vaguely the ... er... what were they called again.... ZX80, wasn't it? Cute little green on black screens, and to save a 1KB program you had to connect a tape recorder with an audio tape in it to the machine. Those were the days.... I still remember when this new-fangled thing called "DOS" came out......

Must say the PC games were better back then. Ever played the original "Frogger"?
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2682874
wileecoy,

I'm in no doubt that emoreau's suggestions are well thought out and work perfectly. What I am saying though is that you should not do it. Let me put it this way..... If I bought a piece of software and it would try to change my display settings to something other than *MY* preferences, I would go back to the vendor and demand my money back. Trust me, the worst thing an application can do is to mess with a user's preferences. There is nothing more guaranteed to seriously piss the punter off.
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2683465
I have to agree with caraf_g, changing the rez on a user is a sure path to customer complaints and returned product.

Is a really helpful feature. You have a user that has icons all over the edges of their screen at a high rez. You step them down a notch and now their icons are either off the screen or re-arrainged. When you return the rez to its original settings (you *were* planning on restoring the settings on exit?) their set of carefully placed icons are now scattered.

Additionally yours is not the *only* program that might be active when you decide to "help" by resetting the screen size. So perhaps you wind up hiding the URHC of some program that they already have open.

One more thought, What if *every* program did this to users - you're concept is so good that everyone jumps on the automatic resize bandwagon. Only problem is that if you try an run two programs that *both* want to resize but at two different settings... then what? Screen jumping from one rez to another each time you task switch? (kind of like how DOS mode windows behave - annoying ain't it?) Or does the user get that lovely "You must reboot for these changes to take effect" every time they fire your app?

No, Make the screen look pretty at the lowest rez you feel comfortable at and live with it being smaller at higher rez's and put a "minimum rez = xxx/yyyy" label on the box.

M
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2683474
So mskannan, are you satisfied?
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2683520
caraf_g,

I go back earlier than the Sinclair! I started out when a 1 was a hole in a piece of paper!

(senile sounding, high pitched voice)

Yep sonny them was 'he daze. You made programs the best way, FORTRAN-IV on 80 column cards or miles of that dad-gum ASR-33 punched paper tape. Yep, 029 keypunch, now *that* was computing power! None of this sissy boot ROM stuff, you started your 'puter in the morning by flippling toggle switches and then doing a 7-card loader IPL! This was back in the days when the Apple ][ was the cat's meow and Apple hadn't figured out LOWERCASE yet. Yessiree, my *first* home computer was a S-100 bus 1Mhz 6802 with a whopping 16k of RAM! (And we watched every byte in them daze, nothin' like being 4 bytes over the amount of RAM you had!)

Yep, had to write my own BIOS. Peripherial cards? You wanted one, you got out your wire-wrap gun and by gum you "rolled your own". For "mass storage" we had 300 baud "Kansas City" tape!

At work on the "big iron" (SEL 32/55) we had a newfangled thing, a 80Mb hard drive. Oh it were great, about the size of a "top loader" washing machine with them big 14" platters you could actually *see* the bits! (true, we had a magnetic flux viewer that you could place against media and *see* data!)

(loud maniacle cackle and toothless lip smacking sounds...)

M
0
 
LVL 6

Expert Comment

by:MELeBlanc
ID: 2683636
Oh man.... did I turn this thread in a new direction or what?!  ;-)

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2683946
HahAA!

WELL.... my daddy invented the abacus

So there.
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2684026
(grin)

Yeah, but my story is *true*!

(big toothy grin)

In all honesty I started in computer in jr. high back in early 70s. Learned to program on a timeshare (now known as client/server) dialup *acoustic* modem on a IBM360-65. Had a Selectric typewriter with an "APL" ball on it. We also had a ASR-33 for dialin on another system. First "small" computer was a Wang "micro". Programmed in Basic and was small enough to fit on *two* A/V carts. Had an 8k *CORE* memory module the size of a pullman suitcase tied to the CPU that was the size of a two drawer filing cabinet. Was *hot*!

Have lost count on the number of different machines I've cut code for. Everthing from embedded 6502's to Z8000's, 68x000's, IBM, Dec, SEL, Honeywell, and every sort of PC. Seems like most folx believe that computing started with the PC...

M
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2684140
I concede defeat. You are a true veteran!
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2684332
(grin)

M
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2684374
...but I'd still like to see you square your comment "(big toothy grin)" with "toothless lip smacking sounds..."

0
 
LVL 12

Expert Comment

by:mark2150
ID: 2684430
Sound effects are dubbed in...

M
0
 

Author Comment

by:mskannan
ID: 2685970
hi all,
thanks for your comments,
i also doesn't want to implement this feature in my application. I simply put that "This application needs 800*600 resolution for best performance" in the requirements section and gave the appln for the user acceptance test.
During the tests, the end users said that if the system is not running in 800*600, the appln should detect and give a warning and give the facility to change the resolution with a click of a mouse button.
So I have no option other than just lodge a protest and implement.
That's how this question propped up.
One more thing, how is the task bar afffects this screen size. Because some times task bar comes and the screen adjusts automatically and some other times even if the task bar is missing still the form size is total screen  minus taskbar?
why this behaviour?
any help.
as of emoreau's answer, it's working for me and i am awarding the points.

Thank you all for some nice discussion.

mskannan
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2686028
To know real screen dimensions, use this:


Private Declare Function SystemParametersInfo _
                Lib "user32" _
                Alias "SystemParametersInfoA" _
                (ByVal uAction As Long, _
                 ByVal uParam As Long, _
                 lpvParam As Any, _
                 ByVal fuWinIni As Long _
                ) As Long

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
   
Private Const SPI_GETWORKAREA = 48

Dim lngReturn As Long
Dim WA As RECT
    'Get screen dimension
    lngReturn = SystemParametersInfo(SPI_GETWORKAREA, 0&, WA, 0&)
    WA.Left = WA.Left * Screen.TwipsPerPixelX
    WA.Right = WA.Right * Screen.TwipsPerPixelX
    WA.Top = WA.Top * Screen.TwipsPerPixelY
    WA.Bottom = WA.Bottom * Screen.TwipsPerPixelY
0
 

Author Comment

by:mskannan
ID: 2688355
hi emoreau,
your code lists all the possible combinations of resolution in the user's computer.
but what i want is the current resolution the user is having and check whether it's 800*600 or give an option to change
how to change the code?
mskannan


0
 
LVL 12

Expert Comment

by:mark2150
ID: 2689394
Take Screen.Width and divide by Screen.TwipsPerPixelX and .Height divided by Screen.TwipsPerPixelY to get X and Y resolution of screen.

M
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2689567
Have you looked at this condition:
        If lBits = D(lNumModes).dmBitsPerPel And _
            lWidth = D(lNumModes).dmPelsWidth And _
            lHeight = D(lNumModes).dmPelsHeight Then
            List1.ListIndex = List1.NewIndex
        End If

This line identifies the current config.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now