onlyou13

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1. 获取应用程序指针 与 主窗口指针

//获得应用程序指针
CDemoApp* pApp = (CDemoApp*)AfxGetApp();
//获得主窗口指针
CWnd* pMainWnd = pApp->m_pMainWnd;
CString strText = _T("");
strText.Format("pMainWnd=0x%X\nthis=0x%X\n", pMainWnd, this);
AfxMessageBox(strText);

2. 发送消息 

void CDemoDlg::OnTest() 
{
    //关闭窗口
    SendMessage(WM_CLOSE, 0, 0);
}

void CDemoDlg::OnClose() 
{
    //判断是否关闭
    if (IDYES == MessageBox(_T("是否关闭窗口?"), NULL, MB_YESNO))
    {
        CDialog::OnClose();
    }
}

3. 透明窗体

//添加WS_EX_LAYERED(0x80000)扩展风格
ModifyStyleEx(0, 0x80000);

//加载User32.DLL动态链接库
HMODULE hModule = LoadLibrary("User32.DLL"); 
if (hModule != NULL) 
{ 
    typedef BOOL (WINAPI *FUNC)(HWND, COLORREF, BYTE, DWORD); 
    //获得SetLayeredWindowAttributes函数指针 
    FUNC func = (FUNC)GetProcAddress(hModule, "SetLayeredWindowAttributes");
    if(func != NULL)
    {
        func(GetSafeHwnd(), 0, 128, 2); 
    }
    FreeLibrary(hModule); 
}

4. 加载图片背景

BOOL CDemoDlg::OnEraseBkgnd(CDC* pDC) 
{
    CRect rect;
    GetWindowRect(&rect);

    CBitmap bmp;
    bmp.LoadBitmap(IDB_BITMAP);

    CDC dc;
    dc.CreateCompatibleDC(pDC);
    dc.SelectObject(&bmp);

    pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dc, 0, 0, SRCCOPY);

    return TRUE;
//    return CDialog::OnEraseBkgnd(pDC);
}

5.定时器

void CDemoDlg::OnTest1() 
{
    //设置定时器
    SetTimer(1, 1000, NULL);
}

void CDemoDlg::OnTest2() 
{
    //关闭定时器
    KillTimer(1);

}

void CDemoDlg::OnTimer(UINT nIDEvent) 
{
    if (nIDEvent == 1)
    {
        //do something
    }

    CDialog::OnTimer(nIDEvent);
}

6. 枚举所有窗口1

void CDemoDlg::OnTest1() 
{
    CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
    pList->DeleteAllItems();
    pList->SetRedraw(FALSE);

    //获得桌面窗口
    CWnd* pDesktopWnd = CWnd::GetDesktopWindow();

    //获得第一个子窗口
    CWnd* pWnd = pDesktopWnd->GetWindow(GW_CHILD);

    while (pWnd != NULL) 
    {        
        int nItem = pList->GetItemCount();

        //获得窗口类名
        CString strClassName = _T("");
        ::GetClassName(pWnd->GetSafeHwnd(), strClassName.GetBuffer(256), 256);
        strClassName.ReleaseBuffer();
        pList->InsertItem(nItem, strClassName);

        //获得窗口标题
        CString strWindowText= _T("");
        ::GetWindowText(pWnd->GetSafeHwnd(), strWindowText.GetBuffer(256), 256);
        strWindowText.ReleaseBuffer();
        pList->SetItemText(nItem, 1, strWindowText);

        //继续下一个子窗口 
        pWnd = pWnd->GetWindow(GW_HWNDNEXT);
    }

    pList->SetRedraw(TRUE);
}

7. 枚举所有窗口2

void CDemoDlg::OnTest2() 
{
    CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
    pList->DeleteAllItems();
    pList->SetRedraw(FALSE);

    //枚举窗口
    ::EnumWindows(EnumWndProc, (LPARAM)pList);

    pList->SetRedraw(TRUE);
}

BOOL CALLBACK EnumWndProc(HWND hwnd,LPARAM lParam)
{
    if (hwnd == NULL)
    {
        return FALSE;
    }

     CListCtrl* pList = (CListCtrl*)lParam;
    int nItem = pList->GetItemCount();

    //获得窗口类名
    CString strClassName = _T("");
    ::GetClassName(hwnd, strClassName.GetBuffer(256), 256);
    strClassName.ReleaseBuffer();
    pList->InsertItem(nItem, strClassName);

    //获得窗口标题
    CString strWindowText= _T("");
    ::GetWindowText(hwnd, strWindowText.GetBuffer(256), 256);
    strWindowText.ReleaseBuffer();
    pList->SetItemText(nItem, 1, strWindowText);

    return TRUE;
}

 

posted on 2013-11-26 18:58  onlyou13  阅读(153)  评论(0编辑  收藏  举报