C# GDI+繪圖(四)實現網格繪製,並填充相應的表格內容

這是爲了實現在項目中的功能實現的一個Demo,在完成這個Demo後,我將其直接移植到了項目中,進行數據合併後,一切完美運行,

 

廢話不多說,直接上圖:

這裏給出部分主要代碼,

構造函數:

public uc_layout()
        {
            InitializeComponent();

            InitializeDrawResources();
            StartRow = startRow;
            DrawAll();

            this.SizeChanged += (s, w) =>
            {
                DrawAll();
            };
            this.MouseDown += (s, e) =>
                {
                    lastRow = e.Y / BlockHeight - 1+startRow;//確定行的位置
                    lastCol = e.X / BlockWidth - 1;
                    if (lastCol >= 0 && lastCol <= 7&&lastRow>=0&&lastRow<=63)
                    {
                        int index = 8 * (lastRow) + lastCol;//確定信號的startbit
                        if (list.Contains(index))//此處判斷是否按下了信號
                        {
                            startBit = list.Min();//若是,則獲取按下的信號
                            MouseIsDown = true;
                            DrawAll();
                        }
                    }
                };
            this.MouseUp += (s, e) =>
                {
                    MouseIsDown = false;
                    this.Cursor = Cursors.Default;
                };
            this.MouseMove += uc_layout_MouseMove;
        }

鼠標移動時,數據重繪,用MouseMove事件來控制,如下:

void uc_layout_MouseMove(object sender, MouseEventArgs e)
        {
            int mouseRow = e.Y / BlockHeight - 1+startRow;//確定鼠標移動到的行
            int mouseCol = e.X / BlockWidth - 1;
            if (MouseIsDown)
            {
                if (mouseRow!=lastRow||mouseCol!=lastCol)
                {
                    //lastRow = 7;
                    //lastCol = 6;
                    int difRow = mouseRow - lastRow;
                    int difCol = mouseCol - lastCol;
                    //int oldFirst = list.Min();
                    //int newFirst = oldFirst + 8 * difRow + difCol;
                    int newFirst = startBit + 8 * difRow + difCol;

                    lastRow = mouseRow;
                    lastCol = mouseCol;

                    if (this.Cursor!=Cursors.SizeAll)
                    {
                        this.Cursor = Cursors.SizeAll;
                    }
                    startBit = newFirst;
                    //if (startBit + list.Count > (startRow + 8) * 8 - 1)                    if (startBit > (startRow + 8) * 8 - 1)                    {
                        if (startRow<maxRow-8)
                        {
                            startRow++;
                        }
                    }
                    else if (startBit<startRow*8)
                    {
                        if (startRow>0)
                        {
                            startRow--;
                        }
                    }
                    list.Clear();
                    for (int i = 0; i < 6; i++)
                    {
                        list.Add(startBit + i);
                    }
                    DrawAll();
                }
            }
        }

以及畫筆初始化代碼

GrayBrush_G = new SolidBrush(Color.FromArgb(200, 200, 200));
            OrangeBrush_G = new SolidBrush(Color.FromArgb(190, 165, 210));
            DeepGrayBrush_G = new SolidBrush(Color.FromArgb(50, 50, 50));
            BlackBrush_G = new SolidBrush(Color.Black);
            BigFont_G = new Font("宋體", 12, FontStyle.Bold);
            SmallFont_G = new Font("宋體", 10);
            OverLayFont_G = new Font("宋體", 10, FontStyle.Bold);
            GrayPen_G = new Pen(Color.FromArgb(200, 200, 200), 1);
            BlackPen_G = new Pen(Color.FromArgb(0, 0, 0), 1);
            CenterSF_G = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center, FormatFlags = StringFormatFlags.NoWrap, Trimming = StringTrimming.Character };

想要進階學習的,可以在這裏C# GDI+實現網格繪製,並顯示內容 進行下載,項目編譯通過,vs2012編寫,下載有問題的,可以聯繫我。

這裏給出得是升級版,通過滾動條來控制顯示的行數,使繪製的表格動態刷新,可下載得代碼中包含前三篇中的代碼案例。

關注下方公衆號,回覆GDI+網格繪製 或 截圖 即可免費獲取源代碼


--------------------------------------

公衆號:攻城獅客棧

CSDN:畫雞蛋的不止達芬奇

 

更多精彩內容,請微信搜索攻城獅客棧 或掃描下方二維碼

                                                                 

讓我們一起變的更優秀。


 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章