1、当点击关闭按钮时确定是否退出
CLoginDlg Dlg;
int nRes=0;
do
{
nRes=Dlg.DoModal();
//回到主界面
if(nRes==IDOK)
return;
//退出系统
if(nRes==IDCANCEL)
{
if(AfxMessageBox("您确定要退出系统吗?",MB_OKCANCEL)==IDCANCEL)
{
return;
}
CFrameWnd::OnClose();
return;
}
}while(1);
if (IDYES==AfxMessageBox(L"你单击了一下\n是否退出?",MB_YESNO))
{
DestroyWindow();
}
if (IDCANCEL==AfxMessageBox(L"你单击了一下\n是否退出?",MB_OKCANCEL))
{
return;
}
else
DestroyWindow();//
2 模态非模态对话框
CTestDlg dlg;//模态对话框
dlg.DoModal();
CTestDlg *pDlg = new CTestDlg();//非模态对话框
pDlg->Create(IDD_DIALOG1,this);
pDlg->ShowWindow(SW_SHOW);
}
3 设置为全屏
放入OnInitDialog()中
int cx,cy;
cx = GetSystemMetrics(SM_CXSCREEN);
cy = GetSystemMetrics(SM_CYSCREEN);//设置为全屏并置顶
SetWindowPos(&wndTopMost,0,0,cx,cy,SWP_SHOWWINDOW);
4 设置标题动态显示
整段代码放入MainFrame::OnCreate中并在MainFrame定义HICON m_hIcons[3];
m_hIcons[0]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1));
m_hIcons[1]=LoadIcon(theApp.m_hInstance,MAKEINTRESOURCE(IDI_ICON2));
m_hIcons[2]=LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON3));//加载ICON图片
SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcons[0]);
SetTimer(1,1000,NULL);//添加定时器 并添加WM_TIMER消息响应函数
在OnTimer中加入
static int index=1;
SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcons[index]);
index=++index%3;
5 文件的读取和写入
在View视图中
void CGraphicSingleView::OnFileOpen()//重写打开和保存
{
// TODO: 在此添加命令处理程序代码
HMETAFILE hm;
hm = GetMetaFile(L"meta.wmf");
m_dcMetaFile.PlayMetaFile(hm);
DeleteMetaFile(hm);
Invalidate();
}
void CGraphicSingleView::OnFileSave()
{
// TODO: 在此添加命令处理程序代码
HMETAFILE hm;
hm = m_dcMetaFile.Close();
CopyMetaFile(hm,L"meta.wmf");
DeleteMetaFile(hm);
}
void CGraphicSingleView::OnWrite()
{
// TODO: 在此添加命令处理程序代码
CFile file(L"luo.txt",CFile::modeCreate|CFile::modeWrite);
CArchive ar(&file,CArchive::store);
int i = 5;
char ch = 'a';
float f = 1.00f;
CString str("luozhenfei");
ar<<i<<ch<<f<<str;
}
void CGraphicSingleView::OnRead()
{
// TODO: 在此添加命令处理程序代码
CFile file(L"luo.txt",CFile::modeRead);
CArchive ar(&file,CArchive::load);
CString str,strResult;
int i;
float f;
char ch;
ar>>i>>ch>>f>>str;
strResult.Format(L"%d,%c,%f,%s",i,ch,f,str);
MessageBox(strResult);
}
6 画图操作
在View中public:
定义变量
CPoint m_origin;
UINT m_nDrawType;
CPtrArray m_ptrArray;
void CGraphicSingleView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_origin = point;
CScrollView::OnLButtonDown(nFlags, point);
}
void CGraphicSingleView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CClientDC dc(this);
CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));//创建透明画刷
dc.SelectObject(pBrush);
//m_dcMetaFile.SelectObject(pBrush);
switch(m_nDrawType)
{
case 1:
dc.SetPixel(point,RGB(0,0,0));
//m_dcMetaFile.SetPixel(point,RGB(0,0,0));
break;
case 2:
dc.MoveTo(m_origin);
dc.LineTo(point);
// m_dcMetaFile.MoveTo(m_origin);
// m_dcMetaFile.LineTo(point);
break;
case 3:
dc.Rectangle(CRect(m_origin,point));
/*m_dcMetaFile.Rectangle(CRect(m_origin,point));*/
break;
case 4:
dc.Ellipse(CRect(m_origin,point));
/*m_dcMetaFile.Ellipse(CRect(m_origin,point));*/
break;
}
CGraph graph(m_nDrawType,m_origin,point);//窗口变化时图像消失
m_ptrArray.Add(&graph);
// CGraph *pGraph = new CGraph(m_nDrawType,m_origin,point);//窗口变化时图像不消失
// m_ptrArray.Add(pGraph);
CScrollView::OnLButtonUp(nFlags, point);
}
void CGraphicSingleView::OnPot()
{
// TODO: 在此添加命令处理程序代码
m_nDrawType = 1;
}
void CGraphicSingleView::OnLine()
{
// TODO: 在此添加命令处理程序代码
m_nDrawType = 2;
}
void CGraphicSingleView::OnRectangle()
{
// TODO: 在此添加命令处理程序代码
m_nDrawType = 3;
}
void CGraphicSingleView::OnEllipse()
{
// TODO: 在此添加命令处理程序代码
m_nDrawType = 4;
}
7 对话框的收缩与扩展
在对话框类中添加按钮事件处理
void CTestDlg::OnBnClickedChenge()
{
// TODO: 在此添加控件通知处理程序代码
CString str;
if (GetDlgItemText(IDC_BUTTON2,str),str=="收缩<<")
{
SetDlgItemText(IDC_BUTTON2,L"扩展>>");
}
else
{
SetDlgItemText(IDC_BUTTON2,L"收缩<<");
}
static CRect rectLarge;
static CRect rectSmall;
if (rectLarge.IsRectNull())
{
CRect rectSeparater;
GetWindowRect(&rectLarge);
GetDlgItem(IDC_STATIC_PICTURE)->GetWindowRect(&rectSeparater);
rectSmall.left=rectLarge.left;
rectSmall.bottom=rectSeparater.bottom;
rectSmall.right=rectLarge.right;
rectSmall.top=rectLarge.top;
}
if (str=="收缩<<")
{
SetWindowPos(NULL,0,0,rectSmall.Width(),rectSmall.Height(),SWP_NOMOVE|SWP_NOZORDER);//分割 窗口
}
else
{
SetWindowPos(NULL,0,0,rectLarge.Width(),rectLarge.Height(),SWP_NOMOVE|SWP_NOZORDER);//分割 窗口
}
}
8 访问控件
变量定义
CButton m_btn;
BOOL m_btnCreate;
public:
int m_num1;
public:
int m_num2;
public:
int m_num3;
public:
CEdit m_edit1;
public:
CEdit m_edit2;
public:
CEdit m_edit3;
void CTestDlg::OnBnClickedAdd()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);//访问控件4
m_num3 = m_num1+m_num2;
UpdateData(FALSE);
}
9 WM_ONCHAR消息函数
void CSingleMenuView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
/*CMenu menu;*/
CClientDC dc(this);
if(0x0d==nChar)
{
if (0==++m_nIndex)
{
m_menu.CreatePopupMenu();
GetParent()->GetMenu()->AppendMenu(MF_POPUP,(UINT)m_menu.m_hMenu,L"PhoneBook");
GetParent()->DrawMenuBar();
}
m_menu.AppendMenu(MF_STRING,ID_ABC_PHONE1+m_nIndex,m_strLine.Left(m_strLine.Find(' ')));
m_strArray.Add(m_strLine);
m_strLine.Empty();
Invalidate();
}
else
{
m_strLine+=(char)nChar;
dc.TextOut(0,0,m_strLine);
}
CView::OnChar(nChar, nRepCnt, nFlags);
}
10 在单文档的View视图中显示图片
void CSingleMenuView::OnDraw(CDC* pDC)
{
CSingleMenuDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
BITMAP bmp;
CDC dc;
dc.CreateCompatibleDC(pDC);
dc.SelectObject(&bitmap);
CRect rect;
GetClientRect(&rect);
pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,SRCCOPY);//不能缩放的显示
//pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);//能缩放 的显示
CFont *pFont = pDC->SelectObject(&m_font);//更改字体
pDC->TextOut(0,0,m_strFont);
pDC->SelectObject(pFont);
}
11 单文档中的文字编辑
void CSingleDocView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)//文本编辑
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CClientDC dc(this);
CFont ft;
ft.CreatePointFont(200,L"华文行楷",NULL);
CFont *pFont = dc.SelectObject(&ft);
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
if (0x0d == nChar)
{
m_strLine.Empty();
m_point.y+=tm.tmHeight;//按下回车键后纵坐标变化
}
else if (0x08==nChar)//退格
{
COLORREF color = dc.SetBkColor(dc.GetBkColor());
dc.TextOut(m_point.x,m_point.y,m_strLine);
m_strLine = m_strLine.Left(m_strLine.GetLength()-1);
dc.SetBkColor(color);
}
else
{
m_strLine+=(char)nChar;//接收键盘的输入
}
CSize sz = dc.GetTextExtent(m_strLine);
CPoint pt;
pt.x = m_point.x+sz.cx;
pt.y = m_point.y;
SetCaretPos(pt);
dc.TextOut(m_point.x,m_point.y,m_strLine);
dc.SelectObject(pFont);
CView::OnChar(nChar, nRepCnt, nFlags);
}
12 右键弹出菜单
void CSingleDocView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CMenu menu;//创建右键弹出菜单
menu.LoadMenu(IDR_MENU1);//加载资源
CMenu *popMenu = menu.GetSubMenu(0);
ClientToScreen(&point);
popMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
CView::OnRButtonDown(nFlags, point);
}
13 在View视图中显示网格
void CSingleDocView::OnDraw(CDC *pDC)
{
CSingleDocDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
CString str("HelloWorld!!!");
pDC->TextOut(100,50,str);//显示文字
CSize sz = pDC->GetTextExtent(str);
str.LoadString(IDS_STRING59394);//显示String Table 中的ID中的文字
pDC->TextOut(200,50,str);
pDC->BeginPath();
pDC->Rectangle(100,50,100+sz.cx,50+sz.cy);//画矩形
pDC->EndPath();
pDC->SelectClipPath(RGN_DIFF);
for (int i=0;i<1500;i+=10)
{
pDC->MoveTo(0,i);//纵坐标变化
pDC->LineTo(1500,i);//画横线
pDC->MoveTo(i,0);//横坐标变化
pDC->LineTo(i,1000);//画竖线
}
}
14 在View视图中显示变色的字
在MainFram::OnCreate中添加SetTimer(1,1000,NULL);//添加定时器
void CSingleDocView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_Width+=5;
CClientDC dc(this);
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
CRect rect;
rect.left=0;
rect.right=m_Width;
rect.top=200;
rect.bottom=rect.top+tm.tmHeight;
dc.SetTextColor(RGB(255,0,0));
CString str;
str.LoadString(IDS_STRING59394);
dc.DrawText(str,rect,DT_LEFT);
rect.left=150;
rect.bottom=rect.top+tm.tmHeight;
CSize sz = dc.GetTextExtent(str);
if (m_Width>sz.cx)
{
m_Width=0;
dc.SetTextColor(RGB(0,255,0));
dc.TextOut(0,200,str);
}
CView::OnTimer(nIDEvent);
}
15 在状态栏显示鼠标坐标
void CStyleView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CString str;
str.Format("x=%d,y=%d",point.x,point.y);
//((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(str);public:
//在MainFram中添加变量CStatusBar m_wndStatusBar;
//((CMainFrame*)GetParent())->SetMessageText(str);
//((CMainFrame*)GetParent())->GetMessageBar()->SetWindowText(str);
GetParent()->GetDescendantWindow(AFX_IDW_STATUS_BAR)->SetWindowText(str);
CView::OnMouseMove(nFlags, point);
}
16 在状态栏显示时间
void CMainFrame::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
static int index=1;
SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcons[index]);
index=++index%3;
CTime t=CTime::GetCurrentTime();
CString str=t.Format("%H:%M:%S");
CClientDC dc(this);
CSize sz=dc.GetTextExtent(str);
m_wndStatusBar.SetPaneInfo(1,IDS_TIMER,SBPS_NORMAL,sz.cx);
m_wndStatusBar.SetPaneText(1,str);
CFrameWnd::OnTimer(nIDEvent);
}
17 添加属性表单
先在Dialog中添加属性对话框资源,并为每个属性对话框添加类从CPropertyPage继承
添加OnWizardNext OnWizardFinish OnSetActive消息处理函数
18 树形控件
BOOL CTree::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
HTREEITEM InsertItemEx(HTREEITEM hParent, LPCTSTR lpszItem, UINT nIdBitmap = 0,
COLORREF crTransparent = RGB(255,255,255));HTREEITEM InsertItemEx(HTREEITEM hParent, LPCTSTR lpszItem, LPCTSTR lpszBitmap,
COLORREF crTransparent = RGB(255,255,255));
m_tree.SetItemHeight(20);//树形控件
HTREEITEM hRoot,hParent;
hRoot = m_tree.InsertItem(L"My Document");
hParent = m_tree.InsertItem(L"My eBook",hRoot);
m_tree.InsertItem(L"My BookCase",hParent);
m_tree.InsertItem(L"My Notes",hParent);
hParent = m_tree.InsertItem(L"Picture",hRoot);
m_tree.InsertItem(L"Picture Example",hParent);
m_tree.InsertItem(L"My Picture",hParent);
hRoot = m_tree.InsertItem(L"Computer");
m_tree.InsertItem(L"WIN7(C:)",hRoot);
m_tree.InsertItem(L"TOOLS(D:)",hRoot);
m_tree.InsertItem(L"FUN(E:)",hRoot);
m_tree.InsertItem(L"STUDYS(F:)",hRoot);
m_tree.InsertItem(L"回车站",0,1);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
19 列表控件
BOOL CCtrDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
m_list.InsertColumn(0,L"图标",LVCFMT_RIGHT,100,-1);
m_list.InsertColumn(1,L"意义",LVCFMT_RIGHT,100,-1);
//创建图像列表
m_smallPicture.Create(IDB_BITMAP_SMALL,20,10,RGB(0x00,0x80,0x80));
m_largePicture.Create(IDB_BITMAP_LARGE,32,10,RGB(0x00,0x80,0x80));
//设置关联图像列表
m_list.SetImageList(&m_largePicture,LVSIL_NORMAL);
m_list.SetImageList(&m_smallPicture,LVSIL_SMALL);
CString tempstr;
CString drives[] = {L"Folder",L"文件夹",L"Open Folder",L"打开文件",L"3.5-Inch Floppy",L"3.5英寸 盘",L"Hard Drive",L"硬盘",L"Network Drive",L"网络设备",L"CD-ROM",L"光驱",L"Audio CD",L"视频光盘"};
for (int i = 0;i<7;++i)
{
//插入表项
int nIndex = m_list.InsertItem(m_list.GetItemCount(),drives[2*i],i);
m_list.SetItemText(nIndex,1,drives[i*2+1]);
}
CheckRadioButton(IDC_RADIO1,IDC_RADIO4,IDC_RADIO4);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}