Set desktop metrics

Just starting out? Need help? Post your questions and find answers here.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Set desktop metrics

Post by netmaestro »

Is there a good way to set desktop settings, like height/width/frequency?
Last edited by netmaestro on Tue Feb 21, 2006 7:53 pm, edited 2 times in total.
BERESHEIT
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Code: Select all

;Open Display Properties, Settings tab.
RunProgram("RUNDLL32.EXE","SHELL32.DLL,Control_RunDLL desk.cpl,@0,3","")
The most user and system friendly way.
Let the user adjust it themselfs using whatever driver/consol stuff they may have.

The only alternative way, is to open your own screen using a different resolution.

Personaly I would not recommend any other ways or "hacks" to switch desktop resolution on a program to program basis.
Windows tends to re-arrange desktop stuff/layout etc when swapping resolutions.
So let the user do it themselfs instead using a interface they are familiar with.
(Display control panel).
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

From Purearea.net:

Code: Select all

; www.purearea.net (Sourcecode collection by cnesm)
; Author: Andreas
; Date: 22. November 2003

;############################# 
;Author : Andreas 
;16.06.2003 
;############################# 

NewList Display.DEVMODE() 

#DM_PELSHEIGHT = $100000 
#DM_PELSWIDTH = $80000 
#DM_BITSPERPEL = $40000 
#DM_DISPLAYFLAGS = $200000 
#DM_DISPLAYFREQUENCY = $400000 
#DISP_CHANGE_RESTART = 1 
#CDS_UPDATEREGISTRY = 1 

