Results 1 to 9 of 9

Thread: screen resolution

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    screen resolution

    hello!
    i could i do this?

    when i load form1 it will check the screen resolution
    if it is not 800x600 it will convert to 800x600 pixels
    and when i close the form1 it will go back to previous resolution.

    pls. help.
    *****************
    VB6,PHP,VS 2005

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: screen resolution

    I'm fairly certain that you have to use the Windows API to set the screen resolution, although I can't recall which function. I would ask though, why exactly you want to change the resolution. This question has been asked many times and more often than not it is misguided. Changing the user's screen resolution is something that should very rarely be done.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: screen resolution

    Right, users hate it when a program changes the screen resolution.

    If you still want to do it use the ChangeDisplaySettings API.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: screen resolution

    i'm writing a POS that's why i want to display it 800x600 pixels
    it is easy for the cashier to see the value (money)

    and in 800x600 it is ease in the eyes.

    can you give example about the API how to change the resolution?

    i found this code here in forums but i don't know how to convert in C#
    if anyone can convert this, this is to solve my problem.

    Code:
    Option Explicit
    
    Const WM_DISPLAYCHANGE = &H7E
    Const HWND_BROADCAST = &HFFFF&
    Const EWX_LOGOFF = 0
    Const EWX_SHUTDOWN = 1
    Const EWX_REBOOT = 2
    Const EWX_FORCE = 4
    Const CCDEVICENAME = 32
    Const CCFORMNAME = 32
    Const DM_BITSPERPEL = &H40000
    Const DM_PELSWIDTH = &H80000
    Const DM_PELSHEIGHT = &H100000
    Const CDS_UPDATEREGISTRY = &H1
    Const CDS_TEST = &H4
    Const DISP_CHANGE_SUCCESSFUL = 0
    Const DISP_CHANGE_RESTART = 1
    Const BITSPIXEL = 12
    
    Private Type DEVMODE
        dmDeviceName As String * CCDEVICENAME
        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 * CCFORMNAME
        dmUnusedPadding As Integer
        dmBitsPerPel As Integer
        dmPelsWidth As Long
        dmPelsHeight As Long
        dmDisplayFlags As Long
        dmDisplayFrequency As Long
    End Type
    
    Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" _
    (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
    Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" _
    (lpDevMode As Any, ByVal dwFlags As Long) As Long
    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 Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, _
    ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As Any) As Long
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Public OldX As Long
    Public OldY As Long
    Public nDC As Long
    
    Private Sub ChangeResolution(x As Long, Y As Long, Bits As Long)
        Dim DevM As DEVMODE, ScInfo As Long, erg As Long, an As VbMsgBoxResult
        erg = EnumDisplaySettings(0&, 0&, DevM)
        DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
        DevM.dmPelsWidth = x
        DevM.dmPelsHeight = Y
        DevM.dmBitsPerPel = Bits
        erg = ChangeDisplaySettings(DevM, CDS_TEST)
        Select Case erg&
            Case DISP_CHANGE_RESTART
                an = MsgBox("You need to restart your computer for new settings to take effect.", vbYesNo + vbSystemModal, "Info")
                If an = vbYes Then
                    erg& = ExitWindowsEx(EWX_REBOOT, 0&)
                End If
            Case DISP_CHANGE_SUCCESSFUL
                erg = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
                ScInfo = Y * 2 ^ 16 + x
                SendMessage HWND_BROADCAST, WM_DISPLAYCHANGE, ByVal Bits, ByVal ScInfo
        End Select
    End Sub
    
    Public Sub ResizeDisplay(ByVal Enabled As Boolean, Optional ByVal x As Long, Optional ByVal Y As Long)
        If Enabled And Not IsMissing(x) And Not IsMissing(Y) Then
            If x And Y > 0 Then
                OldX = Screen.Width / Screen.TwipsPerPixelX
                OldY = Screen.Height / Screen.TwipsPerPixelY
                nDC = CreateDC("DISPLAY", vbNullString, vbNullString, ByVal 0&)
                ChangeResolution x, Y, GetDeviceCaps(nDC, BITSPIXEL)
            End If
        Else
            If OldX And OldY > 0 Then
                ChangeResolution OldX, OldY, GetDeviceCaps(nDC, BITSPIXEL)
                DeleteDC nDC
            End If
        End If
    End Sub
    *****************
    VB6,PHP,VS 2005

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: screen resolution

    Its VB 6 code so its manually convertable.

    Heres the first part.

    Code:
    using System.Runtime.InteropServices;
    
    public sealed class modMain
    {
    	private const int WM_DISPLAYCHANGE = 0X7E;
    	private const int HWND_BROADCAST = 0XFFFFL;
    	private const int EWX_LOGOFF = 0;
    	private const int EWX_SHUTDOWN = 1;
    	private const int EWX_REBOOT = 2;
    	private const int EWX_FORCE = 4;
    	private const int CCDEVICENAME = 32;
    	private const int CCFORMNAME = 32;
    	private const int DM_BITSPERPEL = 0X40000;
    	private const int DM_PELSWIDTH = 0X80000;
    	private const int DM_PELSHEIGHT = 0X100000;
    	private const int CDS_UPDATEREGISTRY = 0X1;
    	private const int CDS_TEST = 0X4;
    	private const int DISP_CHANGE_SUCCESSFUL = 0;
    	private const int DISP_CHANGE_RESTART = 1;
    	private const int BITSPIXEL = 12;
    
    	[StructLayout(LayoutKind.Sequential)]
    	private struct DEVMODE
    	{
    	[MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHDEVICENAME)]
    	public string dmDeviceName;
    		public Int16 dmSpecVersion;
    		public Int16 dmDriverVersion;
    		public Int16 dmSize;
    		public Int16 dmDriverExtra;
    		public Int32 dmFields;
    		public Int16 dmOrientation;
    		public Int16 dmPaperSize;
    		public Int16 dmPaperLength;
    		public Int16 dmPaperWidth;
    		public Int16 dmScale;
    		public Int16 dmCopies;
    		public Int16 dmDefaultSource;
    		public Int16 dmPrintQuality;
    		public Int16 dmColor;
    		public Int16 dmDuplex;
    		public Int16 dmYResolution;
    		public Int16 dmTTOption;
    		public Int16 dmCollate;
    		public string * CCHFORMNAME dmFormName;
    		public Int16 dmUnusedPadding;
    		public Int16 dmBitsPerPel;
    		public Int32 dmPelsWidth;
    		public Int32 dmPelsHeight;
    		public Int32 dmDisplayFlags;
    		public Int32 dmDisplayFrequency;
    	}
    
    }
    Last edited by RobDog888; Jul 6th, 2007 at 09:17 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: screen resolution

    Code:
    [System.Runtime.InteropServices.DllImport("user32", EntryPoint="EnumDisplaySettingsA", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
    private static extern bool EnumDisplaySettings(long lpszDeviceName, long iModeNum, Any lpDevMode);
    
    [System.Runtime.InteropServices.DllImport("user32", EntryPoint="ChangeDisplaySettingsA", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
    private static extern long ChangeDisplaySettings(Any lpDevMode, long dwFlags);
    
    [System.Runtime.InteropServices.DllImport("user32", EntryPoint="ExitWindowsEx", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
    private static extern long ExitWindowsEx(long uFlags, long dwReserved);
    
    [System.Runtime.InteropServices.DllImport("gdi32", EntryPoint="GetDeviceCaps", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
    private static extern long GetDeviceCaps(long hdc, long nIndex);
    
    [System.Runtime.InteropServices.DllImport("gdi32", EntryPoint="CreateDCA", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
    private static extern long CreateDC(string lpDriverName, string lpDeviceName, string lpOutput, Any lpInitData);
    
    [System.Runtime.InteropServices.DllImport("gdi32", EntryPoint="DeleteDC", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
    private static extern long DeleteDC(long hdc);
    
    [System.Runtime.InteropServices.DllImport("user32", EntryPoint="SendMessageA", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
    private static extern long SendMessage(long hwnd, long wMsg, long wParam, Any lParam);
    The rest should be easier to change to C# so let you give it a try.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: screen resolution

    i am not so advance in API so please guide me...

    errors:
    1. Any is not recognize so i change it ti "long"

    2. public string * CCHFORMNAME dmFormName;
    Error 2 Invalid token ';' in class, struct, or interface member declaration

    what's next?
    *****************
    VB6,PHP,VS 2005

  8. #8
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: screen resolution

    If it's a cash register, then why don't you manually set it to 800x600 and lock out users from changing it?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: screen resolution

    ok thank you for your efforts
    nowadays nothing is impossible...
    because i have mutiple forms with. the only 800x600pixels is the POS itself.
    but i have solved my problem. if anyone want to change their resolution try this.

    Code:
     [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
            struct DEVMODE
            {
                public const int DM_PELSWIDTH = 0x80000;
                public const int DM_PELSHEIGHT = 0x100000;   
                private const int CCHDEVICENAME = 32;
                private const int CCHFORMNAME = 32;
    
              [MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHDEVICENAME)]
                public string dmDeviceName;
                public short dmSpecVersion;
                public short dmDriverVersion;
                public short dmSize;
                public short dmDriverExtra;
                public int dmFields;
    
                public int dmPositionX;
                public int dmPositionY;
                public DMDO dmDisplayOrientation;
                public int dmDisplayFixedOutput;
    
                public short dmColor;
                public short dmDuplex;
                public short dmYResolution;
                public short dmTTOption;
                public short dmCollate;
              [MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHFORMNAME)]
                public string dmFormName;
                public short dmLogPixels;
                public int dmBitsPerPel;
                public int dmPelsWidth;
                public int dmPelsHeight;
                public int dmDisplayFlags;
                public int dmDisplayFrequency;
                public int dmICMMethod;
                public int dmICMIntent;
                public int dmMediaType;
                public int dmDitherType;
                public int dmReserved1;
                public int dmReserved2;
                public int dmPanningWidth;
            }
            //--------------------------------\
           
            [DllImport("user32.dll", CharSet=CharSet.Auto)]
            //static extern int ChangeDisplaySettings( DEVMODE lpDevMode,  int dwFlags);
           
            // should I use this line?
            static extern int ChangeDisplaySettings( [In] ref DEVMODE lpDevMode,  int dwFlags);
    
            void ChangeRes()
            {
                long RetVal=0;
               
                DEVMODE dm = new DEVMODE();
                dm.dmSize= (short)Marshal.SizeOf(typeof(DEVMODE));
    
                dm.dmPelsWidth = 800;
                dm.dmPelsHeight= 600;
                dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT;
               
                RetVal = ChangeDisplaySettings(ref dm, 0);
            }
    *****************
    VB6,PHP,VS 2005

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width