载入中。。。 'S bLog
 
载入中。。。
 
载入中。。。
载入中。。。
载入中。。。
载入中。。。
载入中。。。
 
填写您的邮件地址,订阅我们的精彩内容:


 
VC++学习笔记21对话框设计2
[ 2010/8/21 17:13:00 | By: 梦翔儿 ]
 


查找替换程序:
加入Edit m_Edit button
头部加入变量:
 CFindReplaceDialog* dlg;//声明"查找/替换"对话框指针
 int nindex; //存储查找字符串的起始位置
 int rindex; //替换字符串的大小
 BOOL degree;//判断是否为第一次替换的变量
 BOOL find;//判断是否进行查找的变量
初始化加入:
 CString str="";

 str += "君不见黄河之水天上来,奔流到海不复回。\r\n";
 str += "君不见高堂明镜悲白发,朝如青丝暮成雪。\r\n";
 str += "人生得意须尽欢,莫使金樽空对月。\r\n";
 str += "天生我材必有用,千金散尽还复来。\r\n";
 str += "烹羊宰牛且为乐,会须一饮三百杯。\r\n";
 str += "岑夫子,丹丘生,将进酒,杯莫停。\r\n";
 str += "与君歌一曲,请君为我倾耳听。\r\n";
 str += "钟鼓馔玉不足贵,但愿长醉不复醒。\r\n";
 str += "古来圣贤皆寂寞,惟有饮者留其名。\r\n";
 str += "陈王昔时宴平乐,斗酒十千恣欢谑。\r\n";
 str += "主人何为言少钱,径须沽取对君酌。\r\n";
 str += "五花马,千金裘,呼儿将出换美酒,与尔同销万古愁。";
  m_Edit.SetWindowText(str);
 nindex = 0;
 degree = FALSE;
protected:
 HICON m_hIcon;

 // Generated message map functions
 //{{AFX_MSG(CReplaceDlg)

 virtual BOOL OnInitDialog();
 afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
 afx_msg void OnPaint();
 afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnKeyDown(UINT nRepCnt, UINT nFlags);

 afx_msg void OnReplace(); //加
 //}}AFX_MSG

 DECLARE_MESSAGE_MAP()
------
//添加该消息的宏映射
BEGIN_MESSAGE_MAP(CReplaceDlg, CDialog)
 //{{AFX_MSG_MAP(CReplaceDlg)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_WM_KEYDOWN()
 ON_REGISTERED_MESSAGE(WM_FINDMESSAGE, On_FindReplace); //加

 //}}AFX_MSG_MAP
END_MESSAGE_MAP()
-----

long CReplaceDialogDlg::OnFindReplace(WPARAM wParam, LPARAM lParam)
{
 CString strText,repText;       //声明字符串变量
 strText = dlg->GetFindString();      //获得查找字符串
 CString str;          //声明字符串变量
 m_Edit.GetWindowText(str);       //获得编辑框中是文本
 if(dlg->ReplaceCurrent())       //判断是否进行替换
  find = FALSE;         //进行替换
 else
  find = TRUE;         //进行查找
 int len;           //声明整型变量
 if(dlg->ReplaceAll())       //判断是否全部替换
 {
  repText = dlg->GetReplaceString();   //获得替换字符串
  len    = repText.GetLength();    //获得替换字符串长度
  str.Replace(strText,repText);    //使用替换字符串替换查找字符串
  m_Edit.SetWindowText(str);     //将替换后的字符串显示在编辑框中
 }
 if(find)           //判断是查找还是替换
 {
  len = strText.GetLength();      //获得要查找字符串的长度
 }
 else
 {
  CString left,right;       //声明字符串变量
  int num   = strText.GetLength();   //获得查找字符串的长度
  int strnum = str.GetLength();    //获得编辑框中文本长度
  int index;         //声明整型变量
  int ret = str.Find(strText,0);
  if(ret < 0)
   return 1;
  if(!degree)         //判断是否为第一次替换
   index = str.Find(strText,nindex);  //获得查找字符串在编辑框文本中的位置
  else if(nindex-rindex >= 0)     //判断起始查找位置是否小于0
   index = str.Find(strText,nindex-rindex);//获得查找字符串在编辑框文本中的位置
  else
  {
   nindex = rindex;      //设置起始查找位置
   return 1;
  }
  degree = TRUE;
  left = str.Left(index);      //获得替换字符串左侧的字符串
  right = str.Right(strnum-index-num);  //获得替换字符串右侧的字符串
  repText = dlg->GetReplaceString();   //获得替换字符串
  len = repText.GetLength();     //获得替换字符串长度
  rindex = len;
  str = left + repText + right;    //组合成新的字符串
  m_Edit.SetWindowText(str);     //在编辑框中显示新的字符串
 }
 int index = str.Find(strText,nindex);    //获得查找字符串在编辑框文本中的位置
 m_Edit.SetSel(index,index+len);      //选中查找或替换的字符串
 nindex = index+len;         //设置起始查找位置
 m_Edit.SetFocus();         //编辑框获得焦点

 return 0;
}
------
//处理替换按钮的事件
void CReplaceDialogDlg::OnReplace()
{
 // TODO: Add your control notification handler code here
 dlg = new CFindReplaceDialog;
 dlg->Create(FALSE,NULL);
 dlg->ShowWindow(SW_SHOW);
}
=======
//打印程序
button: IDC_PRINT 打印
//变量
 CString str[6];
 CFont font;
 int screenx,screeny;
 int printx,printy;
 double ratex,ratey;