Procedure.s ReadRegKey(OpenKey.l,SubKey.s,ValueName.s) 
    hKey.l=0 
    keyvalue.s=Space(255) 
    datasize.l=255 
    If RegOpenKeyEx_(OpenKey,SubKey,0,#KEY_READ,@hKey) 
        keyvalue="Fehler. Kann Registry nicht lesen" 
    Else 
        If RegQueryValueEx_(hKey,ValueName,0,0,@keyvalue,@datasize) 
            keyvalue="Fehler. Kann Schlüssel nicht lesen" 
        Else 
            keyvalue=Left(keyvalue,datasize-1) 
        EndIf 
        RegCloseKey_(hKey) 
    EndIf 
    ProcedureReturn keyvalue 
EndProcedure 


Procedure.s GetCurrentSettings() 
    c.s=ReadRegKey(#HKEY_LOCAL_MACHINE,"Config\0001\Display\Settings","BitsPerPixel") 
    wh.s=ReadRegKey(#HKEY_LOCAL_MACHINE,"Config\0001\Display\Settings","Resolution") 
    If c = "4" 
        co$ = "16 Farben" 
    ElseIf c = "8" 
        co$ = "256 Farben" 
    ElseIf c = "16" 
        co$ = "HightColor" 
    ElseIf c = "32" 
        co$ = "TrueColor" 
    EndIf 
    retstring.s = StringField(wh,1,",") + " * " + StringField(wh,2,",") + "  -  " + co$ 
    ProcedureReturn retstring 
EndProcedure 

If OpenWindow(0, 100, 200, 295, 80, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, "Change Display-Setting") 
    
    If CreateGadgetList(WindowID()) 
        CB = CreateWindowEx_($200,"COMBOBOX","",#WS_CHILD|#WS_VISIBLE|#CBS_DROPDOWN|#WS_VSCROLL ,2,10,290,400,WindowID(),100,GetModuleHandle_(0),0) 
        SendMessage_(CB,#WM_SETFONT,GetStockObject_(#DEFAULT_GUI_FONT),1) 
        ButtonGadget(1, 2, 44, 290,24,"Change Display") 
    EndIf 
    
    While EnumDisplaySettings_(0,x,dev.DEVMODE) 
        If dev\dmBitsPerPel = 4 
            f$ = "16 Farben" 
        ElseIf dev\dmBitsPerPel = 8 
            f$ = "256 Farben" 
        ElseIf dev\dmBitsPerPel = 16 
            f$ = "HightColor" 
        ElseIf dev\dmBitsPerPel = 32 
            f$ = "TrueColor" 
        EndIf 
        AddElement(Display()) 
        Display()\dmSize = SizeOf(DEVMODE) 
        Display()\dmFields = #DM_PELSHEIGHT|#DM_PELSWIDTH|#DM_BITSPERPEL|#DM_DISPLAYFLAGS|#DM_DISPLAYFREQUENCY 
        Display()\dmBitsPerPel = dev\dmBitsPerPel 
        Display()\dmPelsWidth = dev\dmPelsWidth 
        Display()\dmPelsHeight = dev\dmPelsHeight 
        Display()\dmDisplayFlags = dev\dmDisplayFlags 
        Display()\dmDisplayFrequency = dev\dmDisplayFrequency 
        dd$ = Str(dev\dmPelsWidth) + " * " + Str(dev\dmPelsHeight) + "  -  " + f$ 
        x + 1 
        SendMessage_(CB,#CB_ADDSTRING,0,dd$) 
    Wend 
    
    SendMessage_(CB,#CB_SETCURSEL,SendMessage_(CB,#CB_FINDSTRING,0,GetCurrentsettings()),0) 
    InvalidateRect_(WindowID(),0,0) 
    
    Repeat 
        EventID.l = WaitWindowEvent() 
        If EventID = #PB_Event_CloseWindow 
            Quit = 1 
        EndIf 
        If EventID = #PB_EventGadget 
            Select EventGadgetID() 
            Case 1 
                SelectElement(Display(),SendMessage_(CB,#CB_GETCURSEL,0,0)) 
                If Display()\dmBitsPerPel = 4 
                    f$ = "16 Farben" 
                ElseIf Display()\dmBitsPerPel = 8 
                    f$ = "256 Farben" 
                ElseIf Display()\dmBitsPerPel = 16 
                    f$ = "HightColor" 
                ElseIf Display()\dmBitsPerPel = 32 
                    f$ = "TrueColor" 
                EndIf 
                d$ = Str(Display()\dmPelsWidth) + " * " + Str(Display()\dmPelsHeight) + "  -  " + f$ 
                If d$ <> GetCurrentSettings() 
                    If ChangeDisplaySettings_(Display(),#CDS_UPDATEREGISTRY) = #DISP_CHANGE_RESTART 
                        MessageRequester("Meldung","Neustart nötig",64) 
                    EndIf 
                Else 
                    MessageRequester("Meldung","Nichts zu ändern",64) 
                EndIf 
            EndSelect 
        EndIf 
    Until Quit = 1 
EndIf 
End 

; ExecutableFormat=Windows
; EnableXP
; EOF
I may look like a mule, but I'm not a complete ass.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Just what I need. Thanks!
Last edited by netmaestro on Tue Feb 21, 2006 7:54 pm, edited 2 times in total.
BERESHEIT
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Pretty cool srod, but why the need to restart?
Windows (at least later variants) do not need a restart at display changes.

Oh nm. I looked over it a bit too fast, your checking if a restart is needed :)
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Code: Select all

dtw=GetSystemMetrics_(#SM_CXSCREEN)
dth=GetSystemMetrics_(#SM_CYSCREEN)
I notice you are using these API commands. Did you know there is a cross platform Desktop library now?
--Kale

Image
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Code from DarkDragon & Gillou

Code: Select all

#DISP_CHANGE_SUCCESSFUL = 0   ; The settings change was successful.
#DISP_CHANGE_RESTART = 1      ; The computer must be restarted in order for the graphics mode to work.
#DISP_CHANGE_BADFLAGS = -4    ; An invalid set of flags was passed in.
#DISP_CHANGE_FAILED = -1      ; The display driver failed the specified graphics mode.
#DISP_CHANGE_BADMODE = -2     ; The graphics mode is not supported.
#DISP_CHANGE_NOTUPDATED = -3  ; Windows NT only: Unable to write settings to the registry.

ProcedureDLL ChangeDisplaySettings(width,height,Depth,Freq,Permanent)
  dmScreenSettings.DEVMODE 
  dmScreenSettings\dmSize = SizeOf(dmScreenSettings) 
  dmScreenSettings\dmPelsWidth = width
  dmScreenSettings\dmPelsHeight = height
  dmScreenSettings\dmBitsPerPel = Depth
  dmScreenSettings\dmDisplayFrequency=Freq
  dmScreenSettings\dmFields = 262144 | 524288 | 1048576 
  
  If Permanent
    retour=ChangeDisplaySettings_(@dmScreenSettings, 1) 
  Else
    retour=ChangeDisplaySettings_(@dmScreenSettings, 4) 
  EndIf
  
  ProcedureReturn retour
EndProcedure

;/ Test
ChangeDisplaySettings(1024,768,16,70,0)
MessageRequester("Change Display Settings","OK to Restore")
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

And for Desktop Enumeration ( Gillou Code )

Code: Select all

ProcedureDLL.s EnumDisplay() 
  Static b
  If b > -1
    If EnumDisplaySettings_(0,b,dmEcran.DEVMODE)
      retour.s=Str (dmEcran\dmPelsWidth)+","+ Str (dmEcran\dmPelsHeight)+","+ Str (dmEcran\dmBitsPerPel) + ","+Str(dmEcran\dmDisplayFrequency)
    Else
      b=-2
    EndIf
  EndIf
  b=b+1
  ProcedureReturn retour
EndProcedure

;/ Test
x.s=EnumDisplay()
While x<>""
  Debug x
  x.s=EnumDisplay()
Wend
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Why not using the desktop lib ?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

But can the PB desktop library actually change the settings? As far as I can tell it only returns the various settings etc.
I may look like a mule, but I'm not a complete ass.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

another very user-friendly is to let the resolution unmodified and to encapsulate the entire gadgets list in a scrollareagadget(). this way, if resolution is good the scrollbars are not visible and if resolution is not adapted the scrollbars become visibles. :roll:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Perhaps a future PB version will have Set capabilities in the desktop lib?
Last edited by netmaestro on Tue Feb 21, 2006 7:55 pm, edited 2 times in total.
BERESHEIT
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

:twisted:\ What if they have multiple monitors with seperate desktops at different resolutions?
Maybe the 2nd desktop has a higher resolution?
Or maybe the user has a odd setup, where the width is 800 but the height is 1200 ? /:twisted:
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I think a method with auto-revert is important, the last thing you want to do is garble a user's screen so he can't fix it.
Last edited by netmaestro on Tue Feb 21, 2006 7:57 pm, edited 2 times in total.
BERESHEIT
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

I must admit I think Flype's suggestion is the way to go. My previous monitor was worn out and useless above the screen resolution settings
I used for it, even though you could switch it to a much higher setting.

So if a program had done that, I would be in trouble, and the program would have been uninstalled right away. :roll:
Post Reply