VC入门笔记(四)

 
 
 
1、最小化桌面所有窗口
添加按键的消息处理函数如下:
void CDemo14Dlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 //获得任务栏窗口
 CWnd* pwnd=CWnd::FindWindow(_T("Shell_TrayWnd"),NULL);
 //发送ID为0x1f5(Win+M)的wm_hotkey消息
 pwnd->SendMessage(WM_HOTKEY,0x1f5);
 
}

2、获取光标坐标。
创建一个单文档的应用程序。
在demovie类中天加变量,如下:
public
Cpoint m_point;
在类中添加消息处理函数,如下:
void CDemo17View::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 m_point=point;//保存光标坐标
 Invalidate();
 CView::OnMouseMove(nFlags, point);
}
在类中重载CView::OnDraw函数,代码如下:
void CDemo17View::OnDraw(CDC* pDC)
{
 CDemo17Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 //获得客户区坐标
 CRect rect;
 GetClientRect(rect);
 //绘制十字光标
 pDC->MoveTo(0,m_point.y);
 pDC->LineTo(rect.Width(),m_point.y);
 pDC->MoveTo(m_point.x,0);
 pDC->LineTo(m_point.x,rect.Height());
 //输出光标坐标
 CString str=_T("");
 str.Format(_T("%d,%d"),m_point.x,m_point.y);
 pDC->SetBkMode(TRANSPARENT);
 pDC->SetTextAlign(TA_LEFT|TA_BOTTOM);
 pDC->TextOut(m_point.x,m_point.y,str);
}
效果图如下:

3、限制光标移动范围。
创建一个基于单文档的应用程序。
在CDemo中重载CView::OnDraw函数,如下:
void CDemo18View::OnDraw(CDC* pDC)
{
 CDemo18Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 //获取客户区坐标
 CRect rect;
 GetClientRect(rect);
 rect.left=rect.left+rect.Width()/3;
 rect.right=rect.right-rect.Width()/3;
 rect.top=rect.top+rect.Height()/3;
 rect.bottom=rect.bottom-rect.Height()/3;
 //绘制矩形,即鼠标光标的移动范围
 pDC->Rectangle(rect);
}
在CDemoView中分别添加WM_LBUTTONDOWN和WM_LBUTTONUP的消息处理函数,如下:
void CDemo18View::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 //获得客户区坐标
 CRect rect;
 GetClientRect(rect);
 rect.left=rect.left+rect.Width()/3;
 rect.right=rect.right-rect.Width()/3;
 rect.top=rect.top+rect.Height()/3;
 rect.bottom=rect.bottom-rect.Height()/3;
 //映射屏幕坐标
 ClientToScreen(rect);
 //限制光标移动范围
 ClipCursor(&rect);
 CView::OnLButtonDown(nFlags, point);
}
void CDemo18View::OnLButtonUp(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 //光标自由移动
 ClipCursor(NULL);
 CView::OnLButtonUp(nFlags, point);
}
效果图如下:

4、选择预定义的GDI对象。
创建一个多文档的应用程序如下:
在CDemo类中重载CView::OnDraw函数,代码如下:
void CDemo19View::OnDraw(CDC* pDC)
{
 CDemo19Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 //选择画刷
 pDC->SelectStockObject(GRAY_BRUSH);
 //选择画笔
 pDC->SelectStockObject(BLACK_PEN);
 //选择字体
 pDC->SelectStockObject(ANSI_FIXED_FONT);
 CRect rect(0,0,100,100);
 pDC->Ellipse(rect);
 pDC->SetBkMode(TRANSPARENT);
 pDC->TextOut(rect.right,rect.bottom,_T("Hello!"));
}
效果图如下:

5、设置背景格式。
创建一个多文档的应用程序。
在CDemoView类中重载CView::OnDraw函数,代码如下:

void CDemo22View::OnDraw(CDC* pDC)
{
 CDemo22Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 //设置灰色背景
 pDC->SelectStockObject(GRAY_BRUSH);
 CRect rect;
 GetClientRect(rect);
 pDC->Rectangle(rect);
 //设置不透明背景
 pDC->SetBkMode(OPAQUE);
 pDC->TextOut(10,10,_T("Hello!"));
 //设置透明背景
 pDC->SetBkMode(TRANSPARENT);
 pDC->TextOut(10,50,_T("I love U!"));
}
效果图如下:

