- 1 year ago
Windows 10 VirtualDesktop/Task View in VB/C#
Comment
1 / 1
To interact with the Virtual Desktops or Task View feature in Windows 10 using VB.NET or C#, you can utilize the Windows API functions available in the User32 DLL. Here's an example of how you can switch virtual desktops programmatically:
C#:
csharpusing System;
using System.Runtime.InteropServices;
public class VirtualDesktop
{
private const int SPI_SETDESKVIEWPORT = 0x101D;
private const int SPIF_SENDCHANGE = 0x2;
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SystemParametersInfo(int uiAction, int uiParam, IntPtr pvParam, int fWinIni);
public static void SwitchToDesktop(int desktopIndex)
{
IntPtr desktopIndexPtr = new IntPtr(desktopIndex);
SystemParametersInfo(SPI_SETDESKVIEWPORT, 0, desktopIndexPtr, SPIF_SENDCHANGE);
}
}
VB.NET:
vb.netImports System
Imports System.Runtime.InteropServices
Public Class VirtualDesktop
Private Const SPI_SETDESKVIEWPORT As Integer = &H101D
Private Const SPIF_SENDCHANGE As Integer = &H2
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SystemParametersInfo(ByVal uiAction As Integer, ByVal uiParam As Integer, ByVal pvParam As IntPtr, ByVal fWinIni As Integer) As Boolean
End Function
Public Shared Sub SwitchToDesktop(ByVal desktopIndex As Integer)
Dim desktopIndexPtr As New IntPtr(desktopIndex)
SystemParametersInfo(SPI_SETDESKVIEWPORT, 0, desktopIndexPtr, SPIF_SENDCHANGE)
End Sub
End Class
You can then use the SwitchToDesktop
method to switch between virtual desktops by passing the index of the desired desktop. The index starts from 0.
Here's an example usage in C#:
csharpVirtualDesktop.SwitchToDesktop(1); // Switches to the second virtual desktop (index 1)
And in VB.NET:
vb.netVirtualDesktop.SwitchToDesktop(1) ' Switches to the second virtual desktop (index 1)
Please note that the code above only demonstrates how to switch between virtual desktops programmatically. Interacting with other aspects of Task View, such as creating, closing, or moving windows between virtual desktops, requires more advanced functionality and API usage.