2013-06-16 2 views
0

나는 MonthCalendar 컨트롤이있는 WinForm 앱이 있습니다. MaxSelectionCount 속성은 1 일로 설정됩니다..NET의 MonthCalendar - 월 확장

변경하고 싶은 특정 동작이 있습니다. 컨트롤이 12 개월 동안 표시되고 사용자가 한 달을 클릭하는 것과 같은보기 인 경우 해당 달이 확장되고 선택한 날짜는 해당 월의 마지막 일이됩니다. 그 달의 첫 번째 일로 변경하고 싶습니다. 어떻게해야합니까?

또한이 방식으로 한 달을 확장하면 이벤트이 발생합니까? 특정 이벤트가 있습니까?

감사합니다.

답변

1

Vista가 네이티브 컨트롤에서보기가 변경되면 알림 (MCM_VIEWCHANGE)을 전송하기 때문에 기술적으로 가능합니다. 이 알림을 캡처하여 이벤트로 바꿀 수 있습니다. 프로젝트에 새 클래스를 추가하고 아래 표시된 코드를 붙여 넣습니다. 나는 첫날을 선택하는 코드를 미리 요리했다. 엮다. 새 컨트롤을 도구 상자의 위쪽에서 폼으로 끌어다 놓습니다. Windows 8에서 테스트 한 결과 Vista 및 Win7에서 제대로 작동하는지 확인해야합니다.

using System; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

class MonthCalendarEx : MonthCalendar { 
    public enum View { Month, Year, Decade, Century }; 
    public class ViewEventArgs : EventArgs { 
     public ViewEventArgs(View newv, View oldv) { NewView = newv; OldView = oldv; } 
     public View NewView { get; private set; } 
     public View OldView { get; private set; } 
    } 
    public event EventHandler<ViewEventArgs> ViewChange; 

    protected virtual void OnViewChange(ViewEventArgs e) { 
     if (ViewChange == null) return; 
     // NOTE: I saw painting problems if this is done when MCM_VIEWCHANGE fires, delay it 
     this.BeginInvoke(new Action(() => { 
      // Select first day when switching to Month view: 
      if (e.NewView == View.Month) { 
       this.SetDate(this.GetDisplayRange(true).Start); 
      } 
      ViewChange(this, e); 
     })); 
    } 

    protected override void WndProc(ref Message m) { 
     if (m.Msg == 0x204e) {   // Trap WMREFLECT + WM_NOTIFY 
      var hdr = (NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NMHDR)); 
      if (hdr.code == -750) { // Trap MCM_VIEWCHANGE 
       var vc = (NMVIEWCHANGE)Marshal.PtrToStructure(m.LParam, typeof(NMVIEWCHANGE)); 
       OnViewChange(new ViewEventArgs(vc.dwNewView, vc.dwOldView)); 
      } 
     } 
     base.WndProc(ref m); 
    } 
    private struct NMHDR { 
     public IntPtr hwndFrom; 
     public IntPtr idFrom; 
     public int code; 
    } 
    private struct NMVIEWCHANGE { 
     public NMHDR hdr; 
     public View dwOldView; 
     public View dwNewView; 
    } 
} 
+0

Win7에서 잘 작동합니다. 감사. – mcu