| 发表于:2007-02-08 17:51:465楼 得分:0 |
msdn上的示例代码: // the following three methods will draw a rectangle and allow // the user to use the mouse to resize the rectangle. if the // rectangle intersects a control 's client rectangle, the // control 's color will change. bool isdrag = false; rectangle therectangle = new rectangle (new point(0, 0), new size(0, 0)); point startpoint; private void form1_mousedown(object sender, system.windows.forms.mouseeventargs e) { // set the isdrag variable to true and get the starting point // by using the pointtoscreen method to convert form // coordinates to screen coordinates. if (e.button==mousebuttons.left) { isdrag = true; } control control = (control) sender; // calculate the startpoint by using the pointtoscreen // method. startpoint = control.pointtoscreen(new point(e.x, e.y)); } private void form1_mousemove(object sender, system.windows.forms.mouseeventargs e) { // if the mouse is being dragged, // undraw and redraw the rectangle as the mouse moves. if (isdrag) // hide the previous rectangle by calling the // drawreversibleframe method with the same parameters. { controlpaint.drawreversibleframe(therectangle, this.backcolor, framestyle.dashed); // calculate the endpoint and dimensions for the new // rectangle, again using the pointtoscreen method. point endpoint = this.pointtoscreen(new point(e.x, e.y)); int width = endpoint.x-startpoint.x; int height = endpoint.y-startpoint.y; therectangle = new rectangle(startpoint.x, startpoint.y, width, height); // draw the new rectangle by calling drawreversibleframe // again. controlpaint.drawreversibleframe(therectangle, this.backcolor, framestyle.dashed); } } private void form1_mouseup(object sender, system.windows.forms.mouseeventargs e) { // if the mouseup event occurs, the user is not dragging. isdrag = false; // draw the rectangle to be evaluated. set a dashed frame style // using the framestyle enumeration. controlpaint.drawreversibleframe(therectangle, this.backcolor, framestyle.dashed); // find out which controls intersect the rectangle and // change their color. the method uses the rectangletoscreen // method to convert the control 's client coordinates // to screen coordinates. rectangle controlrectangle; for(int i = 0; i < controls.count; i++) { controlrectangle = controls[i].rectangletoscreen (controls[i].clientrectangle); if (controlrectangle.intersectswith(therectangle)) { controls[i].backcolor = color.burlywood; } } // reset the rectangle. therectangle = new rectangle(0, 0, 0, 0); } | | |
|