代码改变世界

Visual c++ 2011-4-19

2011-04-20 21:18  Clingingboy  阅读(530)  评论(0编辑  收藏  举报

 

一.SEH(C++异常处理机制)

参考:
http://www.cppblog.com/fwxjj/archive/2008/01/25/41914.html
http://www.cppblog.com/fwxjj/archive/2008/01/25/41913.html

二.long long

long的两倍,用sizeof查看

三.枚举当前计算机用户

使用NetUserEnum返回相关信息,得到第一个用户信息后,然后for循环添加指针来遍历,完成之后用NetApiBufferFree函数释放资源

BOOL CLogonDlg::_initAdminList()
{
    NET_API_STATUS  status;
    USER_INFO_1 *pUserInfo = NULL;
    DWORD  dwPrefMaxLen = MAX_PREFERRED_LENGTH;
    DWORD  dwEntriesRead = 0;
    DWORD  dwTotalEntries = 0;
    DWORD  dwResumeHandle = 0;
    int    nAdminNum = 0;
    
    do
    {
        status = NetUserEnum(NULL,
                             1,
                             FILTER_NORMAL_ACCOUNT,
                             (LPBYTE*)&pUserInfo,
                             dwPrefMaxLen,
                             &dwEntriesRead,
                             &dwTotalEntries,
                             &dwResumeHandle);

        if(status == NERR_Success || status == ERROR_MORE_DATA)
        {
            USER_INFO_1 *pTempInfo;
            DWORD dwIndex;
            for(pTempInfo = pUserInfo, dwIndex = 0; dwIndex < dwEntriesRead; pTempInfo++, dwIndex++)
            {
                if(pTempInfo->usri1_priv == USER_PRIV_ADMIN)
                {
                    m_comboAdmin.AddString(pTempInfo->usri1_name);
                    nAdminNum++;
                }
            }

            if(pUserInfo)
            {
                NetApiBufferFree(pUserInfo);
                pUserInfo = NULL;
            }
        }

    }while(status == ERROR_MORE_DATA);

    return nAdminNum ? TRUE : FALSE;

}

四.DrawFocusRect

画一个虚线框矩形(焦点看起来就是灰色虚线框这样子的),可多次调用,会删除上次的焦点矩形
这种焦点矩形在Windows软件下随处可见

五.#pragma pack的使用

参考:http://baike.baidu.com/view/2317161.htm#sub2317161

六.TrackMouseEvent

窗体消息可以捕捉到MouseMove事件,MouseEnter(Hover)和MouseLeave由此事件而诞生,内置并没有这两个事件,而需要借助TrackMouseEvent函数来捕获.否则将无法捕获到MouseLeave事件.这属于很基本的事件.

// In the message map
ON_WM_MOUSEMOVE ()
ON_MESSAGE (WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE (WM_MOUSEHOVER, OnMouseHover)

  
void CMainWindow::OnMouseMove (UINT nFlags, CPoint point)
{
    if (!m_bMouseOver) {
        TRACE (_T ("Mouse enter\n"));
        m_bMouseOver = TRUE;

        TRACKMOUSEEVENT tme;
        tme.cbSize = sizeof (tme);
        tme.dwFlags = TME_HOVER | TME_LEAVE;
        tme.hwndTrack = m_hWnd;
        tme.dwHoverTime = HOVER_DEFAULT;
        ::TrackMouseEvent (&tme);
    }
}

LRESULT CMainWindow::OnMouseLeave (WPARAM wParam, LPARAM lParam)
{
    TRACE (_T ("Mouse leave\n"));
    m_bMouseOver = FALSE;
    return 0;
}

LRESULT CMainWindow::OnMouseHover (WPARAM wParam, LPARAM lParam)
{
    TRACE (_T ("Mouse hover (x=%d, y=%d)\n"),
        LOWORD (lParam), HIWORD (lParam));

    TRACKMOUSEEVENT tme;
    tme.cbSize = sizeof (tme);
    tme.dwFlags = TME_HOVER | TME_LEAVE;
    tme.hwndTrack = m_hWnd;
    tme.dwHoverTime = HOVER_DEFAULT;
    ::TrackMouseEvent (&tme);
    return 0;
}

参考:http://blog.csdn.net/wangzai117/archive/2008/09/24/2973404.aspx MFC Windows程序设计

七.重绘控件

先将样式定义成允许重绘

SetButtonStyle(GetButtonStyle() | BS_OWNERDRAW);

然后重写DrawItem方法

void CMyVBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    UINT uStyle = DFCS_BUTTONPUSH;
  
     // This code only works with buttons.
  
     ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
  
     // If drawing selected, add the pushed style to DrawFrameControl.
  
     if (lpDrawItemStruct->itemState & ODS_SELECTED)
        uStyle |= DFCS_PUSHED;
  
     // Draw the button frame.
  
     ::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, 
        DFC_BUTTON, uStyle);
  
     // Get the button's text.
  
     CString strText;
     GetWindowText(strText);
  
     // Draw the button text using the text color red.
  
     COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
     ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), 
        &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
     ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);

    
    // TODO:  Add your code to draw the specified item
}

最后记得映射控件ID

DDX_Control(pDX, IDOK, m_btnOK);

知道做法后就可以好好研究DrawItem的GDI画法了