Ratio result placing a square not working - when resizing window
-
Hi, I am very confused to why this isn't working. My starting window width size is 756 I got this result by declaring: CRect Rect; GetClientRect(&Rect); Rect.Width(); I divide the 756 / 2 to plae the triangle in the centre of the window. I have set up a message handler for the left and right arrow key press, which moves the triagle by 10. if the user resizes the the window, the window should display in proportion, by the statement. widthRatio = Rect.Width() / 756; then * the x posistion by the widthRatio I have tried declaring the variables 'float, double, and int I can not seem to find a reason why this shouldn't work the posistion remains the same if I expand, and snaps to the left hand of the screen if the size has gone smaller does anybody know whats going on cheers Simon
-
Hi, I am very confused to why this isn't working. My starting window width size is 756 I got this result by declaring: CRect Rect; GetClientRect(&Rect); Rect.Width(); I divide the 756 / 2 to plae the triangle in the centre of the window. I have set up a message handler for the left and right arrow key press, which moves the triagle by 10. if the user resizes the the window, the window should display in proportion, by the statement. widthRatio = Rect.Width() / 756; then * the x posistion by the widthRatio I have tried declaring the variables 'float, double, and int I can not seem to find a reason why this shouldn't work the posistion remains the same if I expand, and snaps to the left hand of the screen if the size has gone smaller does anybody know whats going on cheers Simon
simon alec smith wrote:
I can not seem to find a reason why this shouldn't work
What doesn't work? What value does
widthRatio
contain? Since both the numerator and the denominator are integers, it will likely contain0
. Try dividing my756.0
instead."Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Hi, I am very confused to why this isn't working. My starting window width size is 756 I got this result by declaring: CRect Rect; GetClientRect(&Rect); Rect.Width(); I divide the 756 / 2 to plae the triangle in the centre of the window. I have set up a message handler for the left and right arrow key press, which moves the triagle by 10. if the user resizes the the window, the window should display in proportion, by the statement. widthRatio = Rect.Width() / 756; then * the x posistion by the widthRatio I have tried declaring the variables 'float, double, and int I can not seem to find a reason why this shouldn't work the posistion remains the same if I expand, and snaps to the left hand of the screen if the size has gone smaller does anybody know whats going on cheers Simon
Modified for the 2nd time... Looks like I missed the point with your question - dividing an integer with another integer, especially a larger one is going to give you BAD results. - Iain. Where are you doing these calculations?
class CMyView : public CView
{
...
int m_nHorzOffset;
...
};void CMyView::OnDraw(CDC* pDC)
{
CRect rc;
GetClientRect (&rc);
// Make a point in the middle
CPoint ptCentre = rc.CenterPoint ();ptCentre.x = rc.Width () / 4; // make it 1/4 the way across if we like...
// draw a vertical centre line
pDC->MoveTo (ptCentre.x, rc.top);
pDC->LineTo (ptCentre.x, rc.bottom);// draw a triangle, offset by m_nHorzOffset
pDC->MoveTo (ptCentre.x + m_nHorzOffset - 100, ptCentre.y + 50);
pDC->LineTo (ptCentre.x + m_nHorzOffset + 100, ptCentre.y + 50);
pDC->LineTo (ptCentre.x + m_nHorzOffset, ptCentre.y - 100);
pDC->LineTo (ptCentre.x + m_nHorzOffset - 100, ptCentre.y + 50);
}
...void CMyView::OnLeftABit ()
{
m_nHorzOffset -= 10;
Invalidate ();
}I'm assuming you have the left arrow mapped via an accelerator to (eg) IDC_LEFTABIT, and have a command handler called OnLeftABit in your view class. Hopefully that should work. Iain. Modified: Added centre line, some comments, and x=width/4 as an example.
modified on Thursday, September 18, 2008 12:07 PM
-
simon alec smith wrote:
I can not seem to find a reason why this shouldn't work
What doesn't work? What value does
widthRatio
contain? Since both the numerator and the denominator are integers, it will likely contain0
. Try dividing my756.0
instead."Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
Thank you very much, I have tried 756.0 and it works it would of taken me a hundred years to work that one out cheers
-
Hi, I am very confused to why this isn't working. My starting window width size is 756 I got this result by declaring: CRect Rect; GetClientRect(&Rect); Rect.Width(); I divide the 756 / 2 to plae the triangle in the centre of the window. I have set up a message handler for the left and right arrow key press, which moves the triagle by 10. if the user resizes the the window, the window should display in proportion, by the statement. widthRatio = Rect.Width() / 756; then * the x posistion by the widthRatio I have tried declaring the variables 'float, double, and int I can not seem to find a reason why this shouldn't work the posistion remains the same if I expand, and snaps to the left hand of the screen if the size has gone smaller does anybody know whats going on cheers Simon
In addition to DavidCrow's reply...
simon alec smith wrote:
widthRatio = Rect.Width() / 756; then * the x posistion by the widthRatio
When working with integers like this, you can often rearrange the calculation to avoid getting the 0. Do the multiply first, then the divide. This way you can avoid floating point types which can give better performance in certain situations. Of course, if you need the ratio variable in other places, then you'll need to make sure you do a floating point calculation as DavidCrow showed. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: