MFC-AfxGetApp获取当前进程的指针

 

CWinApp* pwin = NULL;
void CAfxGetAppDlg::OnBnClickedButton1()
{
    // TODO: 在此添加控件通知处理程序代码

    pwin= AfxGetApp();//获取当前应用进程的指针
    //确切的说是获取由CWinApp派生出类的对象
    //通过这个指针可以访问到这个进程中的对象


    CWnd* pWnd = pwin->GetMainWnd();//获取主窗口指针
    CAfxGetAppDlg* pDlg= (CAfxGetAppDlg*)pWnd;
    //主窗口指针转化成对话框类指针

    pDlg->SetWindowText(_T("练习"));

}


void CAfxGetAppDlg::OnBnClickedButton2()
{
    HANDLE handle = NULL;
    HANDLE handle1 = NULL;
    pwin = AfxGetApp();
    handle=AfxGetApp()->m_pMainWnd;//获得主窗口的句柄

    CString str;
    str.Format(_T("主线程句柄=%d\r\n"), handle);
    ::OutputDebugString(str);

    handle1 = AfxGetMainWnd();//获得当前线程窗口句柄
    str.Format(_T("当前线程句柄=%d\r\n"), handle1);
    ::OutputDebugString(str);


}

UINT func1(LPVOID pParam)   //线程函数
{
    
    CString str;
    HANDLE handle3 = NULL;
    HANDLE handle = NULL;
    for (int i = 0; i < 10;i++) {
        str.Format(_T("n=%d\r\n"), i);
        ::OutputDebugString(str);
        handle3= AfxGetMainWnd();
        str.Format(_T("当前子线程句柄=%d\r\n"), handle3);
        ::OutputDebugString(str);

        pwin = AfxGetApp();
        handle = AfxGetApp()->m_pMainWnd;//获得主线程的句柄
        str.Format(_T("主线程句柄=%d\r\n"), handle);
        ::OutputDebugString(str);

        Sleep(1000);
    }
    

    return 0;
}
void CAfxGetAppDlg::OnBnClickedButton3()
{
    CWinThread* pp = AfxBeginThread(func1, LPVOID(456), 0, 0, 0, NULL);
    
    
    

}

 

 

 

 

 

 

 

 

posted @ 2023-04-12 07:08  天子骄龙  阅读(75)  评论(0编辑  收藏  举报