CDC/HDC????

I am trying to develop a print screen utility for a windows CE program.
 I am writing it in Embedded VC++ and running into some weirdness.

My plan of action is to use GetClientRect() to find the location of the
window, then loop through each pixel in that region and perform a
GetPixel() to get the RGB value.

My problem is that I need a HDC in order to perform GetPixel().  I used
GetActiveWindow() to get an HWND, then used FromWindow() to get a CWnd
pointer.  I then tried the following line to get an HDC.

HDC hdc=pWnd->GetWindowDC();

When I compile though I get the following error.

error C2440: 'initializing' : cannot convert from 'class CDC *' to
'struct HDC__ *'

I thought GetWindowDC() was supposed to redurn an HDC, why is it
returning a CDC?

I think its obvious that I am not too familiar with these classes.  Am
I totally off base here?  Is there an easier way to do what I want?

Thanks,
pico

0
picopir8 (4)
9/26/2006 1:28:37 PM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
923 Views

Similar Articles

[PageSpeed] 20

picopir8@hotmail.com wrote:

> I am trying to develop a print screen utility for a windows CE program.
>  I am writing it in Embedded VC++ and running into some weirdness.
> 
> My plan of action is to use GetClientRect() to find the location of the
> window, then loop through each pixel in that region and perform a
> GetPixel() to get the RGB value.
> 
> My problem is that I need a HDC in order to perform GetPixel().  I used
> GetActiveWindow() to get an HWND, then used FromWindow() to get a CWnd
> pointer.  I then tried the following line to get an HDC.
> 
> HDC hdc=pWnd->GetWindowDC();
> 
> When I compile though I get the following error.
> 
> error C2440: 'initializing' : cannot convert from 'class CDC *' to
> 'struct HDC__ *'
> 
> I thought GetWindowDC() was supposed to redurn an HDC, why is it
> returning a CDC?

Because you use MFC and don't want to use the plain old GDI API. If you 
want to have just the Handle you could get it as follows:

CDC* pTempDC = pWnd->GetWindowDC ();
HDC hdc = pTempDC->GetSafeHdc ();
pWnd->ReleaseDC (pTempDC);

However, working with the Win32 Handle is somewhat cumbersome.

> I think its obvious that I am not too familiar with these classes.  Am
> I totally off base here?  Is there an easier way to do what I want?

Nope. Since MFC supposes that you never want to access the API directly, 
it gives you only pointers to the wrapper classes like CDC or CWnd. If 
you need the underlying handles, you have to ask for them.

Regards,
Stuart
0
DerTopper (96)
9/26/2006 2:03:38 PM
>I am trying to develop a print screen utility for a windows CE program.
> I am writing it in Embedded VC++ and running into some weirdness.
>
> My plan of action is to use GetClientRect() to find the location of the
> window, then loop through each pixel in that region and perform a
> GetPixel() to get the RGB value.

Note that GetClientRect() returns rectangle coordincates relative to that 
window's client area. For all practical purposes, the top, left will always 
be 0, 0.

> My problem is that I need a HDC in order to perform GetPixel().  I used
> GetActiveWindow() to get an HWND, then used FromWindow() to get a CWnd
> pointer.  I then tried the following line to get an HDC.
>
> HDC hdc=pWnd->GetWindowDC();
>
> When I compile though I get the following error.
>
> error C2440: 'initializing' : cannot convert from 'class CDC *' to
> 'struct HDC__ *'
>
> I thought GetWindowDC() was supposed to redurn an HDC, why is it
> returning a CDC?
>
> I think its obvious that I am not too familiar with these classes.  Am
> I totally off base here?  Is there an easier way to do what I want?

As you found, CWnd::GetWindowDC() returns a pointer to a CDC. You can easily 
bypass MFC and use ::GetWindowDC() to return an HDC.

-- 
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com


0
jwood (1291)
9/26/2006 3:21:00 PM
Use like this as MFC provide flexibility to use win32 api so why not
use it

