Is it possible to attach another apps' window DC? (About Gdi handle table and objects)
-
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 evaluatedIs 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
-
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 evaluatedIs 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
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] -
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 evaluatedIs 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
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:
-
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:
-
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]Just fixed the vote on this post of yours. :|
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]
-
Just fixed the vote on this post of yours. :|
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]
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] -
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]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++[^]
-
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++[^]
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] -
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]:laugh:
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]