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?
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!)
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.
Re: Turn on/off second monitor
Thanks, very much appreciated! Can't believe you answered so quickly!
Re: Turn on/off second monitor
Got an e-mail when you posted. :)
Re: Turn on/off second monitor
chit-chat posts removed - please keep to the technical issues in our technical forums. ;)
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.
Re: Turn on/off second monitor
No prob matey, thanks for looking!
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.
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
Re: Turn on/off second monitor
Thanks - I'll give that a go. One question - is that VB6 or .net?
Re: Turn on/off second monitor
Quote:
Originally Posted by
coox
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
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.
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...
Re: Turn on/off second monitor
Quote:
Originally Posted by
coox
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?
Re: Turn on/off second monitor
If possible - would that be ok?
1 Attachment(s)
Re: Turn on/off second monitor
Sure heres a very simplistic version, Let me know if it works
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:
Re: Turn on/off second monitor
I tried it on a couple machines and haven't had an issue like that?
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
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!