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. Is it possible to attach another apps' window DC? (About Gdi handle table and objects)

Is it possible to attach another apps' window DC? (About Gdi handle table and objects)

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsdebugginghelpquestion
9 Posts 4 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.
  • S Offline
    S Offline
    sawerr
    wrote on last edited by
    #1

    Hi I want to make a program that retrieves other windows' pixel values. First i made a default application which title name is "Untitled - NewWindow" and one more MFC project which is trying to draw to that window. The code is:

    void CDeviceContextDlg::OnBnClickedButton1()
    {
    ::EnumWindows(EnumWindowsProc, NULL);
    }

    BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
    if(hwnd == FindWindow(NULL,L"Untitled - NewWindow"))
    {
    HDC dc = GetDC(hwnd);
    CDeviceContextDlg *dlg = (CDeviceContextDlg *)AfxGetMainWnd();
    if((dlg->Clientdc.Attach(dc)))
    {
    dlg->Clientdc.SetBkColor(RGB(255,0,0));

    	};
    };
    return TRUE;
    

    }

    It gives run-time error. I debugged it and in that line:

    HDC dc = GetDC(hwnd);

    In Debugger:

    dc 0x53011041 {unused=??? } HDC__ *
    unused CXX0030: Error: expression cannot be evaluated

    Is it because process gdi handle table is process specific and it is impossible to get DC and draw another process' window? Or is there a way to draw another process' window or getpixel values? Thanks

    C M 2 Replies Last reply
    0
    • S sawerr

      Hi I want to make a program that retrieves other windows' pixel values. First i made a default application which title name is "Untitled - NewWindow" and one more MFC project which is trying to draw to that window. The code is:

      void CDeviceContextDlg::OnBnClickedButton1()
      {
      ::EnumWindows(EnumWindowsProc, NULL);
      }

      BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
      {
      if(hwnd == FindWindow(NULL,L"Untitled - NewWindow"))
      {
      HDC dc = GetDC(hwnd);
      CDeviceContextDlg *dlg = (CDeviceContextDlg *)AfxGetMainWnd();
      if((dlg->Clientdc.Attach(dc)))
      {
      dlg->Clientdc.SetBkColor(RGB(255,0,0));

      	};
      };
      return TRUE;
      

      }

      It gives run-time error. I debugged it and in that line:

      HDC dc = GetDC(hwnd);

      In Debugger:

      dc 0x53011041 {unused=??? } HDC__ *
      unused CXX0030: Error: expression cannot be evaluated

      Is it because process gdi handle table is process specific and it is impossible to get DC and draw another process' window? Or is there a way to draw another process' window or getpixel values? Thanks

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Maybe this [^] helps. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      R 1 Reply Last reply
      0
      • S sawerr

        Hi I want to make a program that retrieves other windows' pixel values. First i made a default application which title name is "Untitled - NewWindow" and one more MFC project which is trying to draw to that window. The code is:

        void CDeviceContextDlg::OnBnClickedButton1()
        {
        ::EnumWindows(EnumWindowsProc, NULL);
        }

        BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
        {
        if(hwnd == FindWindow(NULL,L"Untitled - NewWindow"))
        {
        HDC dc = GetDC(hwnd);
        CDeviceContextDlg *dlg = (CDeviceContextDlg *)AfxGetMainWnd();
        if((dlg->Clientdc.Attach(dc)))
        {
        dlg->Clientdc.SetBkColor(RGB(255,0,0));

        	};
        };
        return TRUE;
        

        }

        It gives run-time error. I debugged it and in that line:

        HDC dc = GetDC(hwnd);

        In Debugger:

        dc 0x53011041 {unused=??? } HDC__ *
        unused CXX0030: Error: expression cannot be evaluated

        Is it because process gdi handle table is process specific and it is impossible to get DC and draw another process' window? Or is there a way to draw another process' window or getpixel values? Thanks

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        I tested this real quick, and it works fine for me. This example finds a NotePad instance and Blts the Notepad window to my window (note that "*pThis" in the callback == the HWND for the dialog window)...

        BOOL CALLBACK CMFCTesterDlg::EnumWindowsProc(HWND hwnd, LPARAM lParam)
        {
        if(hwnd == ::FindWindow(NULL, L"Untitled - Notepad"))
        {
        CMFCTesterDlg *pThis = (CMFCTesterDlg *)lParam;
        HWND hwnddest = *pThis;

        	RECT rect;
        	::GetWindowRect(hwnd, &rect);
        	HDC dc = ::GetDCEx(hwnd, NULL, DCX\_WINDOW);
        	HDC destdc = ::GetDC(hwnddest);
        	::BitBlt(destdc, 0, 0, rect.right-rect.left, rect.bottom-rect.top, dc, 0, 0, SRCCOPY);
        	::ReleaseDC(hwnddest, destdc);
        	::ReleaseDC(hwnd, dc);
        };
        return TRUE;
        

        }

        void CMFCTesterDlg::OnOK()
        {
        ::EnumWindows(&CMFCTesterDlg::EnumWindowsProc, (LPARAM)this);
        }

        Also, why the search within a search (FindWindow() within an EnumWindows() callback)?? :)

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        S 1 Reply Last reply
        0
        • M Mark Salsbery

          I tested this real quick, and it works fine for me. This example finds a NotePad instance and Blts the Notepad window to my window (note that "*pThis" in the callback == the HWND for the dialog window)...

          BOOL CALLBACK CMFCTesterDlg::EnumWindowsProc(HWND hwnd, LPARAM lParam)
          {
          if(hwnd == ::FindWindow(NULL, L"Untitled - Notepad"))
          {
          CMFCTesterDlg *pThis = (CMFCTesterDlg *)lParam;
          HWND hwnddest = *pThis;

          	RECT rect;
          	::GetWindowRect(hwnd, &rect);
          	HDC dc = ::GetDCEx(hwnd, NULL, DCX\_WINDOW);
          	HDC destdc = ::GetDC(hwnddest);
          	::BitBlt(destdc, 0, 0, rect.right-rect.left, rect.bottom-rect.top, dc, 0, 0, SRCCOPY);
          	::ReleaseDC(hwnddest, destdc);
          	::ReleaseDC(hwnd, dc);
          };
          return TRUE;
          

          }

          void CMFCTesterDlg::OnOK()
          {
          ::EnumWindows(&CMFCTesterDlg::EnumWindowsProc, (LPARAM)this);
          }

          Also, why the search within a search (FindWindow() within an EnumWindows() callback)?? :)

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          S Offline
          S Offline
          sawerr
          wrote on last edited by
          #4

          Thank you Mr.Mark Salsbery. Got it. :)

          1 Reply Last reply
          0
          • C CPallini

            Maybe this [^] helps. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #5

            Just fixed the vote on this post of yours. :|

            Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

            C 1 Reply Last reply
            0
            • R Rajesh R Subramanian

              Just fixed the vote on this post of yours. :|

              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              Thank you friend. :rose: Anyway don't bother. Let's my personal troll having a touch of glory... :-D

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              R 1 Reply Last reply
              0
              • C CPallini

                Thank you friend. :rose: Anyway don't bother. Let's my personal troll having a touch of glory... :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                I am searching ebay for a device that would let you find and punch one troll at a time, with a folded fist, through the internet.

                Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                C 1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  I am searching ebay for a device that would let you find and punch one troll at a time, with a folded fist, through the internet.

                  Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Well, you can buy one (ore more) of these [^] and then proceed with voodoo ceremony. :laugh:

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  R 1 Reply Last reply
                  0
                  • C CPallini

                    Well, you can buy one (ore more) of these [^] and then proceed with voodoo ceremony. :laugh:

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #9

                    :laugh:

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                    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