6、绘制图形。
新建一个基于单文档的应用程序。
在图形主菜单下面新建四个子菜单,名称和ID分别为:
绘制直线           ID_DRAW_LINE
绘制矩形           ID_DRAW_RECTANGLE
绘制椭圆           ID_DRAW_ELLIPSE
绘制扇形           ID_DRAW_PIE
在CDemoView中添加成员变量:
int m_ndraw;
在CDemoView中重载CView::OnDraw函数,代码如下:
void CDemo25View::OnDraw(CDC* pDC)
{
 CDemo25Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 CRect rect;
 GetClientRect(rect);
 rect.InflateRect(-50,-50,-50,-50);
 //绘制直线
 if (m_ndraw==1)
 {
  pDC->MoveTo(rect.left,rect.top);
  pDC->LineTo(rect.right,rect.bottom);
 }
 //绘制矩形
 else if (m_ndraw==2)
 {
  pDC->Rectangle(rect);
 }
 //绘制椭圆
 else if (m_ndraw==3)
 {
  pDC->Ellipse(rect);
 }
 //绘制扇形
 else if (m_ndraw==4)
 {
  pDC->Pie(rect,CPoint(rect.right,rect.CenterPoint().y),CPoint(rect.CenterPoint().x,rect.right));
 }
}
在CDemoView中为四个菜单项添加消息处理函数如下:

void CDemo25View::OnDrawLine()
{
 // TODO: Add your command handler code here
 m_ndraw=1;//绘制直线
 Invalidate();//刷新
}
void CDemo25View::OnUpdateDrawLine(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_ndraw==1)
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 } 
}
void CDemo25View::OnDrawRectangle()
{
 // TODO: Add your command handler code here
 m_ndraw=2;//绘制矩形
 Invalidate();
}
void CDemo25View::OnUpdateDrawRectangle(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_ndraw==2)
 {
  pCmdUI->SetCheck(TRUE);//打钩
 }
 else
 {
  pCmdUI->SetCheck(FALSE);//把勾去掉
 }
}
void CDemo25View::OnDrawEllipse()
{
 // TODO: Add your command handler code here
 m_ndraw=3;//绘制椭圆
 Invalidate();
}
void CDemo25View::OnUpdateDrawEllipse(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_ndraw==3)
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 }
 
}
void CDemo25View::OnDrawPie()
{
 // TODO: Add your command handler code here
 m_ndraw=4;//绘制扇形
 Invalidate();//刷新
 
}
void CDemo25View::OnUpdateDrawPie(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_ndraw==4)
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 }
}
效果图如下:

7、画笔的使用。
创建一个单文档的应用程序。
在CDemoView中添加成员变量如下:
public:
 CPoint m_endpoint;
 CPoint m_startpoint;
 bool m_bdraw;
 COLORREF m_color;
 int m_nwdith;
 int m_nstyle;
 CPen m_pen;