CWindowDC pWndDC(pWnd);

//////run your loop here
RGB Px = pWndDC.GetPixel(? , ?) ,


Thanks
Sunil Singh


"Nobody is prefect and remain foreever here except the Universal Truth"

0
eitmsn1 (21)
9/27/2006 11:10:23 AM
apologize , in that i have done mistake as in hurry ,may be that you
confused

Use like this as MFC provide flexibility to use win32 api so why not
use it

CWindowDC pWndDC(pWnd);


//////run your loop here
COLORREF Px = pWndDC.GetPixel(? , ?) ,


Thanks
Sunil Singh


"Nobody is prefect and remain foreever here except the Universal Truth"

0
eitmsn1 (21)
9/27/2006 11:52:55 AM
Thanks everyone,  I now have it mostly working.  I can use GetPixel to
generate a bitmap, however all objects in the window that I am
attempting to capture are blank (ie no buttons, edit boxes, static
text, nothing, just blank rectangles on the windows bitmap).

0
picopir8 (4)
9/27/2006 7:11:15 PM
Reply:

Similar Artilces:

CDC::Rectangle and client area
Hello, Lets say that place a rectangle border on the client area at 10,10,45,45. And lets say that client area is 200x200 or so. How can I make the recangle border resize itself in proportion to the changes of the client area? In other words, lets say I resize the client to 250x200 etc. How can I adjust the border of the rectangle to widen itself? What would the formula look like to figure out the percentage of adjustment? This is pretty simple arithmetic. You have a rectangle which is 10,10, 45, 45. Therefore it is a 35x35 rectangle. It represents 35/200 of the area, and it starts 10/...

Getting CDC
I want to display something in the OnUpdate method, how do I get a handle to the CDC from here? news.microsoft.com wrote: > I want to display something in the OnUpdate method, how do I get a handle to > the CDC from here? > > This sounds like a beginner's mistake. You can call GetDC, but that is poor design and probably won't work the way you intend. Painting in OnUpdate will be lost after the window is minimized/restored or covered/uncovered. The default OnUpdate solves this problem by calling Invalidate(). Then the painting is done in the resulting OnDraw,...

