如果确认代码无误,选择重新生成解决方案,尝试能不能解决问题
this->OnOK();
this->OnCancel();
或者修改ID为IDOK, IDCANCEL
CdialogPracticeDlg dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: 在此放置处理何时用
// “确定”来关闭对话框的代码
}
else if (nResponse == IDCANCEL)
{
// TODO: 在此放置处理何时用
// “取消”来关闭对话框的代码
}
注意:这段代码必须要会,如果记不住:在XXXX(工程名).cpp里面的InitInstance函数有类似的代码,可以仿写
#include "TipDlg.h"
void CdialogPracticeDlg::OnBnClickedButtonCalc()
{
CTipDlg dlg;
INT_PTR nResponse = dlg.DoModal();
if(nResponse == IDCANCEL)return;
UpdateData(TRUE);
m_dAns = m_dOp1 + m_dOp2;
UpdateData(FALSE);
}
int y = _ttoi(year);
void CdialogPracticeDlg::OnBnClickedButtonCalc()
{
UpdateData(TRUE);
if(m_nChoose == -1){
AfxMessageBox(_T("please choosee an operator"));
return;
}
switch(m_nChoose){
case 0:
m_dAns = m_dOp1 + m_dOp2;
break;
case 1:
m_dAns = m_dOp1 - m_dOp2;
break;
case 2:
m_dAns = m_dOp1 * m_dOp2;
break;
case 3:
if( fabs(m_dOp2) < 1e-10 ){
AfxMessageBox(_T("Error"));
return;
}
m_dAns = m_dOp1 / m_dOp2;
break;
}
UpdateData(FALSE);
}
给combo box关联 control类型变量m_comboCalc
定位到:CXXXXXXXDlg::OnInitDialog(),在TODO后面添加代码
m_comboCalc.AddString(_T(“+"));动态添加选项
动态添加:
for(int i=2000;i<=2020;i++){
CString str;
str.Format(_T("%d"),i);
this->m_combo_year.AddString(str);
}
获得当前选中的选项和文本
CString year;
int nChoose = this->m_combo_year.GetCurSel();
this->m_combo_year.GetLBText(nChoose,year);
修改选项:SetCurSel()
清空选项: ResetContent()
选项变化事件:CBN_SELCHANGE
如果是dropdown,怎么获得输入框里的文本?m_combo.GetWindowTextW()
拓展阅读: https://blog.csdn.net/qq_33583069/article/details/106167719
CButton *p8 = ((CButton*)GetDlgItem(IDC_B8));
GetCheck()
判断是否选中,0未选中1选中pButton->EnableWindow(FALSE);
pButton->EnableWindow(TRUE);
https://blog.csdn.net/qq_33583069/article/details/106183203
// CCalculatorDlg 消息处理程序
BOOL CCalculatorDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
CButton* pRadioDec = (CButton*)GetDlgItem(IDC_RADIO_DEC);
pRadioDec->SetCheck(TRUE);
CButton *pCheck = (CButton*)GetDlgItem(IDC_CHECK);
pCheck->SetCheck(TRUE);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CCalculatorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CCalculatorDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CCalculatorDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CCalculatorDlg::OnBnClickedB0(){
m_nValue = m_nValue*10;
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedB1()
{
m_nValue = m_nValue*10+1;
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedB2()
{
m_nValue = m_nValue*10+2;
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedB3()
{
m_nValue = m_nValue*10+3;
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedB4()
{
m_nValue = m_nValue*10+4;
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedB5()
{
m_nValue = m_nValue*10+5;
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedB6()
{
m_nValue = m_nValue*10+6;
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedB7()
{
m_nValue = m_nValue*10+7;
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedB8()
{
m_nValue = m_nValue*10+8;
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedB9()
{
m_nValue = m_nValue*10+9;
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedBtnAdd()
{
UpdateData(TRUE);
m_nOp1 = m_nValue;
m_nValue = 0;
op = '+';
}
void CCalculatorDlg::OnBnClickedBtnSub()
{
UpdateData(TRUE);
m_nOp1 = m_nValue;
m_nValue = 0;
op = '-';
}
void CCalculatorDlg::OnBnClickedBtnMul()
{
UpdateData(TRUE);
m_nOp1 = m_nValue;
m_nValue = 0;
op = '*';
}
void CCalculatorDlg::OnBnClickedBtnDiv()
{
UpdateData(TRUE);
m_nOp1 = m_nValue;
m_nValue = 0;
op = '/';
}
void CCalculatorDlg::OnBnClickedBtnClr()
{
m_nValue = 0;
op = 0;
m_nOp1 = 0;
m_nOp2 = 0;
UpdateData(FALSE);
}
int octToDec(int x){
int result = 0;
int w = 1;
while(x){
if(x&1)result += w*(x%8);
w *= 8;
x /= 8;
}
return result;
}
void CCalculatorDlg::OnBnClickedBtnEqu()
{
if(op == 0){
AfxMessageBox(_T("No Operand"));
return;
}
m_nOp2 = m_nValue;
CButton *pRadioOct = ((CButton*)GetDlgItem(IDC_RADIO_OCT));
if(pRadioOct->GetCheck()==1){
m_nOp1 = octToDec(m_nOp1);
m_nOp2 = octToDec(m_nOp2);
}
switch(op){
case '+':
m_nValue = m_nOp1 + m_nOp2;
break;
case '-':
m_nValue = m_nOp1 - m_nOp2;
break;
case '*':
m_nValue = m_nOp1 * m_nOp2;
break;
case '/':
if(m_nOp2 == 0){
AfxMessageBox(_T("Error"));
return;
}
m_nValue = m_nOp1 / m_nOp2;
break;
}
if(pRadioOct->GetCheck()==1){
char s8[30];
itoa(m_nValue,s8,8);
m_nValue = atoi(s8);
}
UpdateData(FALSE);
m_nValue = 0;
op = 0;
m_nOp1 = 0;
m_nOp2 = 0;
}
void CCalculatorDlg::OnBnClickedRadioDec()
{
CButton *p8 = ((CButton*)GetDlgItem(IDC_B8));
CButton *p9 = ((CButton*)GetDlgItem(IDC_B9));
p8->EnableWindow(TRUE);
p9->EnableWindow(TRUE);
transform();
}
void CCalculatorDlg::transform(){
UpdateData(TRUE);
CButton *pRadioOct = ((CButton*)GetDlgItem(IDC_RADIO_OCT));
if(pRadioOct->GetCheck() == 0) {
m_nValue = octToDec(m_nValue);
}
else {
char s8[30];
itoa(m_nValue,s8,8);
m_nValue = atoi(s8);
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnBnClickedRadioOct()
{
CButton *p8 = ((CButton*)GetDlgItem(IDC_B8));
CButton *p9 = ((CButton*)GetDlgItem(IDC_B9));
p8->EnableWindow(FALSE);
p9->EnableWindow(FALSE);
transform();
}
void CCalculatorDlg::OnBnClickedCheck()
{
CButton *pRadioDec = (CButton*)GetDlgItem(IDC_RADIO_DEC);
CButton *pRadioOct = (CButton*)GetDlgItem(IDC_RADIO_OCT);
CButton *pCheck = (CButton*)GetDlgItem(IDC_CHECK);
if(pCheck->GetCheck()==0){
pRadioDec->EnableWindow(FALSE);
pRadioOct->EnableWindow(FALSE);
} else {
pRadioDec->EnableWindow(TRUE);
pRadioOct->EnableWindow(TRUE);
}
}
考察点:重写CEdit
https://blog.csdn.net/qq_33583069/article/details/106194453
如果只是限定Edit输入数字的话
改成True就好
如果是限定用字母的话,需要自己重写,这里才是本节重点(考点)
HBITMAP hBmp=::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1));
m_Btn.SetBitmap(hBmp);
void CSDIView::OnDraw(CDC* pDC)
{
CSDIDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
pDC->TextOut(50,50,_T("hello,单文档"));
}
pDC->TextOut(50,50,_T("hello,单文档"));
CreatePointFont()
void CApp15View::OnDraw(CDC* pDC)
{
CApp15Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc) return;
CFont newFont; // 新字体
CFont *pOldFont; // 选择新字体之前的字体
newFont.CreatePointFont(180, _T("隶书")); // 创建一种新的字体
pOldFont=(CFont*)pDC->SelectObject(&newFont);
// 在指定位置输出文本
pDC->TextOut(40,10,_T("你好!"));
// 恢复以前的字体
pDC->SelectObject(pOldFont);
pDC->TextOut(50,50, _T("这是我的第一个单文档应用程序!"));
}
pDC->SetTextColor(RGB(255,0,0));
、SetBkColor()
修改菜单,并添加处理事件
void CSDIDoc::OnFontMenu()
{
CFontDialog dlg;
if(dlg.DoModal() == IDOK){
LOGFONT lf;
COLORREF color;
dlg.GetCurrentFont(&lf);
m_font.DeleteObject();
m_font.CreateFontIndirect(&lf);
m_color = dlg.GetColor();
UpdateAllViews(NULL);
}
}
在view内里刷新的话是RedrawWindow()
void CSDIDoc::OnAddText()
{
CTextDlg dlg;
if(dlg.DoModal() == IDOK){
m_nX = dlg.m_nX;
m_nY = dlg.m_nY;
m_strText = dlg.m_strText;
}
}
void CSDIView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_posText = point;
RedrawWindow();
CView::OnLButtonDown(nFlags, point);
}
修改光标
BOOL CSDIView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CSDIDoc* pDoc = GetDocument();
if(pDoc->m_nCursorFlag == 1){
::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR));
return TRUE;
} else {
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return TRUE;
}
}
对象链表
前置条件: 自定义数据类(例如文本信息类,继承自CObject)
https://blog.csdn.net/qq_33583069/article/details/106239080
注意这里记录的是LOGFONT
void CSDIDoc::OnAppendText()
{
CFontDialog dlg;
LOGFONT lf;
COLORREF color;
if(dlg.DoModal() == IDOK){
dlg.GetCurrentFont(&lf);
color = dlg.GetColor();
CTextDlg tdlg;
if(tdlg.DoModal() == IDOK){
m_nX = tdlg.m_nX;
m_nY = tdlg.m_nY;
m_strText = tdlg.m_strText;
CMyText* pText = new CMyText(m_strText,CPoint(m_nX,m_nY),lf,color);
m_textList.AddTail(pText)
}
}
UpdateAllViews(NULL);
}
把view里ondraw的后面的代码删完,重写。
void CSDIView::OnDraw(CDC* pDC)
{
CSDIDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
POSITION pos = pDoc->m_textList.GetHeadPosition();
while(pos != NULL){
CMyText* pText = (CMyText*)pDoc->m_textList.GetNext(pos);
CFont font;
font.CreateFontIndirect(&pText->m_textFont);
CFont* pOld = pDC->SelectObject(&font);
pDC->SetTextColor(pText->m_textColor);
pDC->TextOut(pText->m_textPos.x,pText->m_textPos.y,pText->m_strText);
pDC->SelectObject(pOld);
}
}
删除:
void CSDIDoc::RemoveTextList()
{
POSITION pos = m_textList.GetHeadPosition();
while(pos != NULL)
{
CMyText* pText = (CMyText*)m_textList.GetNext(pos);
delete pText;
}
m_textList.RemoveAll();
}
为CMainFrame类添加窗口关闭响应函数OnClose()
void CMainFrame::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CSDIDoc* pDoc = (CSDIDoc*)this->GetActiveDocument();
pDoc->RemoveTextList();
CFrameWndEx::OnClose();
}
void CSDIDoc::DeleteContents()
{
RemoveTextList();
CDocument::DeleteContents();
}
class CMyText:public CObject
{
DECLARE_SERIAL(CMyText)
public:
……
virtual void Serialize(CArchive &ar);
……
}
CArchive& operator<<(CArchive& ar, LOGFONT& logfont)
{
ar.Write(&logfont, sizeof(logfont));
return ar;
}
CArchive& operator>>(CArchive& ar, LOGFONT& logfont)
{
ar.Read(&logfont, sizeof(logfont));
return ar;
}
void CMyText::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << m_strText << m_textPos << m_textFont << m_textColor;
}
else
{
ar >> m_strText >> m_textPos >> m_textFont >> m_textColor;
}
}
void CSDIDoc::Serialize(CArchive& ar)
{
m_textList.Serialize(ar);
if (ar.IsStoring())
{
// TODO: 在此添加存储代码
}
else
{
// TODO: 在此添加加载代码
}
}
https://blog.csdn.net/qq_33583069/article/details/106248164