码迷,mamicode.com
首页 > 其他好文 > 详细

GDI绘图1——自定义函数

时间:2015-08-03 18:44:29      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

1
定义函数时注意
void类型无返回值,可以对外界的变量进行操作
对于有返回值的变量尽量不要对外界的变量进行操作,因为后来可能会考虑多种情况(比如drawState = State.move;外界也在使用drawState,那么返回的结果是不确定的)
2
全局变量
尽量少定义全局变量,因为其他地方会不会使用到他

编写程序时要注意此处变量使用对其他位置的影响。

4 三个自定义函数

技术分享
     bool JudgingState(List<Shape> shapeList, int X, int Y)
        {
            var shape = JudgingShape(shapeList, X, Y);
            return shape == null ? false : true;
        }
        //功能函数,当鼠标在指定区域内,判断是哪个元素发生改变
        Shape JudgingShape(List<Shape> shapeList, int X, int Y)
        {
            Shape shapeItem=null;
            foreach (Shape item in shapeList)
            {
                if (item != null)
                {
                    if (item is Line)
                    {
                        double bianC = CountDistance(item.P1.X, item.P1.Y, item.P2.X, item.P2.Y);
                        double bianA = CountDistance(item.P1.X, item.P1.Y, X, Y);
                        double bianB = CountDistance(item.P2.X, item.P2.Y, X, Y);
                        if (bianA + bianB < bianC + rongChaZhi)
                        {
                            shapeItem = item;
                            break;
                        }
                    }
                    else if (item is MyRectangle)
                    {
                        double bianC1 = CountDistance(item.P1.X, item.P1.Y, item.P2.X, item.P1.Y);
                        double bianC2 = CountDistance(item.P2.X, item.P1.Y, item.P2.X, item.P2.Y);
                        double bianC3 = CountDistance(item.P2.X, item.P2.Y, item.P1.X, item.P2.Y);
                        double bianC4 = CountDistance(item.P1.X, item.P2.Y, item.P1.X, item.P1.Y);
                        double bianC1A = CountDistance(X, Y, item.P1.X, item.P1.Y);
                        double bianC1B = CountDistance(X, Y, item.P2.X, item.P1.Y);
                        double bianC2A = CountDistance(X, Y, item.P2.X, item.P1.Y);
                        double bianC2B = CountDistance(X, Y, item.P2.X, item.P2.Y);
                        double bianC3A = CountDistance(X, Y, item.P2.X, item.P2.Y);
                        double bianC3B = CountDistance(X, Y, item.P1.X, item.P2.Y);
                        double bianC4A = CountDistance(X, Y, item.P1.X, item.P2.Y);
                        double bianC4B = CountDistance(X, Y, item.P1.X, item.P1.Y);
                        if (bianC1A + bianC1B < bianC1 + rongChaZhi || bianC2A + bianC2B < bianC2 + rongChaZhi || bianC3A + bianC3B < bianC3 + rongChaZhi || bianC4A + bianC4B < bianC4 + rongChaZhi)
                        {
                            shapeItem = item;
                            break;
                        }
                    }
                    else if (item is Ellipse)
                    {

                    }
                    else
                    {
                        shapeItem = null;
                    }

                }
            }
            return shapeItem;
        }
        //计算距离公式
        private double CountDistance(double pAx, double pAy, double pBx, double pBy)
        {
            return Math.Sqrt(Math.Abs(pAx - pBx) * Math.Abs(pAx - pBx) + Math.Abs(pAy - pBy) * Math.Abs(pAy - pBy));
        }
View Code

 

GDI绘图程序:

在paint函数中只进行绘画

在mouseDown,mouseMove,mouseUp进行各种状态的判断

GDI绘图1——自定义函数

标签:

原文地址:http://www.cnblogs.com/lv-sally/p/4699994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!