CRect.PtInRect(point) problem on scrolling.
Hello all,
I am developing this little app in which there's a table and the user may scroll sideways to view the full table and choose to make an entry in any of the cells by clicking on the respective cell.
When the screen is not scrolled (m_nHScrollPos is zero) and when I click on a cell, the app creates a CEdit control in that particular cell and it's all good. However, when I scroll sideways (m_nHScrollPos > 0) to further reveal the table and then click on the cell which appears, the app creates a CEdit control which is offset greatly to the right (from it's actual required position).
It appears to be a problem with the origin to my inexperienced programmer mind. Please note that to bring about the scroll effect, in the OnPaint handler, I have used pDC->SetwindowOrg(m_nHscrollPos, m_nVScrollPos).
I'd be grateful if you could shed light on this problem. I've been working on this problem for quite a while already...
Thanks a lot, cheers.
Re: CRect.PtInRect(point) problem on scrolling.
How do you create the CEdit control? How do you tell it where it's placed?
Re: CRect.PtInRect(point) problem on scrolling.
This is how I create the CEdit control:
Code:
void CMainChildWnd :: OnLButtonDown(UINT nFlags, CPoint point)
{
GetClientRect(&cRectChildWnd);
if(cNewEntry.crEditorAdd.PtInRect(point))
{
if(cNewEntry.bfSelected == FALSE)
{
//Create the field text control
cNewEntry.SetValues(&cRectChildWnd, TYPE_BANKACC, m_nHScrollPos);
cNewEntry.CreateField(this);
SelectNewEntry();
return;
}
}
else if(cNewEntry.crEntries[0].PtInRect(point)) //input region (1st entry rect)
{
if(cNewEntry.bfSelected == TRUE)
{
cNewEntry.DestroyTextIn();
cNewEntry.nFieldNo = 0;
GetClientRect(&cRectChildWnd);
cNewEntry.SetValues(&cRectChildWnd, TYPE_BANKACC, m_nHScrollPos);
cNewEntry.CreateField(this);
cNewEntry.ceTextIn.SetFocus();
//Force repaint to ghost text
Invalidate();
SendMessage(WM_PAINT);
}
}
}
The function CreateField:
Code:
void CEntry :: CreateField(CWnd* pHome)
{
pParent = pHome;
ceTextIn.Create(ES_CENTER | WS_VISIBLE | ES_NOHIDESEL, crEntries[nFieldNo], pParent, ID_TEXTFIELD);
if(bAlreadyInputBefore)
SelectGhostTexts();
}
SelectGhostTexts() displays the old (destroyed) CEdit's data. cNewEntry.SetValues(..) sets the exact CRects for all the required CEdit cells which are crEntries[4]. Finally, cNewEntry.DestroyTextIn() destroys the window after retrieving the CEdit's information.
Thanks for replying.
Re: CRect.PtInRect(point) problem on scrolling.
Quote:
Originally Posted by
nitinmalapalli
cNewEntry.SetValues(..) sets the exact CRects for all the required CEdit cells which are crEntries[4].
Then I guess that's where you have to adjust the position of the rectangle for the scrolling.
Re: CRect.PtInRect(point) problem on scrolling.
Quote:
Originally Posted by
D_Drmmr
Then I guess that's where you have to adjust the position of the rectangle for the scrolling.
I have been trying precisely that. Any suggestions as to how I can do that? What sort of adjustment would I have to do? I have tried many combinations but none of them work.
Re: CRect.PtInRect(point) problem on scrolling.
Quote:
Originally Posted by
nitinmalapalli
I have been trying precisely that. Any suggestions as to how I can do that? What sort of adjustment would I have to do? I have tried many combinations but none of them work.
I had hoped that perhaps this was a standard problem.
Re: CRect.PtInRect(point) problem on scrolling.
Quote:
Originally Posted by
nitinmalapalli
I have been trying precisely that. Any suggestions as to how I can do that? What sort of adjustment would I have to do? I have tried many combinations but none of them work.
It's just plain logic. Think what the values should be and adjust the way the actual values are calculated such that they match.
You can also do this using trial-and-error by scrolling just a little bit and observing how the edit is misplaced. If it's too far to the right, you'll have to decrease the left and right values in the rectangle. If it's too low, you'll have to increase the top and bottom values.
Re: CRect.PtInRect(point) problem on scrolling.
I could do that. But wouldn't it be a problem when I run the app on a computer with different monitor specifications having different resolutions? Dabbling with pixels can cause such errors, right?
Re: CRect.PtInRect(point) problem on scrolling.
Quote:
Originally Posted by
nitinmalapalli
I could do that. But wouldn't it be a problem when I run the app on a computer with different monitor specifications having different resolutions? Dabbling with pixels can cause such errors, right?
Different resolution, different settings, different theme, even a different window placement could reveal a bug in your code. So you should test for all of these things to make sure your code works correctly in all situations. If your code uses magic numbers, it's likely to fail in a different situation. So make sure you call the proper Windows functions to get the size of scrollbars, etc.