CDC::arc() with big pen crash on win98
Hi, I've encountered a problem that occurs only under Win98: I have a dc object and I'm trying to draw an arc (I'm using the CDC::arc() function). I'm using a pen that has a big width. The problem is that it crashes sometimes (only when the arc is big and only a small part of it is displayed). I tried to look in the knowledge base for this kind of bug and I found something but it was a Win95 bug. How can I know when the CDC::arc() function will crash? For example, I had a problem when the bounding rectangle was bigger than 32767 (it crashed also under Win98) but since I knew wh...

which CDC
Hello, I am a bit confused as to which dc to call in the following situation. I have a dialog app with a static control on it that I am using to paint a graph on. I am doing the drawing in the lbuttondown function in the dialog class. My qestion is how do I call the device context to paint on the static control. Do I use the CClientDC dc(this); to get the device context of the dialog and do the drawing using this dc, or do I call CClinetDC dc(&m_myStaticControl); or dc = m_myStaticControl.GetDC(); to get the dc of the control, and use it to do the drawing? Are all these ...

HDC and CDC
I am attempting for the first time to use CreateDibSection and am puzzled as to how to use HDC within the MFC framework. I can't see that CDC returns a handle, so I am wondering if these two are mutually exclusive. Is there a bridge between the handle and the class? "Steve Russell" <srussell@removethisinnernet.net> wrote in message news:uQoa24INEHA.624@TK2MSFTNGP11.phx.gbl... > I am attempting for the first time to use CreateDibSection and am puzzled as > to how to use HDC within the MFC framework. I can't see that CDC returns a > handle, so I am wonderin...

Strange CDC::MoveTo and CDC::MoveTo performance problem
Hi, I am writing a chart drawing program and encountered a strange performance problem with CDC::MoveTo and CDC::LineTo function calls. To avoid flickering, the program does the drawing on a memory device context(m_DrawDC) and then BitBlt to the real display device. Here is the code fragment: void CView::OnDraw(CDC* pDC) { .... if(m_DrawDC.CreateCompatibleDC(pDC)) { ... for(INT n=0;n<m_DataSeriesArray.GetSize();n++) { m_DataSeriesArray[n].PlotLine(&m_DrawDC); } ... pDC->BitBlt(0, 0, ClientRect.Width(), ClientRect.Height(), &m_DrawDC, 0, 0, SRCCOPY); } m_Dr...

CDC:TextOut and Plus or Minus Sign
I am using MSVC 7.1.3088 on MS XP 5.1 service pack 2. I am trying to write the plaus or misu sign to a display window using CDC:TextOut. I can write the symbol to a character buffer using sprintf(csText, "%s (%c%f %s)", csLabel, 177, fError, csUnits); which gives me "Elevation (=B17.926902 Meters)" in csText. However, when I try to write it to the window using TextOut(DeviceContext, (int)(pWindowPoint.x), (int)(pWindowPoint.y), (LPCTSTR)csText, (int)stStringlength); I get a rectangle where the =B1 (plus or minus) sign should be. Is there a way I can write the PM sign to ...

Pb CDC::Arc
Hello, I have a problem with this function of the GDI -> CDC::Arc. The arc is drawn, but there is also a line joining the 2 extremities of the arc. I don't want to have this line. Thank you for your help, Christophe, Christophe, Arc should not do this. Perhaps you could show the code where you are drawing the arc? Johan Rosengren Abstrakt Mekanik AB "Christophe Jacquelin" <cjacquel@club-internet.fr> a �crit dans le message de news:401ab231$0$6976$7a628cd7@news.club-internet.fr... > Hello, > > I have a problem with this function of the GDI -> CDC::Arc....

trouble with CDC
I'm doing a program that needs to capture bitmaps off a window of another program. I'm doing okay except I have an intermittent failure. I admit I'm not testing for all the possible MFC error returns at this early stage, but even I did I'm not sure how I'd deal with this error. My piece of code looks like this. CDC m_memoryDc; CBitmap m_bitmap; void CMyDlg::GrabRect(CWnd *pWnd, CRect rc) { CDC * pSourceDc = pWnd->GetDC(); Sleep(50); // !!!!! need this or the crash happens almost right away m_bitmap.Detach(); m_bitmap.CreateCompatibleBitmap(pS...

CDC::SelectObject
Dear people, I'd like to select bitmaps into a device context created by CreateCompatibleDC. My problem is that if I select a bitmap stored in a CImageList, the result from SelectObject is Null indicating failure. But if I select a bitmap loaded with LoadBitmap from a resource, I can select it into the same DC without problems. What am I doing wrong? Any suggestions appreciated! At the point where you select the bitmap, it needs to be a valid bitmap. You have not shown the code you are using, so it is hard to tell what you have done. joe On Fri, 5 Dec 2008 02:51:42 -0800 (PST), F...

Transferring control of CClientDC to CDC
Hi, I have a class MSWinDisplayManager which I want to take a CClientDC device context so that it's member functions can perform drawing routines on it. I want the class to have it's own CClientDC member which all the methods have access to draw on. My constructor looks like this: MSWinDisplayManager::MSWinDisplayManager(CClientDC& win) { private_win.attach(win); } This is called by the user like: CClientDC dlg(this); MSWinDisplayManager wdm(dlg); then I want to do things like: wdm.drawCars(); The problem I have is that private_win isn't getting control of the device ...

CDC.TextOut
Dear All! Has anyone had problems with the screen flickering using the CDC member fuction "TextOut", with an update rate of 1000 Hz using on-screen counters in the View's OnDraw() function, or anything else ? -- Engineering On Mon, 12 Mar 2007 02:13:18 -0700, C Hill MBSC <CHillMBSC@discussions.microsoft.com> wrote: >Dear All! > >Has anyone had problems with the screen flickering using the CDC member >fuction "TextOut", with an update rate of 1000 Hz using on-screen counters in >the View's OnDraw() function, or anything else ? If I reca...

CDC::SelectClipRgn
Hi, I have to draw a line (using MoveTo and LineTo) that has to be clipped to a portion of the screen. The starting and ending coordinates are on opposite ends outside the visible area. Clipping it does the trick of showing only a portion of the line within the area it is supposed to draw in. When using a printer DC to print (standard print preview of an MDI application) the clipping doesn't seem to work, so the line gets drawn all over the place.. even outside the paper boundaries in print-preview. What can I do about this? It would be quite a hassle to have to calculate the 'inte...

CDC same as CClientDC
Are they the same? -- @~@ Might, Courage, Vision, SINCERITY. / v \ Simplicity is Beauty! May the Force and Farce be with you! /( _ )\ (Xubuntu 8.04.1) Linux 2.6.26.5 ^ ^ 12:38:01 up 4 days 21:28 3 users load average: 1.03 1.03 1.00 ? ? (CSSA): http://www.swd.gov.hk/tc/index/site_pubsvc/page_socsecu/sub_addressesa/ Not exactly, no. CClientDC is derived from CDC. -- Jonathan Wood SoftCircuits Programming http://www.softcircuits.com "Man-wai Chang ToDie (33.6k)" <toylet.toylet@gmail.com> wrote in message news:%23M5oQPiFJHA.4304@TK2MSFTNGP02.phx.gbl... > &g...

why i can not use CDC::TransparentBlt
I am using VC 6.0 sp6 the sdk is the most latest but when i use CDC::TransparentBlt the complier report error the error message is: G:\myproject\game004\Man.cpp(72) : error C2039: 'TransparentBlt' : is not a member of 'CDC' d:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(636) : see declaration of 'CDC' Error executing cl.exe. Exactly what error do you get? -- Regards, Nish [VC++ MVP] http://www.voidnish.com http://blog.voidnish.com "howyougen" <take..care@tom.com> wrote in message news:OpvMMsFjFHA.132@TK2MSFTNGP10.p...

CDC problems
Ok this is really starting to mess things up for me and ive searched the msdn library until my head hurts, so ive decided to ask you guys. Theres two things im having problems with: 1) Copying an image to a temporary CDC, then stretching it onto another CDC. For example this code generates runtime errors for me (while if i use this->GetDC() it does not): CImage cImg; CDC dcImg, dcTemp; dcImg.Attach(cImg.GetDC()); dcTemp.CreateCompatibleDC(this->GetDC()); dcTemp.StretchBlt(0, 0, 250, 250, &dcImg, 0, 0, 100, 100, SRCCOPY); 2) this->GetDC() will not let me change the brush, p...

CDC::m_hAttribDC
Hi, I still have to see a purpose for this data member. With the exception of CMetafileDC, m_hDC is always equals to m_hAttribDC. In my applications, all it does is adding overhead in all the functions that first check if (m_hDC == m_hAttrib) and then check if ( m_hAttrib != NULL). Does someone has a concrete example of where m_hAttribDC is actually helpful? Thank you, olanglois, The attribute DC is also used for print previews. I assume you are doing something really bizarre if you often need to check equality the way you describe. Johan Rosengren Abstrakt Mekanik AB <olanglois@s...

CDC Help!
One of the common shapes that seems to be missing from the CDC members is the arrow. Is there a good way to do arrows? I need to create arrows around the circumference of a circle pointing toward the center of the circle. Any hints on how this is best done? Ron H. ----------------- www.Newsgroup-Binaries.com - *Completion*Retention*Speed* Access your favorite newsgroups from home or on the road ----------------- I'm not sure if CDC has any line caps. In the past I used GDI+ to draw arrows and different kind of line caps. IMHO, the default line caps don't scale very well, so ...

CDC/HDC????
I am trying to develop a print screen utility for a windows CE program. I am writing it in Embedded VC++ and running into some weirdness. My plan of action is to use GetClientRect() to find the location of the window, then loop through each pixel in that region and perform a GetPixel() to get the RGB value. My problem is that I need a HDC in order to perform GetPixel(). I used GetActiveWindow() to get an HWND, then used FromWindow() to get a CWnd pointer. I then tried the following line to get an HDC. HDC hdc=pWnd->GetWindowDC(); When I compile though I get the following error. erro...

