快捷键为‘a’
// 消息map处
ON_COMMAND(ID_MODELDIALOG, &Csecond2View::OnModeldialog)
ON_COMMAND(ID_BUTTON_MYDIALOG, &Csecond2View::OnButtonMydialog)
ON_WM_KEYDOWN()
// 弹出对话框
void Csecond2View::OnModeldialog()
{
// TODO: 在此添加命令处理程序代码
// TODO: 在此添加命令处理程序代码
CMyDialog dlg;
// 在模态对话框点击“确定”按钮后
if (dlg.DoModal() == IDOK) {
text_str = dlg.text_str;
text_x = dlg.text_x;
text_y = dlg.text_y;
text_color = dlg.color;
// 绘制字体
CDC *pDC;
pDC = GetDC();
pDC->SetTextColor(text_color);
pDC->TextOutW(text_x, text_y, text_str);
}
}
void Csecond2View::OnButtonMydialog()
{
// TODO: 在此添加命令处理程序代码
CMyDialog dlg;
// 在模态对话框点击“确定”按钮后
if (dlg.DoModal() == IDOK) {
text_str = dlg.text_str;
text_x = dlg.text_x;
text_y = dlg.text_y;
text_color = dlg.color;
// 绘制字体
CDC *pDC;
pDC = GetDC();
pDC->SetTextColor(text_color);
pDC->TextOutW(text_x, text_y, text_str);
}
}
void Csecond2View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (nChar == 65) {
CMyDialog dlg;
// 在模态对话框点击“确定”按钮后
if (dlg.DoModal() == IDOK) {
text_str = dlg.text_str;
text_x = dlg.text_x;
text_y = dlg.text_y;
text_color = dlg.color;
// 绘制字体
CDC *pDC;
pDC = GetDC();
pDC->SetTextColor(text_color);
pDC->TextOutW(text_x, text_y, text_str);
}
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
可以设置文字的内容,显示的位置以及颜色
也可以使用默认的设置
// 使用该设置的按钮
void CMyDialog::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
// 将控件的值赋值给成员变量
UpdateData(1);
// 将成员变量的值赋值给控件
UpdateData(0);
}
// 使用默认设置的按钮
void CMyDialog::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
text_str = "默认输出";
text_x = 100;
text_y = 100;
color = RGB(255,0 , 0);
// 设置控件上显示的颜色
text_color.SetColor(color);
// 将成员变量的值赋值给控件
UpdateData(0);
}
void CMyDialog::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
CDialogEx::OnOK();
}
void CMyDialog::OnBnClickedMymfccolorbutton()
{
// TODO: 在此添加控件通知处理程序代码
//获取控件上选择的RGB
color = text_color.GetColor();
}
seconde2View.cpp
// message map 处
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_POPUP_LINE, &Csecond2View::OnPopupLine)
ON_COMMAND(ID_POPUP_ELLIPSE, &Csecond2View::OnPopupEllipse)
ON_COMMAND(ID_POPUP_RECTANGLE, &Csecond2View::OnPopupRectangle)
void Csecond2View::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: 在此处添加消息处理程序代码
CMenu menu;
menu.LoadMenu(IDR_POPUPMENU);
CMenu *pMenu = menu.GetSubMenu(0);
pMenu->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);
}
void Csecond2View::OnPopupLine()
{
// TODO: 在此添加命令处理程序代码
Csecond2Doc *pDoc = GetDocument();
int start_x, start_y, end_x, end_y;
start_x = 100;
start_y = 100;
end_x = 200;
end_y = 200;
CString str = CString("line");
// int转str
CString str_start_x, str_start_y, str_end_x, str_end_y;
str_start_x.Format(_T("%d"), start_x);
str_start_y.Format(_T("%d"), start_y);
str_end_x.Format(_T("%d"), end_x);
str_end_y.Format(_T("%d"), end_y);
CDC *pDC = GetDC();
pDoc->Set(str,str_start_x, str_start_y, str_end_x, str_end_y);
pDC->MoveTo(start_x,start_y);
pDC->LineTo(end_x,end_y);
ReleaseDC(pDC);
}
void Csecond2View::OnPopupEllipse()
{
// TODO: 在此添加命令处理程序代码
Csecond2Doc *pDoc = GetDocument();
int start_x, start_y, end_x, end_y;
start_x = 100;
start_y = 200;
end_x = 400;
end_y = 500;
CString str = CString("Ellipse");
CString str_start_x, str_start_y, str_end_x, str_end_y;
str_start_x.Format(_T("%d"), start_x);
str_start_y.Format(_T("%d"), start_y);
str_end_x.Format(_T("%d"), end_x);
str_end_y.Format(_T("%d"), end_y);
CDC *pDC = GetDC();
pDoc->Set(str, str_start_x, str_start_y, str_end_x, str_end_y);
pDC->Ellipse(start_x, start_y, end_x, end_y);
ReleaseDC(pDC);
}
void Csecond2View::OnPopupRectangle()
{
// TODO: 在此添加命令处理程序代码
Csecond2Doc *pDoc = GetDocument();
int start_x, start_y, end_x, end_y;
start_x = 300;
start_y = 400;
end_x = 450;
end_y = 550;
CString str = CString("Rectangle");
CString str_start_x, str_start_y, str_end_x, str_end_y;
str_start_x.Format(_T("%d"), start_x);
str_start_y.Format(_T("%d"), start_y);
str_end_x.Format(_T("%d"), end_x);
str_end_y.Format(_T("%d"), end_y);
CDC *pDC = GetDC();
pDoc->Set(str, str_start_x, str_start_y, str_end_x, str_end_y);
pDC->Rectangle(start_x, start_y, end_x, end_y);
ReleaseDC(pDC);
}
second2Doc.cpp
// Csecond2Doc 序列化
void Csecond2Doc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: 在此添加存储代码
ar << str;
}
else
{
// TODO: 在此添加加载代码
ar >> str;
}
}
void Csecond2Doc::Set(CString &str_type, CString start_x, CString start_y, CString end_x, CString end_y) {
type_str = str_type;
start_point_x = start_x;
start_point_y = start_y;
end_point_x = end_x;
end_point_y = end_y;
str += type_str + start_x + start_y + end_x + end_y;
}