WPF遊戲摘記–地圖編輯器(2)

來源:互聯網
上載者:User
知識點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. 儲存

就不詳細說了,自己看代碼吧,就按這個流程來就行了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.