DatagridView Cell Paint Graphics DrawLine方法 [英] DatagridView Cell Paint Graphics DrawLine Methods

查看:66
本文介绍了DatagridView Cell Paint Graphics DrawLine方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是Form1代码

Hi,

This is Form1 Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Datagridview__Cell_Paint
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataGridView1.RowCount = 5;
        }
        

        private void addColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
             if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {
            if (e.Button == MouseButtons.Right)
                {
                DataGridViewRow clickedRow = (sender as DataGridView).Rows[e.RowIndex];
                if (!clickedRow.Selected)
                    dataGridView1.CurrentCell = clickedRow.Cells[e.ColumnIndex];
                }
            }
        }
    }
}



Form2 code Here


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Datagridview__Cell_Paint
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }        
        private void panel4_Paint(object sender, PaintEventArgs e)
        {
            Pen graphPen = new Pen(Color.White, 20);
            PointF pt1D = new PointF();
            PointF pt2D = new PointF();
            pt1D.X = 5;
            pt1D.Y = 20;
            pt2D.X = 175;
            pt2D.Y = 20;

            e.Graphics.DrawLine(graphPen, pt1D, pt2D);
            
        }

        private void panel4_Click(object sender, EventArgs e)
        {
            System.Drawing.Graphics graphicsObj;
            graphicsObj = panel2.CreateGraphics();
            Pen graphPen = new Pen(panel1.BackColor, 20);
            graphicsObj.DrawLine(graphPen, 5, 20, 175, 20);
           
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ColorDialog cd = new ColorDialog();
                if (cd.ShowDialog() == DialogResult.OK)
                {
                    this.panel1.BackColor = cd.Color;
                }
            }
        }
    }
}



如果我右键单击datagridview cell form2打开.i选择颜色使用右键单击panel1 eg.red color.then我点击panel2 panel1颜色显示panel2红色(panel2 iam使用油漆和点击事件图形Drawline方法



之后如果我点击添加到form1按钮点击它将显示panel2 DrawLine红色显示到form1 datagridview单元格。



我也附上了我的项目文件.plz ref并帮助我们。

http://www.mediafire.com/download/8bdn8asb82ht12r/Datagridview__Cell_Paint.rar



Rgds


if i right click on datagridview cell form2 is open.i selected color using right click on panel1 eg.red color.then i click on panel2 panel1 color is shown panel2 red color(panel2 iam using paint and click event graphics Drawline method

after this if i click Add to form1 button click it shold be shown panel2 DrawLine red color show to form1 datagridview cell.

also i attached my project file also.plz ref and help us.
http://www.mediafire.com/download/8bdn8asb82ht12r/Datagridview__Cell_Paint.rar

Rgds

推荐答案

你做错了。你创建一个 Graphics 的实例并使用它绘制,但没有任何支持这个无论出于什么原因,最近的失效将会消除它。



这是你应该使用的: http://msdn.microsoft。 com / en-us / library / system.windows.forms.datagridview.cellpainting%28v = vs.110%29.aspx [ ^ ]。



请注意,您不应该创建 Graphics 的实例,而是使用在事件参数中传递给处理程序的实例。



我在这里解释了一些背景:什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ]。



-SA
You are doing it wrong. You create an instance of Graphics and draw using it, but nothing supports this graphic to persistently show in the control. Nearest invalidation, by whatever reason, will wipe it out.

Here is what you should use: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellpainting%28v=vs.110%29.aspx[^].

Note that you should not create an instance of Graphics but use the one passed to your handler in event arguments.

I explained some background here: What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

—SA


这篇关于DatagridView Cell Paint Graphics DrawLine方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