------
void CPrintDialogDlg::OnPaint()
{
 if (IsIconic())
 {
  CPaintDC dc(this); // device context for painting

  SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  // Center icon in client rectangle
  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;

  // Draw the icon
  dc.DrawIcon(x, y, m_hIcon);
 }
 else
 {
  CDialog::OnPaint();
 }
 CDC* pDC = GetDC(); //这四句是加的
 screenx = pDC->GetDeviceCaps(LOGPIXELSX);
 screeny = pDC->GetDeviceCaps(LOGPIXELSY);
 DrawText(pDC,FALSE);
}
-----
//初始化文字:
str[0] = "    弃我去者,昨日之日不可留;乱我心者,";
 str[1] = "今日之日多烦扰。长空万里送秋雁,对此可以";
 str[2] = "酣高楼。蓬莱文章建安骨,中间小谢又青发。";
 str[3] = "具怀逸兴壮思飞,欲上青天揽明月。抽刀断水";
 str[4] = "水更流,举杯消愁愁更愁。人生在世不称意,";
 str[5] = "明朝散发弄扁舟。";
------
void CPrintDialogDlg::DrawText(CDC *pDC, BOOL isprinted)
{
 CFont font;
 if(!isprinted) //预览
 {
  ratex = 1;
  ratey = 1;
 }
 else //打印
 {
  pDC->StartDoc("printinformation");
 }
 font.CreatePointFont(120,"宋体",pDC);
 
 for(int i=0;i<6;i++)
 {
  pDC->SelectObject(&font);
  pDC->TextOut(int(50*ratex),int((50+i*30)*ratey),str[i]);
 }
 if(isprinted)
 {
  pDC->EndDoc();
 }
}
----------
//填写画刷,设置背景为白色
HBRUSH CPrintDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
 
 // TODO: Change any attributes of the DC here
 CBrush m_brush (RGB(255,255,255));
 CRect m_rect;
 GetClientRect(m_rect);
 pDC->FillRect(m_rect,&m_brush);
 // TODO: Return a different brush if the default is not desired
 return m_brush;
}
===========
//这个案例虽然做出来了,但是有很多参数不太懂,也许要深入看MFC框架了
===========
//浏览并获取路径
按钮IDC_GETBROWSE
静态文本IDC_PATH m_Path
按钮动作:
void CBrowseDlg::OnGetbrowse()
{
 // TODO: Add your control notification handler code here
 CString ReturnPach;
 TCHAR szPath[_MAX_PATH];  //保存路径变量
 BROWSEINFO bi;   //Browseinfo结构变量
 bi.hwndOwner = NULL;//HWND句柄
 bi.pidlRoot = NULL;
 bi.lpszTitle = _T("文件浏览对话框"); //对话框标题
 bi.pszDisplayName = szPath; //选择文件夹路径
 bi.ulFlags = BIF_RETURNONLYFSDIRS; //标记
 bi.lpfn = NULL;
 bi.lParam = NULL; //回调消息
 LPITEMIDLIST pItemIDList= SHBrowseForFolder(&bi); //显示文件浏览对话框
 if(pItemIDList)
 {
  if(SHGetPathFromIDList(pItemIDList,szPath)) //判断是否获得路径
   ReturnPach = szPath;
 }
 else
 {
  ReturnPach = ""; //文件夹路径为空
 }
 m_Path.SetWindowText(ReturnPach);
}
=======

 
 
发表评论:
载入中。。。

 
 
 

梦翔儿网站 梦飞翔的地方 http://www.dreamflier.net
中华人民共和国信息产业部TCP/IP系统 备案序号:辽ICP备09000550号

Powered by Oblog.