技术开发 频道

补充画圆画线


【IT168技术文档】

  这里定义了线,矩形,椭圆等五种类型的外观,还有PEN,刷子,很简单的一个画图工具,希望能提起大家的兴趣
private void Form1_Load(object sender, System.EventArgs e) { // Get the full size of the form //取得窗口的完整大小 fullSize = SystemInformation .PrimaryMonitorMaximizedWindowSize; //其实也即时初始化一副BMP图 // Create a bitmap using full size bitmap = new Bitmap(fullSize.Width, fullSize.Height); // Create a Graphics object from Bitmap //并由于创建一份GRAPHICS curGraphics = Graphics.FromImage(bitmap); // Set background color as form's color curGraphics.Clear(this.BackColor); // Create a new pen and brush as // default pen and brush curPen = new Pen(Color.Black); curBrush = new SolidBrush(Color.Black); } 在DOWN的时候,即取得开始的位置 private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { // Store the starting point of // the rectangle and set the drag mode // to true curX = e.X; curY = e.Y; dragMode = true; } 每次移动,都是重新画,只是肉眼所不能分得清而已 private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { // Find out the ending point of // the rectangle and calculate the // difference of starting and ending // points to find out the height and width // of the rectangle x = e.X; y = e.Y; diffX = e.X - curX; diffY = e.Y - curY; // If drag mode is true, call refresh // that forces window to repaint if (dragMode) { //每次刷新其实都是调用ONPAINT函数 this.Refresh(); } } 在UP的时候,也画,并把标志量改为FALSE private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { diffX = x - curX; diffY = y - curY; //这里下一个留个问题?SWITCH是OO被人痛批的,可这里如果不采用SWITCH,有什么好的办法呢! switch (drawIndex) { //不管是矩形或者是椭圆形,都是取X,Y,差值就可以了 case 1: { // Draw a line curGraphics.DrawLine(curPen, curX, curY, x, y); break; } case 2: { // Draw an ellipse curGraphics.DrawEllipse(curPen, curX, curY, diffX, diffY); break; } case 3: { // Draw rectangle curGraphics.DrawRectangle(curPen, curX, curY, diffX, diffY); break; } case 4: { // Fill rectangle curGraphics.FillRectangle(curBrush, curX, curY, diffX, diffY); break; } case 5: { // Fill ellipse curGraphics.FillEllipse(curBrush, curX, curY, diffX, diffY); break; } } // Refresh RefreshFormBackground(); // Set drag mode to false dragMode = false; }
0
相关文章