Results 1 to 23 of 23

Thread: Turn on/off second monitor

  1. #1

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141

    Turn on/off second monitor

    My machine has dual monitors with the desktop spanned across them.

    I've just bought Doom 3 and find that it's really jerky unless I disable the second display.

    The second display can be turned on and off using the "Extend my Windows desktop onto this monitor." checkbox in the display properties control panel.

    Being the lazy git I am, I want to have a program do this for me, launch Doom then turn the monitor back on afterwards.

    Anybody got any ideas of the API calls required (or .NET framework method) to do this?

  2. #2
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166
    Declare Function EnumDisplayMonitors Lib "user32.dll" (ByVal hdc As Long, ByRef lprcClip As Any, ByVal lpfnEnum As Long, ByVal dwData As Long) As Long

    Declare Function GetMonitorInfo Lib "user32.dll" Alias "GetMonitorInfoA" (ByVal hMonitor As Long, ByRef lpmi As MONITORINFO) As Long

    Declare Function MonitorFromPoint Lib "user32.dll" (ByVal x As Long, ByVal y As Long, ByVal dwFlags As Long) As Long

    Declare Function MonitorFromRect Lib "user32.dll" (ByRef lprc As RECT, ByVal dwFlags As Long) As Long

    Declare Function MonitorFromWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal dwFlags As Long) As Long

    David

  3. #3

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141
    Thank you.

  4. #4
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Re: Turn on/off second monitor

    This is a very old post but I was wondering if anyone could post some source code as I'd like to do exactly the same thing... (but not for Doom 3!)
    Nick Cook
    VB6 (SP6)

  5. #5

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141

    Re: Turn on/off second monitor

    I'll have a poke around and see if I can find what I (presumably) built at the time. Can't promise anything.

  6. #6
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Re: Turn on/off second monitor

    Thanks, very much appreciated! Can't believe you answered so quickly!
    Nick Cook
    VB6 (SP6)

  7. #7

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141

    Re: Turn on/off second monitor

    Got an e-mail when you posted.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Turn on/off second monitor

    chit-chat posts removed - please keep to the technical issues in our technical forums.

  9. #9

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141

    Re: Turn on/off second monitor

    I'm afraid to say that I don't think I have the code anymore. I found all my VB projects but this wasn't amongst them. I don't use VB 6 any more and haven't got a computer with dual monitors so I'd have a hard job trying to figure it out now unfortunately.

  10. #10
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Re: Turn on/off second monitor

    No prob matey, thanks for looking!
    Nick Cook
    VB6 (SP6)

  11. #11
    Lively Member
    Join Date
    Mar 2009
    Posts
    72

    Re: Turn on/off second monitor

    ultramon will do this for you...

    The only code I have made turns off the monitors not disable secondary monitor. but would not fix this issue.. if I decide to play with it some more i'll let you know how to do it. but ultramon will let you bind a key to do it already.. however its a 30$ product.

  12. #12
    Lively Member
    Join Date
    Mar 2009
    Posts
    72

    Re: Turn on/off second monitor

    Well this is what i'm using now to toggle the secondary monitor on and off
    sets the registry key then uses ChangeDisplaySettingsEx to make it refresh.
    Code:
        Const DISPLAY_DEVICE_ATTACHED_TO_DESKTOP As Integer = &H1
        Const DISPLAY_DEVICE_MULTI_DRIVER As Integer = &H2
        Const DISPLAY_DEVICE_PRIMARY_DEVICE As Integer = &H4
        Const DISPLAY_DEVICE_MIRRORING_DRIVER As Integer = &H8
        Const DISPLAY_DEVICE_VGA_COMPATIBLE As Integer = &H10
        Const DISPLAY_DEVICE_REMOVABLE As Integer = &H20
        Const DISPLAY_DEVICE_MODESPRUNED As Integer = &H8000000
        Const DISPLAY_DEVICE_REMOTE As Integer = &H4000000
        Const DISPLAY_DEVICE_DISCONNECT As Integer = &H2000000
        Const DISPLAY_DEVICE_ACTIVE As Integer = &H1
        Const DISPLAY_DEVICE_ATTACHED As Integer = &H2
        Const ENUM_CURRENT_SETTINGS As Integer = -1
    Code:
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure DisplayDevice
            Public cb As Integer
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
            Public DeviceName As String
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
            Public DeviceString As String
            Public StateFlag As Integer
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
            Public DeviceID As String
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
            Public DeviceKey As String
        End Structure
    Code:
        <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="ChangeDisplaySettingsEx")> _
            Public Shared Function ChangeDisplaySettingsEx(ByVal lpszDeviceName As String, ByVal lpDevMode As DEVMODE, ByVal hWnd As Int32, ByVal dwFlags As Int32, ByVal lParam As Int32) As Long
        End Function
        '<System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="EnumDisplayDevices")> _
        <DllImport("user32.dll", CharSet:=CharSet.Ansi)> _
            Public Shared Function EnumDisplayDevices(ByVal lpDevice As Integer, ByVal iDevNum As Short, ByRef lpDisplayDevice As DisplayDevice, ByVal dwFlags As Integer) As Integer
        End Function
        <DllImport("user32.dll", CharSet:=CharSet.Ansi)> _
             Public Shared Function EnumDisplaySettings(ByVal lpszDeviceName As String, ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Integer
        End Function
    Code:
        Public Sub displayDevices()
            Dim dd As DisplayDevice
            Dim cdevice As Boolean = True
            dd.cb = Marshal.SizeOf(dd)
            Dim mnum As Integer = 0
            Dim dev As DEVMODE
            Do While EnumDisplayDevices(Nothing, mnum, dd, Nothing)
                'Console.WriteLine(dd.StateFlag And DISPLAY_DEVICE_PRIMARY_DEVICE & " flag: "  dd.StateFlag)
                If Not CBool(dd.StateFlag And DISPLAY_DEVICE_MIRRORING_DRIVER) Then
                    If Not CBool(dd.StateFlag And DISPLAY_DEVICE_PRIMARY_DEVICE) And CBool(dd.StateFlag And DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) Then
                        Console.WriteLine(dd.DeviceKey & " " & dd.DeviceString & " is attached to the desktop and is not the primary monitor")
                        GetDisplaySetting(dev, dd.DeviceName)
                        Dim key As String = Replace(dd.DeviceKey, "\Registry\Machine", "HKEY_CURRENT_CONFIG")
                        Registry.SetValue(key, "Attach.ToDesktop", 0)
                        ChangeDisplaySettingsEx(dd.DeviceName, dev, Nothing, 0, Nothing)
                    ElseIf Not CBool(dd.StateFlag And DISPLAY_DEVICE_PRIMARY_DEVICE) And Not CBool(dd.StateFlag And DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) Then
                        Console.WriteLine(dd.DeviceKey & " " & dd.DeviceString & " is not attached to the desktop and is not the primary monitor")
                        GetDisplaySetting(dev, dd.DeviceName)
                        Dim key As String = Replace(dd.DeviceKey, "\Registry\Machine", "HKEY_CURRENT_CONFIG")
                        Registry.SetValue(key, "Attach.ToDesktop", 1)
                        ChangeDisplaySettingsEx(dd.DeviceName, dev, Nothing, 0, Nothing)
                    End If
                End If
                mnum += 1
            Loop
        End Sub
    Code:
        Public Sub GetDisplaySetting(ByRef Devmode As DEVMODE, ByVal NameDevice As String)
            EnumDisplaySettings(NameDevice, ENUM_CURRENT_SETTINGS, Devmode)
        End Sub
    I think I posted everything used to do it let me know how it goes.. sub to be called is displayDevices

  13. #13
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Re: Turn on/off second monitor

    Thanks - I'll give that a go. One question - is that VB6 or .net?
    Nick Cook
    VB6 (SP6)

  14. #14
    Lively Member
    Join Date
    Mar 2009
    Posts
    72

    Re: Turn on/off second monitor

    Quote Originally Posted by coox View Post
    Thanks - I'll give that a go. One question - is that VB6 or .net?
    Sorry I wrote it in .net and forgot I replied in vb6.... I can port it over if you can't figure it out

  15. #15
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Re: Turn on/off second monitor

    No, it's fine - I have VS2008 and it's about time I figured out what it's all about! Thanks for the offer though.
    Nick Cook
    VB6 (SP6)

  16. #16
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Re: Turn on/off second monitor

    Ok, I admit it - I'm lost. I've put your code into a module and a form but get errors both ways. Any chance you could make a mini project of it and post that? I'd like to start getting into VS2008...
    Nick Cook
    VB6 (SP6)

  17. #17
    Lively Member
    Join Date
    Mar 2009
    Posts
    72

    Re: Turn on/off second monitor

    Quote Originally Posted by coox View Post
    Ok, I admit it - I'm lost. I've put your code into a module and a form but get errors both ways. Any chance you could make a mini project of it and post that? I'd like to start getting into VS2008...
    You want the project in vb.net vs2008 project?

  18. #18
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Re: Turn on/off second monitor

    If possible - would that be ok?
    Nick Cook
    VB6 (SP6)

  19. #19
    Lively Member
    Join Date
    Mar 2009
    Posts
    72

    Re: Turn on/off second monitor

    Sure heres a very simplistic version, Let me know if it works
    Attached Files Attached Files

  20. #20
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Re: Turn on/off second monitor

    Thanks! But... got an error:

    System.AccessViolationException was unhandled
    Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
    Source="WindowsApplication2"
    StackTrace:
    at WindowsApplication2.Form1.ChangeDisplaySettingsEx(String lpszDeviceName, DEVMODE lpDevMode, Int32 hWnd, Int32 dwFlags, Int32 lParam) at WindowsApplication2.Form1.toggleDisplay() in C:\Documents and Settings\Main\Desktop\WindowsApplication2\WindowsApplication2\WindowsApplication2\Form1.vb:line 88 at WindowsApplication2.Form1.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Main\Desktop\WindowsApplication2\WindowsApplication2\WindowsApplication2\Form1.vb:line 107 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at WindowsApplication2.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
    InnerException:
    Nick Cook
    VB6 (SP6)

  21. #21
    Lively Member
    Join Date
    Mar 2009
    Posts
    72

    Re: Turn on/off second monitor

    I tried it on a couple machines and haven't had an issue like that?

  22. #22
    Lively Member
    Join Date
    Mar 2009
    Posts
    72

    Re: Turn on/off second monitor

    i'm on a 64 bit os i'm not sure if that would make any difference in the application

  23. #23
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550

    Re: Turn on/off second monitor

    Hmmm, I'm just on 32bit. I'll have more of a play tomorrow, and do some Googling on that error. Thanks for replying again though!
    Nick Cook
    VB6 (SP6)

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