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.
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.
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
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...
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...
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: