Archived

This topic is now archived and is closed to further replies.

CuteBug

HELP - MFC - GetPixel doesnt work!!!

Recommended Posts

In an SDI application I created a new Class, say, CTest. In one of the member functions of CTest I included the following code -
  
void CTest::SomeFunc(...)
{ 
     ...
  
     int x;
  
     int y;
     ...

     CFrameWnd* pFrame = (CFrameWnd*)(AfxGetApp()->m_pMainWnd);
     CMyAppView* pView = (CMyAppView*)(pFrame->GetActiveView());
     CClientDC dc(pView);

     COLORREF pix = dc.GetPixel(x, y);
  
     ...
}  
  
But the function GetPixel doent return the correct value of the the pixel color at (x,y); when I debugged it pix always had the value 0xffffffff. Please help... "There is a Bug in every Code!"

Share this post


Link to post
Share on other sites
yeah lose it unless your making a business program, plus I dont think your view is correct.

maybe be true or may not be true

Share this post


Link to post
Share on other sites
I dont know how to use GetLastError(). But I gave the following command after

COLORREF pix = dc.GetPixel(x, y);

DWORD d = GetLastError();


Instead of getting the DC thru CClientDC I tried passing the pDC pointer from the OnDraw() function of the View class to a ststic member pDC of my own class.

while debugging the value of d is 0xcccccccc

and that of pDC is 0x0012f9d4 {hDC = 0xcccccccc attrib = oxcccccccc}

can u help me out?

"There is a Bug in every Code!"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I dont know how to use GetLastError(). But I gave the following command after

COLORREF pix = dc.GetPixel(x, y);

DWORD d = GetLastError();


Instead of getting the DC thru CClientDC I tried passing the pDC pointer from the OnDraw() function of the View class to a ststic member pDC of my own class.

while debugging the value of d is 0xcccccccc

and that of pDC is 0x0012f9d4 {hDC = 0xcccccccc attrib = oxcccccccc}

can u help me out?

Share this post


Link to post
Share on other sites
quote:
Original post by CuteBug
I dont know how to use GetLastError(). But I gave the following command after

COLORREF pix = dc.GetPixel(x, y);

DWORD d = GetLastError();

Instead of getting the DC thru CClientDC I tried passing the pDC pointer from the OnDraw() function of the View class to a ststic member pDC of my own class.

while debugging the value of d is 0xcccccccc

and that of pDC is 0x0012f9d4 {hDC = 0xcccccccc attrib = oxcccccccc}

can u help me out?

"There is a Bug in every Code!"


The 0xcccccccc means that the DC was not initialized (0xcccccccc is the value that the debugger assigns uninitialized variables). Can you show me the code you use to pass the pDC pointer from the OnDraw function to the static member of your class (perhaps the entire OnDraw function also)?

I also encourage you to learn how to use the MSDN because then you would have been able to look up how to use GetLastError().




Qui fut tout, et qui ne fut rien
Invader''s Realm

Share this post


Link to post
Share on other sites
I added two static variables to my class CTest.

Test.h

  
static CDC* pDC;
static bool gotView;


Test.cpp

  
CDC* Spawn::pDC = NULL;
bool Spawn::gotView = false;


Then I initialized pDC in the OnDraw() function

MyView.cpp

  
void CMyView::OnDraw(CDC* pDC)
{
Spawn::pDC = pDC;
Spawn::gotView = true;

CFloodDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
}


I used the gotView variable to ensure that I will be using the member functions of pDC only after it is initialized.

"There is a Bug in every Code!"

Share this post


Link to post
Share on other sites
Don''t listen to anyone one of these kids
nick5454
MattS423
Oberon_Command

As for your problem test the value it is returning check to see it''s not CLR_INVALID. Remeber that the x and y coordinates are logical coordinates.

Share this post


Link to post
Share on other sites
yeah the GetPixel function is returning CLR_INVALID. And the MSDN says CLR_INVALID is returned when the coordinates are outside the clipping region.

I tried to convert the coordinates x and y to device coordinates using the LPtoDP function


  
CPoint P(x, y);
pDC->LPtoDP(&P,1);
x= P.x;
y =P.y;


But when I debugged the program the foloowing assertion failed


  
_AFXWIN_INLINE void CDC::LPtoDP(LPPOINT lpPoints, int nCount) const
{ ASSERT(m_hAttribDC != NULL); VERIFY(::LPtoDP(m_hAttribDC, lpPoints, nCount)); }


It means that pDC is NULL.

So I tried to get a pointer to the View class using CClientDC


  
CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
CFloodView * pView = (CFloodView*)pFrame->GetActiveView();
CClientDC dc(pView);

CPoint P(x, y);
pDC->LPtoDP(&P,1);
x= P.x;
y =P.y;

pix = dc.GetPixel(x, y);


But still GetPixel is returning CLR_INVALID.

what should I do now?





"There is a Bug in every Code!"

Share this post


Link to post
Share on other sites
I am getting the initial x and y coordinates from the OnLButtonDown function in the View Class and I am passing these coordinates to the class CTest.
Also when the program executes I am definitely clicking within the clipping region.

Is there a better method?

"There is a Bug in every Code!"

Share this post


Link to post
Share on other sites
Is there any other way I might be able to call the GetPixel function from an external class?


"There is a Bug in every Code!"

Share this post


Link to post
Share on other sites
Maybe the x and y values are somehow changing from when you get them from OnLButtonDown() and GetPixel()? I''d set up a log or something to check to make sure that they are the same.



Qui fut tout, et qui ne fut rien
Invader''s Realm

Share this post


Link to post
Share on other sites
No the values of x and y are not changing when passed to the OnLButtonDown or GetPixel functions.

Is there any method by which we can implement the drawing function in a user defined class. Or is that we can call drawing functions in the view class only?

Is there any other metho to obtain the device context?

Share this post


Link to post
Share on other sites
CWnd::GetDC() will get a pointer to CDC of the window
CWnd::GetDCEx() will enable clipping (you have to specify a region) and get a pointer to the CDC

However, I believe that CClientDC uses CWnd::GetDC(), which is not clipped. At least that''s what the documentation says:

quote:

The CClientDC class is derived from CDC and takes care of calling the Windows functionsGetDC at construction time andReleaseDC at destruction time. This means that the device context associated with a CClientDC object is the client area of a window.





Qui fut tout, et qui ne fut rien
Invader''s Realm

Share this post


Link to post
Share on other sites