C#MonthCalendar控件在光标下获取日期
是否有方法在MonthCalendar控件上获取光标下的日期?我想通过右键单击将其收集到一个对话框中。右键单击似乎不会触发其单击事件。我试图重载MouseDown事件,但这似乎也没有帮助 如果要捕获右键单击,请尝试此操作C#MonthCalendar控件在光标下获取日期,c#,.net,monthcalendar,C#,.net,Monthcalendar,是否有方法在MonthCalendar控件上获取光标下的日期?我想通过右键单击将其收集到一个对话框中。右键单击似乎不会触发其单击事件。我试图重载MouseDown事件,但这似乎也没有帮助 如果要捕获右键单击,请尝试此操作 private void Form1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { } } 更多信息可以在这里找到 是的,MonthCale
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
}
}
更多信息可以在这里找到
是的,MonthCalendar有自己的上下文菜单,因此右键单击事件被禁用。需要手术来重新启用它。向项目中添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖到窗体上
using System;
using System.Drawing;
using System.Windows.Forms;
class MyCalendar : MonthCalendar {
public event MouseEventHandler RightClick;
protected override void WndProc(ref Message m) {
if (m.Msg == 0x205) { // Trap WM_RBUTTONUP
var handler = RightClick;
if (handler != null) {
var pos = new Point(m.LParam.ToInt32());
var me = new MouseEventArgs((MouseButtons)m.WParam.ToInt32(), 1, pos.x, pos.y, 0);
handler(this, me);
}
this.Capture = false;
return;
}
base.WndProc(ref m);
}
}
现在您可以订阅右键单击事件。类似于此:
private void myCalendar1_RightClick(object sender, MouseEventArgs e) {
var hit = myCalendar1.HitTest(e.Location);
if (hit.HitArea == MonthCalendar.HitArea.Date) {
var dt = hit.Time;
MessageBox.Show(dt.ToString()); // Display your context menu here
}
}
这很有效。谢谢。:)我应该自己考虑的。