在CDemoView类的构造函数中初始化成员变量,如下:
CDemo26View::CDemo26View()
{
 // TODO: add construction code here
    m_nstyle=PS_SOLID;
 m_nwdith=1;
 m_color=RGB(255,0,0);
 m_pen.CreatePen(m_nstyle,m_nwdith,m_color);
 m_bdraw=FALSE;
 m_startpoint=CPoint(0,0);
 m_endpoint=CPoint(0,0);
}
为CDemoView的9个菜单项添加命令处理函数,如下:
/
// CDemo26View message handlers
void CDemo26View::OnSetdashStyle()
{
 // TODO: Add your command handler code here
 m_nstyle=PS_DASH;
 m_pen.DeleteObject();
 //创建画笔
 m_pen.CreatePen(m_nstyle,m_nwdith,m_color);
}
void CDemo26View::OnUpdateSetdashStyle(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_nstyle==PS_DASH)
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 }
 
}
void CDemo26View::OnSetSolidstyle()
{
 // TODO: Add your command handler code here
    m_nstyle=PS_SOLID;
 m_pen.DeleteObject();
 m_pen.CreatePen(m_nstyle,m_nwdith,m_color);
 
}
void CDemo26View::OnUpdateSetSolidstyle(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_nstyle==PS_SOLID)
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 }
 
}
void CDemo26View::OnSetDotstyle()
{
 // TODO: Add your command handler code here
 m_nstyle=PS_DOT;
 m_pen.DeleteObject();
 m_pen.CreatePen(m_nstyle,m_nwdith,m_color);
}
void CDemo26View::OnUpdateSetDotstyle(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_nstyle==PS_DOT)
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 }
 
}
void CDemo26View::OnSetWidth1()
{
 // TODO: Add your command handler code here
 m_nwdith=1;
 m_pen.DeleteObject();
 m_pen.CreatePen(m_nstyle,m_nwdith,m_color);
}
void CDemo26View::OnUpdateSetWidth1(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_nwdith==1)
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 }
}
void CDemo26View::OnSetWidth2()
{
 // TODO: Add your command handler code here
 m_nwdith=2;
 m_pen.DeleteObject();
 //创建画笔
 m_pen.CreatePen(m_nstyle,m_nwdith,m_color);
 
}
void CDemo26View::OnUpdateSetWidth2(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_nwdith==2)
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 }
 
}
void CDemo26View::OnSetWidth3()
{
 // TODO: Add your command handler code here
 m_nwdith=3;
 m_pen.DeleteObject();
 //创建画笔
 m_pen.CreatePen(m_nstyle,m_nwdith,m_color);
}
void CDemo26View::OnUpdateSetWidth3(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_nwdith==3)
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 }
}
void CDemo26View::OnSetRedcolor()
{
 // TODO: Add your command handler code here
 m_color=RGB(255,0,0);//红色
 m_pen.DeleteObject();
 //创建画笔
 m_pen.CreatePen(m_nstyle,m_nwdith,m_color);
}
void CDemo26View::OnUpdateSetRedcolor(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_color==RGB(255,0,0))
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 }
 
}
void CDemo26View::OnSetGreencolor()
{
 // TODO: Add your command handler code here
 m_color=RGB(0,255,0);//绿色
 m_pen.DeleteObject();
 //创建画笔
 m_pen.CreatePen(m_nstyle,m_nwdith,m_color);
 
}
void CDemo26View::OnUpdateSetGreencolor(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_color==RGB(0,255,0))
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 }
 
}
void CDemo26View::OnSetBluecolor()
{
 // TODO: Add your command handler code here
 m_color=RGB(0,0,255);//蓝色
 m_pen.DeleteObject();
 //创建画笔
 m_pen.CreatePen(m_nstyle,m_nwdith,m_color);
 
}
void CDemo26View::OnUpdateSetBluecolor(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 if (m_color==RGB(0,0,255))
 {
  pCmdUI->SetCheck(TRUE);
 }
 else
 {
  pCmdUI->SetCheck(FALSE);
 }
}
在CDemoView中为WM_LBUTTONDOWN、WM_LBUTTONUP添加消息处理函数如下:
在CDemoView中为WM_MOUSEMOVE添加消息处理函数,如下:
void CDemo26View::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 if (m_bdraw)
 {
  CDC* pdc=GetDC();
  //选择画笔
  CPen* poldpen=pdc->SelectObject(&m_pen);
  m_endpoint=point;
  //绘制直线
  pdc->MoveTo(m_startpoint);
  pdc->LineTo(m_endpoint);
  m_startpoint=m_endpoint;
  //恢复画笔
  pdc->SelectObject(poldpen);
 }
 CView::OnMouseMove(nFlags, point);
}
void CDemo26View::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 m_bdraw=TRUE;
 m_startpoint=point;
 CView::OnLButtonDown(nFlags, point);
}
void CDemo26View::OnLButtonUp(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 m_bdraw=FALSE;
 CView::OnLButtonUp(nFlags, point);
}
效果图如下:

8、多媒体播放器。
新建一个基于对话框的应用程序。
添加一个“打开”按钮。
选择:工程-添加工程-components and controls;在Registered ActiveX Controls里面选择windows media player控件,并双击。
为windows media player控件添加control型成员变量:m_ctrplay.
把windows media player拖到对话框里面,为打开按钮添加单击消息处理函数,如下:
void CMedia1Dlg::OnOpen()
{
 // TODO: Add your control notification handler code here
 //打开文件
 CFileDialog opendlg(true);
 opendlg.m_ofn.lpstrFilter="Media Files/0*.mp3;*.avi;*.rmvb;*.wma;*.mid/0";
 CString filename;
 if (opendlg.DoModal()==IDOK)
 {
  filename=opendlg.GetPathName();
 }
 //播放视频文件
 m_ctrplay.SetUrl(filename);
}
效果图如下:
 
9、创建目录。
新建一个基于对话框的应用程序。
添加一个按钮。
为按钮添加消息处理函数,代码如下:
void CFiel1Dlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 CString strdirectory=_T("C://test");
 //查找文件
 CFileFind finder;
 if (finder.FindFile(strdirectory))
 {
  finder.FindNextFile();
  if (finder.IsDirectory())
  {
   AfxMessageBox("目录已经存在");
  }
  else
  {
   AfxMessageBox(_T("目录名与现有的文件名重名"));
  }
 }
 else
 {
  //创建目录
  if (::CreateDirectory(strdirectory,NULL))
  {
   AfxMessageBox("穿件目录成功");
  }
  else
  {
   AfxMessageBox("创建目录失败");
  }
 }
}
效果图如下:
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值