CDC::GetClipRgn ?
Hello, how can I get (save) the current clip region of a CDC? There is no corresponding method and ::GetClipRgn doesn't accept a CDC. Thanks! -- Daniel Daniel Lidstr�m wrote: > Hello, > > how can I get (save) the current clip region of a CDC? There is no > corresponding method and ::GetClipRgn doesn't accept a CDC. > Thanks! > > -- > Daniel CDC::GetClipBox( LPRECT lpRect ) -- Scott McPhillips [VC++ MVP] On Tue, 27 Jan 2004 09:23:17 -0500, Scott McPhillips [MVP] wrote: > Daniel Lidstr�m wrote: >> Hello, >> >> how can I get (save) ...

CDC::DrawEdge
What rectangle is used to draw a 3d edge by DrawEdge? In my opinion DrawEdge() draws on the right and bottom sides always one pixel less than the given rect which is not mentioned in the documentation. But this would be the same behaviour as known from FillRect() and FillSolidRect(). I made some lines around but *on* my window rectangle using LineTo() and than I called DrawEdge with EDGE_SUNKEN and BF_RECT on exactly the same rect. The top left corner is ok, DrawEdge draws onto the corner, but the bottom right corner looks like this: gw| g: grey gw| w...

Rotating text in a CDC
Is it possible to rotate text in a simple way inside a CDC object? I added the next code in OnPaint for a CWnd object, but the text still appears "normal". I tried setting the lfEscapament and lfOrientation to 1, 9, 90, etc (both simultaneously), but nothing worked. void CMyWnd::OnPaint() { CPaintDC dc(this); // device context for painting ..... CFont *pOldFont,*pCurFont,*pNewFont; pNewFont = new CFont(); // Load the present font from the DC pCurFont = dc.GetCurrentFont(); // Just change the orientation LOGFONT logFont; pCurFont->GetLogFont(&logFont); logFo...

CDC Class
I am using the CDC Class to print some data out that does not utilize the CView class. The data is also tab delimited. One thing I ran across is that if I spread the data way out based on the tabs it truncates. I have searched MSDN and I am sure I missed it. Is there a way to set the device to "wrap" then set it back to "wrap"? Bob You can not make DC wrap your output. But you can make DrawString function wrap the line with DT_WORDBREAK flag. To figure out where to print next line, call DrawText with DT_CALCRECT flag to know the height of the printed string. &qu...

CDC: buffering
Hello, I presently work on an application which visualises measured data (that are read from a file). This works fine, but when the window of my program has to be redrawn, the displayed data get lost (of course). Since the amount of these data is very high, I could not re-read all data after calling OnDraw, so I need some sort of buffering (of the displayed values). I guess this buffering can be realised by means of the method CreateCompatibleDC, but I don't know how??????? Maybe someone can give me a few advices.... Thanks, Eddi Joseph M. Newcomer wrote: > What I did in thi...

CDC
Hello, My current application reads measuring data from a file and draws a diagram. Since I don't want to lose this diagram in case the window has to be re-drawn, I draw it to an "in-memory bitmap" and copy this bitmap to the screen. I use the following code for the creation of the bitmap: CDC m_MemDC; CBitmap m_Bitmap; CDC *dc = GetDC(); m_MemDC.CreateCompatibleDC(dc); m_Bitmap.CreateCompatibleBitmap(dc, rect.Width(), rect.Height()); m_MemDC.SelectObject(m_Bitmap); Unfortunately, the colors used to display the data, differ from their expected values. For example, if I w...