MonthCalendar コントロールのセル情報を取得するには、MCM_GETCALENDARGRIDINFO メッセージを送ります。
https://learn.microsoft.com/ja-jp/windows/win32/controls/mcm-getcalendargridinfo
vb
1 Imports System . Runtime . InteropServices
2
3 Public Class Form1
4
5 Private Const MCM_FIRST As Integer = &H1000
6 Private Const MCM_GETCALENDARGRIDINFO As Integer = MCM_FIRST + 24
7
8 < StructLayout ( LayoutKind . Sequential , CharSet : = CharSet . Unicode ) >
9 Private Class MCGRIDINFO
10 Implements IDisposable
11 Public cbSize As Integer
12 Public dwPart As Integer
13 Public dwFlags As UInteger
14 Public iCalendar As Integer
15 Public iRow As Integer
16 Public iCol As Integer
17 Public bSelected As Integer
18 Public stStart As SYSTEMTIME
19 Public stEnd As SYSTEMTIME
20 Public rc As RECT
21 Private pszNamePtr As IntPtr
22 Private cchNamePtr As IntPtr
23
24 Public Sub New ( part As Integer , flags As Integer )
25 cbSize = Marshal . SizeOf ( Me )
26 dwPart = part
27 dwFlags = flags
28 cchName = 1024
29 pszNamePtr = Marshal . StringToCoTaskMemUni ( New String ( ChrW ( 0 ) , cchName * 2 ) )
30 End Sub
31
32 Public Sub Dispose ( ) Implements IDisposable . Dispose
33 Marshal . FreeCoTaskMem ( pszNamePtr )
34 End Sub
35
36 Public ReadOnly Property pszName As String
37 Get
38 Return Marshal . PtrToStringUni ( pszNamePtr )
39 End Get
40 End Property
41
42 Public Property cchName As Integer
43 Get
44 Return cchNamePtr . ToInt32 ( )
45 End Get
46 Set ( value As Integer )
47 cchNamePtr = New IntPtr ( value )
48 End Set
49 End Property
50
51 End Class
52
53 Private Const MCGIP_CALENDARCONTROL = 0
54 Private Const MCGIP_NEXT = 1
55 Private Const MCGIP_PREV = 2
56 Private Const MCGIP_FOOTER = 3
57 Private Const MCGIP_CALENDAR = 4
58 Private Const MCGIP_CALENDARHEADER = 5
59 Private Const MCGIP_CALENDARBODY = 6
60 Private Const MCGIP_CALENDARROW = 7
61 Private Const MCGIP_CALENDARCELL = 8
62
63 Private Const MCGIF_DATE = &H1
64 Private Const MCGIF_RECT = &H2
65 Private Const MCGIF_NAME = &H4
66
67 < StructLayout ( LayoutKind . Sequential ) >
68 Public Structure SYSTEMTIME
69 Public wYear As Short
70 Public wMonth As Short
71 Public wDayOfWeek As Short
72 Public wDay As Short
73 Public wHour As Short
74 Public wMinute As Short
75 Public wSecond As Short
76 Public wMilliseconds As Short
77 Public Function ToDateTime ( ) As DateTime
78 Return New DateTime ( wYear , wMonth , wDay , wHour ,
79 wMinute , wSecond , wMilliseconds )
80 End Function
81 End Structure
82
83 < StructLayout ( LayoutKind . Sequential ) >
84 Public Structure RECT
85 Public Left As Integer
86 Public Top As Integer
87 Public Right As Integer
88 Public Bottom As Integer
89 Public Function ToRectangle ( ) As Rectangle
90 Return Rectangle . FromLTRB ( Left , Top , Right , Bottom )
91 End Function
92 End Structure
93
94 < DllImport ( "user32.dll" , CharSet : = CharSet . Unicode ) >
95 Private Shared Function SendMessage ( hWnd As IntPtr , Msg As Integer ,
96 wParam As IntPtr , lParam As MCGRIDINFO ) As IntPtr
97
98 End Function
99
100 Private Sub Button1_Click ( sender As Object , e As EventArgs ) Handles Button1 . Click
101 ShowGridInfo ( 0 , 0 )
102 ShowGridInfo ( 0 , 1 )
103 ShowGridInfo ( 0 , 2 )
104 ShowGridInfo ( 1 , 0 )
105 ShowGridInfo ( 1 , 1 )
106 ShowGridInfo ( 1 , 2 )
107 End Sub
108
109 Private Sub ShowGridInfo ( Row As Integer , Column As Integer )
110 Using info As New MCGRIDINFO ( MCGIP_CALENDARCELL , MCGIF_DATE Or MCGIF_RECT Or MCGIF_NAME )
111 info . iRow = Row
112 info . iCol = Column
113 Dim ret = SendMessage ( MonthCalendar1 . Handle , MCM_GETCALENDARGRIDINFO , IntPtr . Zero , info )
114 Debug . Print ( $"({Row},{Column})は{info.rc.ToRectangle()}の位置にあります。" )
115 Debug . Print ( $"日付は{info.stStart.ToDateTime():yyyy/MM/dd}で、{info.pszName}が表示されています。" )
116 End Using
117 End Sub
118
119 End Class
カスタム描画ですが、CreateGraphics で作成した Grapchis オブジェクトに描画すると、ウインドウを最小化して戻す等、なにかの拍子に消えてしまいます。
コントロールを継承し、以下のように描画してください。
vb
1 Public Class MonthCalendarEx
2 Inherits MonthCalendar
3
4 Private Const WM_PAINT = &HF
5
6 Protected Overrides Sub OnHandleCreated ( e As EventArgs )
7 MyBase . OnHandleCreated ( e )
8 OwnerDraw = True
9 End Sub
10
11 Protected Overrides Sub OnHandleDestroyed ( e As EventArgs )
12 MyBase . OnHandleDestroyed ( e )
13 OwnerDraw = False
14 End Sub
15
16 Protected WriteOnly Property OwnerDraw As Boolean
17 Set ( value As Boolean )
18 SetStyle ( ControlStyles . UserPaint Or
19 ControlStyles . AllPaintingInWmPaint Or
20 ControlStyles . OptimizedDoubleBuffer , value )
21 End Set
22 End Property
23
24 Protected Overrides Sub OnPaint ( e As PaintEventArgs )
25 'e.Graphics にネイティブ画像を描画してもらう
26 Dim hdc = e . Graphics . GetHdc ( )
27 Dim m = Message . Create ( Handle , WM_PAINT , hdc , IntPtr . Zero )
28 DefWndProc ( m )
29 e . Graphics . ReleaseHdc ( )
30
31 ' ここで e.Graphics に対しカスタム描画を行う
32
33 MyBase . OnPaint ( e )
34 End Sub
35
36 End Class
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/08/19 10:20