CDialog based window doesn't get focus.
-
I'm creating an additional window from my base CDialog application: //m_dtarray[] keeps pointers m_dtharray[i]=(CDialogThread*)AfxBeginThread(RUNTIME_CLASS(CDialogThread),THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED); m_dtharray[i]->m_pt_winpos.x=x; //window pos m_dtharray[i]->m_pt_winpos.y=y; m_dtharray[i]->ResumeThread(); ...and created window doesn't get focus. I tried to add something like this: m_dtharray[i]->m_pMainWnd->SetFocus(); or m_dtharray[i]->m_pMainWnd->SetForegroundWindow(); ...but it doesn't help, still my window doesn't get focus (new window's name is blinking on taskbar). Window is created in OnInitInstance() in this way. (error checking skipped) CTWindow *m_wnd=new CTWindow(); BOOL ret=m_wnd->Create(IDD_TWINDOW, NULL); m_wnd->SetWindowPos(&CWnd::wndTopMost,m_pt_winpos.x,m_pt_winpos.y,0,0,SWP_NOSIZE); m_wnd->ShowWindow(SW_SHOW); // SetForegroundWindow(m_wnd->m_hWnd); //THIS DOESN'T HELP TOO.... // m_wnd->SetFocus(); //NEITHER THIS.... m_pMainWnd=(CWnd *)m_wnd; m_wnd->m_ParentThread=this; How can I set window focus?? (from main application or from newly created window, it doesnt matter) Strange thing is, that when I run this code from VS2003 it's getting focus(?!?!) but when run from icon, it doesn't get... Do You have any idea why?? Thanks for help:) Pat.
-
I'm creating an additional window from my base CDialog application: //m_dtarray[] keeps pointers m_dtharray[i]=(CDialogThread*)AfxBeginThread(RUNTIME_CLASS(CDialogThread),THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED); m_dtharray[i]->m_pt_winpos.x=x; //window pos m_dtharray[i]->m_pt_winpos.y=y; m_dtharray[i]->ResumeThread(); ...and created window doesn't get focus. I tried to add something like this: m_dtharray[i]->m_pMainWnd->SetFocus(); or m_dtharray[i]->m_pMainWnd->SetForegroundWindow(); ...but it doesn't help, still my window doesn't get focus (new window's name is blinking on taskbar). Window is created in OnInitInstance() in this way. (error checking skipped) CTWindow *m_wnd=new CTWindow(); BOOL ret=m_wnd->Create(IDD_TWINDOW, NULL); m_wnd->SetWindowPos(&CWnd::wndTopMost,m_pt_winpos.x,m_pt_winpos.y,0,0,SWP_NOSIZE); m_wnd->ShowWindow(SW_SHOW); // SetForegroundWindow(m_wnd->m_hWnd); //THIS DOESN'T HELP TOO.... // m_wnd->SetFocus(); //NEITHER THIS.... m_pMainWnd=(CWnd *)m_wnd; m_wnd->m_ParentThread=this; How can I set window focus?? (from main application or from newly created window, it doesnt matter) Strange thing is, that when I run this code from VS2003 it's getting focus(?!?!) but when run from icon, it doesn't get... Do You have any idea why?? Thanks for help:) Pat.
PatrykDabrowski wrote:
Do You have any idea why??
Read this article, Using Worker Threads[^] what your trying to do is always going to be problematic.
I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:
-
PatrykDabrowski wrote:
Do You have any idea why??
Read this article, Using Worker Threads[^] what your trying to do is always going to be problematic.
I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:
Thanks! After couple of articles descibing this 'bug' I have successfuly tested this solufion: Instead of using plain SetForegroundWindow()/SetFocus() I use this: //Attach AttachThreadInput(GetWindowThreadProcessId(::GetForegroundWindow(),NULL),GetCurrentThreadId(),TRUE); //Do our stuff SetForegroundWindow(); SetFocus(); //Just playing safe //Detach the attached thread AttachThreadInput(GetWindowThreadProcessId(::GetForegroundWindow(),NULL),GetCurrentThreadId(),FALSE); ...and it's working fine! (on winXP)