Sign in to follow this  
mrtie_dye

Moving rectangles

Recommended Posts

Let's say we have a rectangle whose top-left corner is at (40,20). The bottom right corner is at (80,50), making the rectangle 40 X 30. Now, someone comes and clicks and holds the mouse button at (50,40) and moves it to (40, 30). We want the rectangle to move with the mouse. So, in my mind, the math would go like this: Mouse Click x = 50 y = 40 Original top-left corner xcord = 40 ycord = 20 New mouse coordinates xx = 40 yy = 30 distancex = x - xcord distancey = y - ycord newxcord = xx - distancex newycord = yy - distancey Then, we set x and y to xx and yy for the next movement of the mouse. This is what my code looks like right now.
void CDialog::IsClicked(int x, int y)
{
	/*x and y are the cordinates of the mouse passed in when a left click is detected
	isClicked is a bool that is set to FALSE when the CDlgTitleBar class is initilized.
	xcord and ycord are the cordinates of the Top-Left corner of the recangle
	height and width are the.... yup, height and width of the rectangle.
	Sooo... If this rectangle has not been clicked on yet, but it is now....*/
	if((!CDlgTitleBar->isClicked) && (x < (CDlgTitleBar->xcord + CDlgTitleBar->width)) 
          && (x > CDlgTitleBar->xcord) && (y < (CDlgTitleBar->ycord + CDlgTitleBar->height)) 
          && (y > CDlgTitleBar->ycord))
	{
		CDlgTitleBar->isClicked = TRUE; //has been clicked 
		CDlgTitleBar->clickx = x;	//remember where it was clicked
		CDlgTitleBar->clicky = y;
	}
	
}


void CDialog::IsMoving(int x, int y)
{
	//x and y are the cordinates of the mouse passed in when WM_MOUSEMOVE is detected
	//if the title bar is clicked
	if(CDlgTitleBar->isClicked)
	{		
		/*bmoMain is the main drawing surface. It it is what is drawn to the display
		bmoSnapShot is a section of bmoMain captured before we draw the title bar.
		We took a snap shot when the CDlgTitleBar was initilized, so, we draw it
		to erase the old title bar.*/
		BitBlt(*bmoMain,CDlgTitleBar->xcord,CDlgTitleBar->ycord,CDlgTitleBar->width,
                  CDlgTitleBar->height,CDlgTitleBar->bmoSnapShot,0,0,SRCAND);

		BitBlt(*bmoMain,CDlgTitleBar->xcord,CDlgTitleBar->ycord,CDlgTitleBar->width,
                  CDlgTitleBar->height,CDlgTitleBar->bmoSnapShot,0,0,SRCPAINT);
		
		//Here is the math that is supposed to move the rectangle.
		int xx = CDlgTitleBar->clickx - CDlgTitleBar->xcord;
		int yy = CDlgTitleBar->clickx - CDlgTitleBar->ycord;
		
		CDlgTitleBar->xcord = x - xx;
		CDlgTitleBar->ycord = y - yy;
			
		//Now, we take a new snapshot
		BitBlt(bmoSnapShot,0,0,CDlgTitleBar->width,CDlgTitleBar->height,
                  *bmoMain,CDlgTitleBar->xcord,CDlgTitleBar->ycord,SRCAND);

		BitBlt(bmoSnapShot,0,0,CDlgTitleBar->width,CDlgTitleBar->height,
                  *bmoMain,CDlgTitleBar->xcord,CDlgTitleBar->ycord,SRCPAINT);
		
		//Then draw the title bar. Since we changed xcord and ycord above, this should
		//draw the rectagle at the new cordinates.
		CDlgTitleBar->Draw();
			
		InvalidateRect(hwnd,NULL,FALSE);
		
		//set clickx and clicky so we are ready for the next mouse movement
		CDlgTitleBar->clickx = x;
		CDlgTitleBar->clicky = y;
	}
}

void CTitleBar::Draw()
{
	BitMapObject bmoTemp; //temp drawing surface
	HDC hdc=GetDC(hwnd);	//get the HDC
	HBRUSH hbrush, hbrushOld;  //get some brush handles

	// Create a red brush.
	hbrush = CreateSolidBrush(RGB(0, 0, 255));

	// Select the new brush.
	hbrushOld = (HBRUSH)SelectObject(hdc, hbrush);
		
	//make a rectangle that is as wide as the main window (width) and 20 units tall
	RECT rcTemp;
	SetRect(&rcTemp,0,0,width,20);
	
	//create bmoDude
	bmoTemp.Create(hdc,width,20);
	//color all of bmoDude blue
	FillRect(bmoTemp,&rcTemp,(HBRUSH)(hbrush));
	ReleaseDC(hwnd,hdc);
		
	//draw bmoTemp to the main drawing surface
	BitBlt(*bmoMain,xcord,ycord,width,height,bmoTemp,0,0,SRCAND);
	BitBlt(*bmoMain,xcord,ycord,width,height,bmoTemp,0,0,SRCPAINT);
}










After I compile and run this code, I click and hold on the title bar, but when I move it, it goes flying off the screen. I am pretty sure my math is correct on this. Why does this thing go flying away like that?

Share this post


Link to post
Share on other sites
DOH! Just figured it out! A big hero cookie goes out to anyone who can spot the problem with the following two lines of code...

int xx = CDlgTitleBar->clickx - CDlgTitleBar->xcord;
int yy = CDlgTitleBar->clickx - CDlgTitleBar->ycord;

Share this post


Link to post
Share on other sites
Well, if you begin to give away cookies to those who aer able to spot this very simple error, I bet you'll be naked and moneyless before the end of the year :)

When you C-c/C-v, don't forget to change some x to y if you want your code to do what you want [wink]

Regards,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this