WPF游戏摘记地图编辑器(2)

知识点1:描线

吧totalWidth*totalHeight的carrier分成singleWidth*singleHeight的lineColor颜色的矩形框,框为dashWidth宽,间隙dashSpace

/// <summary>

        /// 绘?制?网ª?格?边À?线?

        /// </summary>

        private void SetGridLines(Canvas carrier, double totalWidth, double totalHeight, double singleWidth, double singleHeight,Color lineColor, double dashWidth, double dashSpace) {

            //欲与之?,ê?必À?先¨¨舍¦¨¢之?

            carrier.Children.Clear();

            int sectionXNum = (int)(totalWidth / singleWidth);

            int sectionYNum = (int)(totalHeight / singleHeight);

            for (int x = 1; x <= sectionXNum; x++) {//竖º¨²线?

                Line line = new Line() {

                    X1 = x * singleWidth,

                    X2 = x * singleWidth,

                    Y1 = 0,

                    Y2 = totalHeight,

                    Stroke = new SolidColorBrush(lineColor),

                    StrokeDashArray = new DoubleCollection() { dashWidth, dashSpace },

                };

                carrier.Children.Add(line);

            }

            for (int y = 1; y <= sectionYNum; y++) {//横¨¢线?

                Line line = new Line() {

                    X1 = 0,

                    X2 = totalWidth,

                    Y1 = y * singleHeight,

                    Y2 = y * singleHeight,

                    Stroke = new SolidColorBrush(lineColor),

                    StrokeDashArray = new DoubleCollection() { dashWidth, dashSpace },

                };

                carrier.Children.Add(line);

            }

        }

知识点2:自定义双击事件

 public DateTime _lastClick = DateTime.Now; 
        private bool _firstClickDone = false;

        private void MainPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
        { 
            TimeSpan span = DateTime.Now - _lastClick;

            if (span.TotalMilliseconds < 300 && _firstClickDone == true) 
            { //两次点击事件不超过0.3s(当然你也可以自己尝试一般双击间隔时间)+前一次是单击(避免3击)

//双击触发 
                //双击代码请在这里编写

                _firstClickDone = false;
               
            } 
            else 
            { //第一次点击        

_firstClickDone = true; 
                _lastClick = DateTime.Now; 
            }  

所谓双击事件就是很快的两次单机,

双击=两次单击时间间隔少于300微秒+前一次是单击

知识点3:自定义长按左键事件

private void ObstructionViewer_PreviewMouseMove(object sender, MouseEventArgs e) {

            if (e.LeftButton == MouseButtonState.Pressed) {

               //code            }

        }

长按左键事件= e.LeftButton == MouseButtonState.Pressed

简单吧,看了感觉很简单啊,但是不知道就很难喽。

知识点4:大图分解为指定尺寸的小图

for (int x = 0; x < sectionXNum; x++) {

                    for (int y = 0; y < sectionYNum; y++) {

                        CroppedBitmap croppedBitmap = new CroppedBitmap(mapSource, new Int32Rect(x * (int)SectionWidth.Value, y * (int)SectionHeight.Value, (int)SectionWidth.Value, (int)SectionHeight.Value));

                        System.IO.FileStream fileStream = new System.IO.FileStream(string.Format(@"{0}/{1}_{2}.jpg", outputSection.SelectedPath, x, y), System.IO.FileMode.Create);

                        JpegBitmapEncoder encoder = new JpegBitmapEncoder();

                        encoder.Frames.Add(BitmapFrame.Create(croppedBitmap));

                        encoder.Save(fileStream);

                        fileStream.Close();

                    }

                }

  1. 从mapSource得到指定位置指定大小的BitmapSource源;
  2. 创建个事件流,流位置即保存地址设定好;
  3. Jpg转码转换;
  4. 保存

就不详细说了,自己看代码吧,就按这个流程来就行了。

原文地址:https://www.cnblogs.com/haichao/p/2599486.html