如何为 VB.NET 禁用 'MonthCalendar' 和 Datetimepicker 的视觉样式?
How can I disable 'MonthCalendar' and Datetimepicker 's visual style for VB.NET?
enter image description here
我想为我的表格填写月历..
但是没有用,我发现问题是 'Visual style'。
所以,我尝试了 Application.VisualStyleState = System.Windows.Forms.VisualStyles.VisualStyleState.NoneEnabled
它有效,但它让我的整个程序看起来很奇怪..;(
如何仅禁用 'MonthCalendar' 和 Datetimepicker 的视觉样式? (对于 VB.NET)
请帮帮我。
谢谢!
您可以使用SetWindowTheme功能来禁用个别控件的主题。
首先,将以下 class 添加到您的程序中:
Imports System.Runtime.InteropServices
Public Class NativeMethods
<DllImport("uxtheme", ExactSpelling:=True, CharSet:=CharSet.Unicode)>
Public Shared Function SetWindowTheme(hWnd As IntPtr,
pszSubAppName As String,
pszSubIdList As String) As Integer
End Function
End Class
然后,你可以简单地做这样的事情:
NativeMethods.SetWindowTheme(DateTimePicker1.Handle, String.Empty, String.Empty)
NativeMethods.SetWindowTheme(MonthCalendar1.Handle, String.Empty, String.Empty)
结果:
Before
After
禁用由 DateTimePicker 创建的 MonthCalendar 的主题有点棘手。因为 DateTimePicker 在您单击下拉按钮时会动态创建它,所以您必须处理 DateTimePicker 的 DropDown
事件,获取 MonthCalender 的句柄,然后禁用其主题。有关详细信息,请参阅 。
这是一个例子:
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Public Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As Integer) As IntPtr
End Function
Private Const DTM_GETMONTHCAL As Integer = &H1008
Private Sub DateTimePicker1_DropDown(sender As Object, e As EventArgs) Handles DateTimePicker1.DropDown
Dim calendarHandle = SendMessage(DateTimePicker1.Handle, DTM_GETMONTHCAL, 0, 0)
If calendarHandle <> IntPtr.Zero Then
NativeMethods.SetWindowTheme(calendarHandle, String.Empty, String.Empty)
End If
End Sub
enter image description here
我想为我的表格填写月历..
但是没有用,我发现问题是 'Visual style'。
所以,我尝试了 Application.VisualStyleState = System.Windows.Forms.VisualStyles.VisualStyleState.NoneEnabled
它有效,但它让我的整个程序看起来很奇怪..;(
如何仅禁用 'MonthCalendar' 和 Datetimepicker 的视觉样式? (对于 VB.NET)
请帮帮我。
谢谢!
您可以使用SetWindowTheme功能来禁用个别控件的主题。
首先,将以下 class 添加到您的程序中:
Imports System.Runtime.InteropServices
Public Class NativeMethods
<DllImport("uxtheme", ExactSpelling:=True, CharSet:=CharSet.Unicode)>
Public Shared Function SetWindowTheme(hWnd As IntPtr,
pszSubAppName As String,
pszSubIdList As String) As Integer
End Function
End Class
然后,你可以简单地做这样的事情:
NativeMethods.SetWindowTheme(DateTimePicker1.Handle, String.Empty, String.Empty)
NativeMethods.SetWindowTheme(MonthCalendar1.Handle, String.Empty, String.Empty)
结果:
Before | After |
---|---|
禁用由 DateTimePicker 创建的 MonthCalendar 的主题有点棘手。因为 DateTimePicker 在您单击下拉按钮时会动态创建它,所以您必须处理 DateTimePicker 的 DropDown
事件,获取 MonthCalender 的句柄,然后禁用其主题。有关详细信息,请参阅
这是一个例子:
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Public Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As Integer) As IntPtr
End Function
Private Const DTM_GETMONTHCAL As Integer = &H1008
Private Sub DateTimePicker1_DropDown(sender As Object, e As EventArgs) Handles DateTimePicker1.DropDown
Dim calendarHandle = SendMessage(DateTimePicker1.Handle, DTM_GETMONTHCAL, 0, 0)
If calendarHandle <> IntPtr.Zero Then
NativeMethods.SetWindowTheme(calendarHandle, String.Empty, String.Empty)
End If
End Sub