Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CDialog based window doesn't get focus.

CDialog based window doesn't get focus.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PatrykDabrowski
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • P PatrykDabrowski

      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.

      S Offline
      S Offline
      S Douglas
      wrote on last edited by
      #2

      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:

      P 1 Reply Last reply
      0
      • S S Douglas

        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:

        P Offline
        P Offline
        PatrykDabrowski
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups