Link to home
Start Free TrialLog in
Avatar of jbirk
jbirk

asked on

why is WindowFromDC returning NULL?

I'm hooking the textout messages being sent, and when the text message for a disabled button is sent, calling WindowFromDC on the hdc sent in the message returns null.  This only seems to happen to me on the disabled buttons text, not all the other text.  If I use GetLastError to check what happened, it reports: "The operation completed successfully.".  What's going on? (this is on W2K system)

Example code:
BOOL  WINAPI TextOutWCallbackProc(HDC hdc, int nXStart, int nYStart, LPCWSTR lpwString, int cbCount)
{
HWND hwnd1;
hwnd1 = WindowFromDC(hdc);


Thanks,
Josh
Avatar of jbirk
jbirk

ASKER

OK, I just ran it again, and I got: "Cannot create a file when that file already exists." a couple of times.  That message doesn't seem to make any sense though...  ?

I used:
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0,NULL);
ofstream ofs("c:\\textout.txt",ios::ate|ios::out);
ofs<<"WindowFromDC failed::"<<(LPCTSTR)lpMsgBuf<<endl;
ofs.close();
LocalFree( lpMsgBuf );

for printing the error....
Avatar of jkr
Well, MSDN states: "The return value is the handle of the window associated with the given DC. If no window is associated with the given DC, the return value is NULL. "
Avatar of jbirk

ASKER

OK, I'm still unclear on why no window would be associated with any DC...  What I'm trying to do is find the DCs ParentOwner, not the actual window associated with it, but I need an HWND or CWnd in order to call GetParentOwner.  How do I do this in the case where no window is associated with the DC?

Thanks,
Josh
In the case of Memory DC there is no Parent window.

If you are hooking the TextOut, you will only get the HWND of the DC that are directly drwaned to the window.

The first case is like, creating a memory DC that is compatible to the window dc, then drawing the text ino that, and blitting it into the main DC. in theis case yuo will not ge the exact window of the TextOut

Roshmon
Avatar of jbirk

ASKER

Hmm...  I'm trying to translate the coordinates sent in  int nXStart, int nYStart to coordinates relative to the main window associated with the text being drawn.  Somehow windows must be translating the coordinates to global coordinates at least because the values getting sent are small (like 5,1) even when the text is getting drawn around the middle of the screen.  So somehow windows is translating that 5,1 to say 532,203.  How do I do that if I can't the window associated with the DC?

Thanks,
Josh
ASKER CERTIFIED SOLUTION
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jbirk

ASKER

Well it sounds like it may be possible, but too difficult for now.  It seems to only be happening on disabled items, so I guess I'll just ignore them for now.  If it turns out I for sure need them down the road, I'll give your suggestion a try.

So I'm gonna close this question now.

Thanks everyone for your help!
-Josh