Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1966537
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类: C/C++

2010-12-19 13:46:56

1、显示或隐藏控件。
为button控件添加消息处理函数:
void CDemo5Dlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 //获取控件窗口指针
 CWnd *pwnd=GetDlgItem(IDC_EDIT1);
 if(pwnd->IsWindowVisible())//判断窗口是否可见
 {
  pwnd->ShowWindow(SW_HIDE);//显示控件
  SetDlgItemText(IDC_BUTTON1,_T("显示控件"));
 }
 else
 {
  pwnd->ShowWindow(SW_SHOW);//隐藏控件
  SetDlgItemText(IDC_BUTTON1,_T("隐藏控件"));
 }
}
效果如下:

2、启用或禁用控件。
void CDemo6Dlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 //获取控件窗口指针
 CWnd* pwnd=GetDlgItem(IDC_EDIT1);
 if(pwnd->IsWindowEnabled())//判断控件窗口是否可用
 {
  pwnd->EnableWindow(FALSE);
  SetDlgItemText(IDC_BUTTON1,_T("启用控件"));
 }
 else
 {
  pwnd->EnableWindow(TRUE);
  SetDlgItemText(IDC_BUTTON1,_T("禁用控件"));
 }
 
}
效果如下:

3、通过hdc指针获取cdc指针
创建一个基于对话框的应用程序。
为改对话框创建一个按钮。
为改按钮添加一个单击事件消息响应函数,如下:
void CDemo9Dlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 //获取当前dc的句柄
 HDC hdc=::GetDC(m_hWnd);
 //通过hdc指针获取cdc指针
 CDC* pdc=CDC::FromHandle(hdc);
 //输出文本
 pdc->SetBkMode(TRANSPARENT);
 pdc->SetTextAlign(TA_CENTER);
 CRect rect;
 GetClientRect(rect);
 pdc->TextOut(rect.Width()/2,rect.Height()/2,_T("Hello World!"));
 
}

4、创建屏幕dc。
创建dc可以调用函数CDC::CreateDC
首先创建一个基于对话框的应用程序。
为对话框添加button控件。
为button控件添加单击消息响应函数,如下:
void CDemo10Dlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 CDC* pdc=new CDC();
 //创建屏幕dc
 pdc->CreateDC(_T("DISPLAY"),NULL,NULL,NULL);//值为display时,表示创建屏幕dc
 //设置背景格式
 pdc->SetBkMode(TRANSPARENT);
 //设置文本对齐方式
 pdc->SetTextAlign(TA_LEFT);
 //获取屏幕长度和高度
 int cx=GetSystemMetrics(SM_CXSCREEN);
 int cy=GetSystemMetrics(SM_CYSCREEN);
 //在屏幕输出文字
 pdc->TextOut(cx/2,cy/2,_T("屏幕DC"));
 delete pdc;
 
}
效果图如下:
 

5、日期控件的使用。
首相创建一个基于对话框的应用程序。
为改对话框添加两个datetimepicker控件,并两控件添加成员变量,代码如下:
CDateTimeCtrl m_ctrldatetime1;
CDateTimeCtrl m_ctrldatetime2;
在CDemo11Dlg类中重载CDialog::OnInitDialog函数,代码如下:
BOOL CDemo11Dlg::OnInitDialog()
{
 CDialog::OnInitDialog();
 // Add "About..." menu item to system menu.
 // IDM_ABOUTBOX must be in the system command range.
 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
 ASSERT(IDM_ABOUTBOX < 0xF000);
 CMenu* pSysMenu = GetSystemMenu(FALSE);
 if (pSysMenu != NULL)
 {
  CString strAboutMenu;
  strAboutMenu.LoadString(IDS_ABOUTBOX);
  if (!strAboutMenu.IsEmpty())
  {
   pSysMenu->AppendMenu(MF_SEPARATOR);
   pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  }
 }
 // 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
 //选择短日期显示格式
 m_ctrldatetime1.ModifyStyle(0,DTS_SHORTDATEFORMAT,0);
 //选择日期显示格式
 m_ctrldatetime2.ModifyStyle(0,DTS_TIMEFORMAT,0);
 //获取当前时间
 CTime time=CTime::GetCurrentTime();
 //设置时间
 m_ctrldatetime1.SetTime(&time);
 m_ctrldatetime2.SetTime(&time);
 return TRUE;  // return TRUE  unless you set the focus to a control
}
为button按键添加消息响应函数如下:
void CDemo11Dlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 CTime time1;
 CTime time2;
 //获取日期和时间
 m_ctrldatetime1.GetTime(time1);
 m_ctrldatetime2.GetTime(time2);
 CString strtext=_T("");
 strtext.Format(_T("%04d-%02d-%02d\n%02d:%02d:%02d"),time1.GetYear(),time1.GetMonth(),\
  time1.GetDay(),time2.GetHour(),time2.GetMinute(),time2.GetSecond());
 AfxMessageBox(strtext);
}

6、获取窗口大小。
创建一个对话框类,加上一个按钮,添加按钮的消息处理函数如下:
void CDemo14Dlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 //获取桌面窗口
 CWnd* pwnd=CWnd::GetDesktopWindow();
 //获得窗口的大小
 CRect rect;
 pwnd->GetClientRect(rect);
 CString strtext=_T("");
 strtext.Format(_T("桌面窗口大小:%d*%d"),rect.Width(),rect.Height());
 AfxMessageBox(strtext);
 
}
效果图如下:
阅读(1915) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~