VC雕虫小技集(六)

原文: VC雕虫小技集(六)
 

VC雕虫小技集()

何志丹

1,,如何取得Tree Control上的CheckBox状态~

OnInit中加:

m_treeCtrl.InsertItem("item1");

m_treeCtrl.InsertItem("item2");

 :office" />

void CCDialogDlg::OnButton1()

{

       HTREEITEM hItem = m_treeCtrl.GetRootItem();

       while(NULL != hItem)

       {

              CString str= m_treeCtrl.GetItemText(hItem);

              if("item2" == str)

              {

                     if(m_treeCtrl.GetCheck(hItem))

                            AfxMessageBox("选中");

              }

              hItem = m_treeCtrl.GetNextVisibleItem(hItem);

       }

}

 

2,怎么用一个程序向另一个程序发送字符并让其显示出来

首先通过FindWindow取得windows程序的窗口句柄,

然后通过GetDlgItem取得其中输入框的窗口句柄,

最后,向该窗口句柄发送WM_CHAR消息即可显示字符

例如,对于Notepad窗口,可以以如下的方式向其中输入一个'a':

取得记事本的窗口句柄

HWND hWnd = ::FindWindow( NULL  , "未定标题 - 记事本" );

取得其中输入框的窗口句柄

HWND hEdit = ::GetDlgItem( hWnd , 0x0F );  // 这里0x0F是编辑框的ID,可在SPY++中观察得到

向输入框中填写'a'

::SendMessage( hEdit , WM_CHAR , (WPARAM)'A' , 0x00000001 );

 

3, 当前时间:

CTime t  = CTime::GetCurrentTime();

       CString str;       str.Format("%d-%d-%d",t.GetYear(),t.GetMonth(),t.GetDay());

       str+= t.Format("--%H-%M-%S");

       AfxMessageBox(str);

一定要用CTime::GetCurrentTime();GetCurrentTime()是一个过时的函数.

 

4, //改变按钮的背景色。

HBRUSH CRectWindow2View::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

{

       HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

               switch (nCtlColor)

    {

        case CTLCOLOR_BTN:

         {

          CBrush Brush (RGB (128 , 0 , 128) );//你的颜色

 

          CBrush* pOldBrush = pDC->SelectObject(&Brush);

 

          pDC->SelectObject (pOldBrush );

           }

    }

        return CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

}

 

5, 加速键的使用.

 Dlg的头文件中加入:

       HACCEL m_hAccel;

Dlg的构造函数中加载加速键:

基于对话框的程序如何使用加速键?      

m_hAccel=::LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACCELERATOR1));//加速键ID

重载PreTranslateMessage函数:

BOOL CDlg::PreTranslateMessage(MSG* pMsg)

{

       // TODO: Add your specialized code here and/or call the base class

       if(m_hAccel!=NULL)

              if(::TranslateAccelerator(m_hWnd,m_hAccel,pMsg))

                     return TRUE;

       return CDialog::PreTranslateMessage(pMsg);

}

 

6,怎么把一个文件保存到其它地方?

if(!CopyFile("f://he.txt","d://he1.txt",true))

       {

              if(IDOK == MessageBox("有同名文件,你要覆盖吗?",NULL,MB_OKCANCEL))

              {

                     CopyFile("f://he.txt","d://he1.txt",false);     

              }

              else

                     return;

       }

       DeleteFile("f://he.txt");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值