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. Handle to a MFC dialog box window?

Handle to a MFC dialog box window?

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
8 Posts 3 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.
  • J Offline
    J Offline
    jc0dex
    wrote on last edited by
    #1

    Hello! I'm working with an MFC dialog based app and am using: SendMessage(GetDlgItem(hDlg, IDC_EDIT_CHATLOG), EM_EXSETSEL, 0, (LPARAM)&ch); But I don't have a way to get the hDlg parameter which is the handle to the window. Anyone know how to get the handle to an mfc dialog box win? thanks in advance, jay

    M B 2 Replies Last reply
    0
    • J jc0dex

      Hello! I'm working with an MFC dialog based app and am using: SendMessage(GetDlgItem(hDlg, IDC_EDIT_CHATLOG), EM_EXSETSEL, 0, (LPARAM)&ch); But I don't have a way to get the hDlg parameter which is the handle to the window. Anyone know how to get the handle to an mfc dialog box win? thanks in advance, jay

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      That depends on where that code is. Is it in a member function of the dialog? If so, just use the CWnd wrapper:

      SendDlgItemMessage(IDC_EDIT_CHATLOG, EM_EXSETSEL, 0, (LPARAM)&ch);

      Otherwise, use AfxGetMainWnd() to get a pointer to the main window:

      AfxGetMainWnd()->SendDlgItemMessage(...);

      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

      1 Reply Last reply
      0
      • J jc0dex

        Hello! I'm working with an MFC dialog based app and am using: SendMessage(GetDlgItem(hDlg, IDC_EDIT_CHATLOG), EM_EXSETSEL, 0, (LPARAM)&ch); But I don't have a way to get the hDlg parameter which is the handle to the window. Anyone know how to get the handle to an mfc dialog box win? thanks in advance, jay

        B Offline
        B Offline
        BadKarma
        wrote on last edited by
        #3

        Hi, you can use one of the following 3 methods to retreive the handle of a control 1. insinde the dialog you can get a window object of the control:

        CWnd* pWnd = GetDlgItem(IDC_EDIT1);
        HWND hWnd = pWnd->GetSafeHwnd();
        

        2. insinde the dialog you can get the handle of the control:

        HWND hWnd2 = NULL;
        GetDlgItem(IDC_EDIT1, &hWnd2);
        

        3. from anywhere you can get a handle of a control by using:

        HWND hWnd3 = ::GetDlgItem(pDlg->GetSafeHwnd(), IDC_EDIT1);
        

        if you need to get a unknown window you'v got to find it using the following code:

        HWND hwndThis = ::FindWindow(NULL, _T("DlgTest"));
        

        where _T("DlgTest") is the caption of the window you are looking for codito ergo sum

        J 1 Reply Last reply
        0
        • B BadKarma

          Hi, you can use one of the following 3 methods to retreive the handle of a control 1. insinde the dialog you can get a window object of the control:

          CWnd* pWnd = GetDlgItem(IDC_EDIT1);
          HWND hWnd = pWnd->GetSafeHwnd();
          

          2. insinde the dialog you can get the handle of the control:

          HWND hWnd2 = NULL;
          GetDlgItem(IDC_EDIT1, &hWnd2);
          

          3. from anywhere you can get a handle of a control by using:

          HWND hWnd3 = ::GetDlgItem(pDlg->GetSafeHwnd(), IDC_EDIT1);
          

          if you need to get a unknown window you'v got to find it using the following code:

          HWND hwndThis = ::FindWindow(NULL, _T("DlgTest"));
          

          where _T("DlgTest") is the caption of the window you are looking for codito ergo sum

          J Offline
          J Offline
          jc0dex
          wrote on last edited by
          #4

          Awesome except I need the handle to the MFC dialog window! :-)

          B 1 Reply Last reply
          0
          • J jc0dex

            Awesome except I need the handle to the MFC dialog window! :-)

            B Offline
            B Offline
            BadKarma
            wrote on last edited by
            #5

            :confused: What do you mean with the MFC dialog window! codito ergo sum

            J 1 Reply Last reply
            0
            • B BadKarma

              :confused: What do you mean with the MFC dialog window! codito ergo sum

              J Offline
              J Offline
              jc0dex
              wrote on last edited by
              #6

              The HWND handle to the MFC dialog box, I think AfxGetMainWnd() will work but for some reason I've been having a bit of trouble trying to implement it in a small UDP voice chat server app.

              B 1 Reply Last reply
              0
              • J jc0dex

                The HWND handle to the MFC dialog box, I think AfxGetMainWnd() will work but for some reason I've been having a bit of trouble trying to implement it in a small UDP voice chat server app.

                B Offline
                B Offline
                BadKarma
                wrote on last edited by
                #7

                Hi, can you be a little specific of the location in code from where did you try to get the handle of the dialog? Is it from a thread handling the UDP connection and is the dialog you reffer to the main dialog of a dialog base application. In that case you should give the handle to the window/dialog via a paramter to the thread when constructing the thread. Or could you tell me a little more of the design of your application so that i can spot your problem codito ergo sum

                J 1 Reply Last reply
                0
                • B BadKarma

                  Hi, can you be a little specific of the location in code from where did you try to get the handle of the dialog? Is it from a thread handling the UDP connection and is the dialog you reffer to the main dialog of a dialog base application. In that case you should give the handle to the window/dialog via a paramter to the thread when constructing the thread. Or could you tell me a little more of the design of your application so that i can spot your problem codito ergo sum

                  J Offline
                  J Offline
                  jc0dex
                  wrote on last edited by
                  #8

                  Yea I'm sorry guys I should have been more descriptive. I've figured it out now, but basically all I was trying to do was log messages to an edit box control inside an mfc dialog based app in a CString. Now initially I was trying to do this from inside the server's functions, which I then realized all I needed was to either it global or pass it to each server's functions. Then I couldnt figure out why the strings weren't being added to the log window correctly, then I saw I needed to go inside the Dialog's Timer function where I handle my updating for my server and refresh the window every 60th of a second. All in all it was more of the fact I forgot where I needed to call what, exactly as you said. I'll remember to be more specific next time, but the help was much much appreciate and used! Jay Aigner http://wwww.jdaigner.com

                  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