zoukankan      html  css  js  c++  java
  • GDI绘图1——自定义函数

    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进行各种状态的判断

  • 相关阅读:
    常用学习网站 (转载)
    Cookie 与 Session 的区别(转载)
    Asp.net 控件ClientID,UniqueID命名规则问题
    HTTP POST GET 本质区别详解
    asp net中的状态管理的方式有哪几种分别有哪些优势和缺点
    http 协议 (转载)
    Java 学习 (转载)
    数据库日常维护常用的脚本部分收录(转载)
    ASP.NET页面与IIS底层交互和工作原理详解(转载)
    Web网站的性能测试工具(转载)
  • 原文地址:https://www.cnblogs.com/lv-sally/p/4699994.html
Copyright © 2011-2022 走看看