Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1612983
  • 博文数量: 585
  • 博客积分: 14610
  • 博客等级: 上将
  • 技术积分: 7402
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-15 10:52
文章存档

2013年(5)

2012年(214)

2011年(56)

2010年(66)

2009年(44)

2008年(200)

分类: C/C++

2012-01-27 03:32:39

C雕虫小技集(六)
分类: 2.3 VC技巧收集 93人阅读 评论(0) 收藏 举报

VC雕虫小技集(六)
何志丹

1,,如何取得Tree Control上的CheckBox状态~ 
OnInit中加:
m_treeCtrl.InsertItem("item1");
m_treeCtrl.InsertItem("item2");

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");

阅读(366) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~