📄 adbcalendardlg.cpp
字号:
// AdbCalendarDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AdbCalendar.h"
#include "AdbCalendarDlg.h"
#include "CalendarLog.h"
//#include "CalendarListDlg.h"
#include "Calendarinfo.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAdbCalendarDlg dialog
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
const int TableLeft=15;
const int TableTop=75;
const int TableHeight=144;
const int hcellh=24;
const int TRow=6;
const int TCol=7;
////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
CAdbCalendarDlg::CAdbCalendarDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAdbCalendarDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAdbCalendarDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
nfocusrow=-1;
nfocuscol=-1;
ncurshowyear=0;
ncurshowmonth=0;
nYMTop=-1;
nYMLeft=-1;
ncurdrow=-1;
ncurdcol=-1;
m_crdatestr=TEXT("");
}
void CAdbCalendarDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAdbCalendarDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAdbCalendarDlg, CDialog)
//{{AFX_MSG_MAP(CAdbCalendarDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDC_BUTYMLEFT, OnButymleft)
ON_BN_CLICKED(IDC_BUTYMRIGHT, OnButymright)
ON_BN_CLICKED(IDC_BUTCANCEL, OnButcancel)
ON_WM_ERASEBKGND()
ON_BN_CLICKED(IDC_BUTCAL, OnButcal)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAdbCalendarDlg message handlers
BOOL CAdbCalendarDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
this->SetWindowPos(this,0,26,240,294,SWP_SHOWWINDOW);
this->SetWindowText(TEXT("日历"));
// this->MoveWindow(0,0,240,294);
Initconctrl();
//初始化年月
// CTime tt=CTime::GetCurrentTime();
// ncurshowyear=tt.GetYear();
// ncurshowyear=tt.GetMonth();
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CAdbCalendarDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
///////////////////////////////////////
PaintTable();
PaintYAndM();
/**/
if(ncurshowyear==0 || ncurshowmonth==0)
{
CTime tt=CTime::GetCurrentTime();
PaintDate(tt.GetYear(),tt.GetMonth());
}
else
{
PaintDate(ncurshowyear,ncurshowmonth);
}
PaintWeekSign();
PaintCurDate();
// Do not call CDialog::OnPaint() for painting messages
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CAdbCalendarDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAdbCalendarDlg::PaintTable()
{
CClientDC hdc(this);
CRect rc,trc,frc;
GetClientRect(rc);
trc.left=TableLeft;
trc.top=TableTop;
trc.right=rc.Width()-TableLeft;
trc.bottom=trc.top+TableHeight;
//设置边框色和背景
CPen hpen(PS_SOLID,1,RGB(254,203,58));
CPen *pensuc=(CPen *)hdc.SelectObject(&hpen);
HBRUSH hbsuc=(HBRUSH)hdc.SelectObject(GetStockObject(NULL_BRUSH));
//画表格外框
hdc.Rectangle(trc);
//画表格线
for (int i=1;i<TRow;i++)
{
//横线
hdc.MoveTo(trc.left,i*trc.Height()/TRow+trc.top);
hdc.LineTo(trc.right-2,i*trc.Height()/TRow+trc.top);
}
for (int n=1;n<7;n++)
{
//画纵线
hdc.MoveTo(n*trc.Width()/TCol+trc.left,trc.top);
hdc.LineTo(n*trc.Width()/TCol+trc.left,trc.bottom-2);
}
//画焦点框
if (nfocusrow!=-1 || nfocuscol!=-1)
{
frc=GetCellRect(nfocusrow,nfocuscol);
hdc.DrawFocusRect(frc);
}
//******************
/* //****画当前日期的边框
if (ncurdrow!=-1 || ncurdcol!=-1)
{
CPen hcurpen(PS_SOLID,2,RGB(0,0,204));
hdc.SelectObject(&hcurpen);
frc=GetCellRect(ncurdrow,ncurdcol);
hdc.RoundRect(frc,CPoint(2,2));
}
//********************
*/
//清理HDC
hdc.SelectObject(hbsuc);
hdc.SelectObject(pensuc);
pensuc->Detach();
pensuc->DeleteObject();
}
void CAdbCalendarDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect rc;
GetClientRect(rc);
int tleft=TableLeft;
int ttop=TableTop;
int cellw=(rc.Width()-2*TableLeft)/TCol;
int cellh=hcellh;
if ((point.x>TableLeft) && (point.x<(rc.Width()-TableLeft)) && (point.y>TableTop) && (point.y<(TableTop+TableHeight)))
{
// int crow,ccol;
int nfrsuc,nfcsuc;
nfrsuc=nfocusrow;
nfcsuc=nfocuscol;
nfocuscol=(point.x-tleft)/cellw+1;
nfocusrow=(point.y-ttop)/cellh+1;
// printf("row1=%d,col1=%d,frow=%d,fcol=%d\n",nfrsuc,nfcsuc,nfocusrow,nfocuscol);
/*
CRect udrc;
udrc.left=TableLeft;
udrc.top=TableTop;
udrc.right=rc.Width()-TableLeft;
udrc.bottom=udrc.top+TableHeight;
*/
CRect fsrc,frc;
fsrc=GetCellRect(nfrsuc,nfcsuc);
frc=GetCellRect(nfocusrow,nfocuscol);
InvalidateRect(fsrc);
InvalidateRect(frc);
// this->SetFocus();
}
CDialog::OnLButtonDown(nFlags, point);
}
CRect CAdbCalendarDlg::GetCellRect(int nrow, int ncol)
{
int x1,x2,y1,y2;
int cellw,cellh;
CRect rc,temprc;
GetClientRect(rc);
// printf("nrow=%d,ncol=%d,rcleft=%d,rctop=%d,rcright=%d,rcboot=%d\n",nrow,ncol,rc.left,rc.top,rc.right,rc.bottom);
cellh=hcellh;
cellw=(rc.Width()-2*TableLeft)/TCol;
// printf("cellh=%d,cellw=%d\n",cellh,cellw);
x1=TableLeft+(ncol-1)*cellw+1;
// printf("x1=%d,tableleft=%d,ncol=%d,cellw=%d\n",x1,TableLeft,ncol,cellw);
y1=TableTop+(nrow-1)*cellh+1;
x2=TableLeft+ncol*cellw-1;
y2=TableTop+nrow*cellh-1;
temprc.left=x1;
temprc.top=y1;
temprc.right=x2;
temprc.bottom=y2;
return temprc;
}
void CAdbCalendarDlg::JDrawText(int nrow,int ncol,CString strtxt,BOOL blul)
{
CClientDC hdc(this);
CRect cellrc;
cellrc=GetCellRect(nrow,ncol);
// printf("nrow=%d,ncol=%d,rcleft=%d,rctop=%d,rcright=%d,rcboot=%d\r\n",nrow,ncol,cellrc.left,cellrc.top,cellrc.right,cellrc.bottom);
hdc.SetBkMode(TRANSPARENT);
hdc.SetTextColor(RGB(255,0,0));
//设置字体
CFont font;
LOGFONT lf;
hdc.GetCurrentFont()->GetLogFont(&lf);
if (blul)
{
//带下划线
lf.lfUnderline=TRUE;
}
font.CreateFontIndirect(&lf);
CFont *oldfont=hdc.SelectObject(&font);
////***********************
hdc.DrawText(strtxt,cellrc,DT_CENTER |DT_SINGLELINE | DT_VCENTER);
hdc.SelectObject(oldfont);
oldfont->Detach();
oldfont->DeleteObject();
}
void CAdbCalendarDlg::PaintDate(int nyear,int nmonth)
{
CTime tt(nyear,nmonth,1,1,1,1,0);
int oneweek=tt.GetDayOfWeek()-1;
int ncr=1,ncc=1;
if ((tt.GetDayOfWeek()-1)==0)
oneweek=7;
else
oneweek=tt.GetDayOfWeek()-1;
ncc=oneweek;
CString temp,dtp;
CTime ct=CTime::GetCurrentTime();
int monthmax=GetMonthMax(nyear,nmonth);
for (int u=0;u<monthmax;u++)
{
if (ncc>7)
{
ncc=1;
ncr++;
}
temp.Format(TEXT("%d"),u+1);
dtp=TEXT("");
dtp.Format(TEXT("%04d-%02d-%02d"),nyear,nmonth,u+1);
// printf("ncr=%d,ncc=%d,nval=%d\r\n",ncr,ncc,u+1);
if(m_crdatestr.Find(dtp,0)!=-1 && (!m_crdatestr.IsEmpty()))
{
JDrawText(ncr,ncc,temp,TRUE);
}
else
{
JDrawText(ncr,ncc,temp,FALSE);
}
if (ct.GetYear()==nyear && ct.GetMonth()==nmonth )
{
//不是当前年月的日历
if (ct.GetDay()==(u+1))
{
ncurdrow=ncr;
ncurdcol=ncc;
}
}
else
{
ncurdrow=-1;
ncurdcol=-1;
}
ncc++;
}
}
int CAdbCalendarDlg::GetMonthMax(int nyear, int nmonth)
{
BOOL bleap=FALSE;//是否闰年,默认不是
int mmnum=0;
if ((nyear % 4==0) && (nyear % 100==0) && (nyear % 400==0))
{
bleap=TRUE;
}
switch(nmonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
mmnum=31;
break;
case 2:
if (bleap)
{
mmnum=29;
}
else
{
mmnum=28;
}
break;
case 4:
case 6:
case 9:
case 11:
mmnum=30;
}
return mmnum;
}
void CAdbCalendarDlg::PaintWeekSign()
{
TCHAR cweeks[7][3]={TEXT("一"),TEXT("二"),TEXT("三"),TEXT("四"),TEXT("五"),TEXT("六"),TEXT("日")};
CRect wrc;
CClientDC hdc(this);
hdc.SetBkMode(TRANSPARENT);
hdc.SetTextColor(RGB(254,203,58) /*RGB(242,129,78) */);
for (int i=0;i<7;i++)
{
wrc=GetCellRect(0,i+1);
// hdc.RoundRect(wrc,CPoint(1,1));
hdc.DrawText(cweeks[i],wrc,DT_CENTER |DT_SINGLELINE | DT_VCENTER);
}
}
void CAdbCalendarDlg::PaintYAndM()
{
const int ymlen=100;
CRect ymrc,rc;
GetClientRect(rc);
CClientDC hdc(this);
hdc.SetBkMode(TRANSPARENT);
hdc.SetTextColor(RGB(0,0,0));
ymrc=GetCellRect(0,1);
ymrc.top-=18;
ymrc.bottom=ymrc.top+20;
if (ncurshowyear==0 || ncurshowmonth==0)
{
CTime tt=CTime::GetCurrentTime();
ncurshowyear=tt.GetYear();
ncurshowmonth=tt.GetMonth();
}
CString temp;
temp.Format(TEXT("%04d年%02d月"),ncurshowyear,ncurshowmonth);
CSize size=hdc.GetTextExtent(temp);
ymrc.left=(rc.Width()-size.cx)/2;
ymrc.right=ymrc.left+size.cx;
hdc.DrawText(temp,ymrc,DT_CENTER |DT_SINGLELINE | DT_VCENTER);
}
void CAdbCalendarDlg::OnButymleft()
{
// TODO: Add your control notification handler code here
ncurshowmonth-=1;
if (ncurshowmonth==0)
{
ncurshowmonth=12;
ncurshowyear-=1;
}
CRect udrc,rc;
GetClientRect(rc);
udrc.left=TableLeft;
udrc.top=TableTop-2*hcellh;
udrc.right=rc.Width()-TableLeft;
udrc.bottom=udrc.top+TableHeight+2*hcellh;
InvalidateRect(udrc);
}
void CAdbCalendarDlg::OnButymright()
{
// TODO: Add your control notification handler code here
ncurshowmonth+=1;
if (ncurshowmonth==13)
{
ncurshowmonth=1;
ncurshowyear+=1;
}
CRect udrc,rc;
GetClientRect(rc);
udrc.left=TableLeft;
udrc.top=TableTop-2*hcellh;
udrc.right=rc.Width()-TableLeft;
udrc.bottom=udrc.top+TableHeight+2*hcellh;
InvalidateRect(udrc);
}
void CAdbCalendarDlg::Initconctrl()
{
//初始化控件
CRect rc,brc;
GetClientRect(rc);
brc=GetCellRect(0,1);
brc.top-=18;
brc.bottom=brc.top+16;
CClientDC hdc(this);
if (ncurshowyear==0 || ncurshowmonth==0)
{
CTime tt=CTime::GetCurrentTime();
ncurshowyear=tt.GetYear();
ncurshowmonth=tt.GetMonth();
}
CString temp;
temp.Format(TEXT("%04d年%02d月"),ncurshowyear,ncurshowmonth);
CSize size=hdc.GetTextExtent(temp);
brc.left=(rc.Width()-size.cx)/2-25;
brc.right=brc.left+15;
// CButton *m_left=(CButton *)GetDlgItem(IDC_BUTYMLEFT);
m_butymleft.SubclassDlgItem(IDC_BUTYMLEFT,this);
m_butymleft.MoveWindow(brc);
m_butymleft.SetWindowText(TEXT("<"));
brc.left=(rc.Width()-size.cx)/2+size.cx+5;
brc.right=brc.left+15;
// CButton *m_right=(CButton *)GetDlgItem(IDC_BUTYMRIGHT);
m_butymright.SubclassDlgItem(IDC_BUTYMRIGHT,this);
m_butymright.MoveWindow(brc);
m_butymright.SetWindowText(TEXT(">"));
// CButton *m_calder=(CButton *)GetDlgItem(IDC_BUTCAL);
brc.left=5;
brc.top=rc.Height()-36;
brc.right=brc.left+50;
brc.bottom=brc.top+28;
m_butcal.SubclassDlgItem(IDC_BUTCAL,this);
m_butcal.MoveWindow(brc);
m_butcal.SetWindowText(TEXT("日程"));
// CButton *m_cancel=(CButton *)GetDlgItem(IDC_BUTCANCEL);
brc.left=rc.Width()-55;
brc.right=brc.left+50;
m_butcancel.SubclassDlgItem(IDC_BUTCANCEL,this);
m_butcancel.MoveWindow(brc);
m_butcancel.SetWindowText(TEXT("返回"));
CJCRLog lgtemp;
lgtemp.InitCalendarInof();
m_crdatestr=lgtemp.GetCalendarDateStr();
// m_crdatestr=TEXT("2007-07-05 |2007-07-18 |");
this->Invalidate();
}
void CAdbCalendarDlg::OnButcancel()
{
// TODO: Add your control notification handler code here
OnCancel();
}
BOOL CAdbCalendarDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CDC bkdc;
CBitmap temp;
temp.LoadBitmap(IDB_BKMAP1);
bkdc.CreateCompatibleDC(pDC);
CBitmap *pOldBitmap=bkdc.SelectObject(&temp);
CRect rc;
GetClientRect(rc);
pDC->BitBlt(0,0,rc.Width(),rc.Height(),&bkdc,0,0,SRCCOPY);
temp.Detach();
temp.DeleteObject();
return true;
// return CDialog::OnEraseBkgnd(pDC);
}
void CAdbCalendarDlg::PaintCurDate()
{
CClientDC hdc(this);
CRect frc;
//****画当前日期的边框
if (ncurdrow!=-1 || ncurdcol!=-1)
{
CPen hcurpen(PS_SOLID,2,RGB(0,0,204));
CPen *pensuc=(CPen *)hdc.SelectObject(&hcurpen);
HBRUSH hbsuc=(HBRUSH)hdc.SelectObject(GetStockObject(NULL_BRUSH));
frc=GetCellRect(ncurdrow,ncurdcol);
frc.left+=2;
frc.top+=2;
frc.right-=1;
frc.bottom-=1;
hdc.Rectangle(frc);
hdc.SelectObject(hbsuc);
hdc.SelectObject(pensuc);
pensuc->Detach();
pensuc->DeleteObject();
}
//********************
}
int CAdbCalendarDlg::GetCellData( int nrow, int ncol)
{
int ntemp;
ntemp=(nrow-1)*TCol+ncol;
CTime tt(ncurshowyear,ncurshowmonth,1,1,1,1,0);
int nweeknum;
if ((tt.GetDayOfWeek()-1)==0)
{
nweeknum=7;
}
else
{
int xx=tt.GetDayOfWeek();
nweeknum=tt.GetDayOfWeek()-1;
}
nweeknum-=1;
ntemp-=nweeknum;
if (ntemp<1 || ntemp>GetMonthMax(ncurshowyear,ncurshowmonth))
{
ntemp=-1;
}
return ntemp;
}
void CAdbCalendarDlg::OnButcal()
{
// TODO: Add your control notification handler code here
int ntemp=GetCellData(nfocusrow,nfocuscol);
CCalendarinfo dlg;
dlg.DoModal();
CJCRLog lgtemp;
lgtemp.InitCalendarInof();
m_crdatestr=lgtemp.GetCalendarDateStr();
this->Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -