rivate
void
contextMenuStrip1_Opening(
object
sender, CancelEventArgs e)
{
// 如果点击无效区域,返回,不弹出菜单
Point gdvPoint = this .dataGridView1.PointToScreen( new Point( 0 , 0 ));
int totalWidth = 0 ;
foreach (DataGridViewColumn col in this .dataGridView1.Columns)
{
totalWidth += col.Width;
}
if (totalWidth > this .dataGridView1.Width - this .dataGridView1.RowHeadersWidth)
{
totalWidth = this .dataGridView1.Width - this .dataGridView1.RowHeadersWidth;
}
if (MousePosition.X < gdvPoint.X + this .dataGridView1.RowHeadersWidth || MousePosition.X > (gdvPoint.X + this .dataGridView1.RowHeadersWidth + totalWidth) ||
MousePosition.Y < gdvPoint.Y || MousePosition.Y > (gdvPoint.Y + this .dataGridView1.ColumnHeadersHeight))
{
e.Cancel = true ;
}
}假设dataGridView1的右键菜单是contextMenuStrip1
增加事件处理:}
Point gdvPoint = this.dataGridView1.PointToScreen(new Point(0, 0));
int clickColumnIndex = this.dataGridView1.HitTest(MousePosition.X - gdvPoint.X, MousePosition.Y - gdvPoint.Y).ColumnIndex;}
int[,] path ={
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
/*(A)*/{ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0},
{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},//(B)
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
List<string> arrayPath = new List<string>(30);
//A to B
for (int i = 1; i < path.GetLength(0) - 2; i++)
{
for (int j = 0; j < path.GetLength(1); j++)
{
if (path[i, j] == 1)
{
arrayPath.Add(i + "," + j);
}
}
}
foreach (string item in arrayPath)
{
Console.WriteLine(item);
}
arrayPath.Clear();
//B to A
for (int i = path.GetLength(0) - 2; i > 1; i--)
{
for (int j = path.GetLength(1) - 1; j >= 0; j--)
{
if (path[i, j] == 1)
{
arrayPath.Add(i + "," + j);
}
}
}
foreach (string item in arrayPath)
{
Console.WriteLine(item);
}
{
// 如果点击无效区域,返回,不弹出菜单
Point gdvPoint = this .dataGridView1.PointToScreen( new Point( 0 , 0 ));
int totalWidth = 0 ;
foreach (DataGridViewColumn col in this .dataGridView1.Columns)
{
totalWidth += col.Width;
}
if (totalWidth > this .dataGridView1.Width - this .dataGridView1.RowHeadersWidth)
{
totalWidth = this .dataGridView1.Width - this .dataGridView1.RowHeadersWidth;
}
if (MousePosition.X < gdvPoint.X + this .dataGridView1.RowHeadersWidth || MousePosition.X > (gdvPoint.X + this .dataGridView1.RowHeadersWidth + totalWidth) ||
MousePosition.Y < gdvPoint.Y || MousePosition.Y > (gdvPoint.Y + this .dataGridView1.ColumnHeadersHeight))
{
e.Cancel = true ;
}
}假设dataGridView1的右键菜单是contextMenuStrip1
增加事件处理:}
Point gdvPoint = this.dataGridView1.PointToScreen(new Point(0, 0));
int clickColumnIndex = this.dataGridView1.HitTest(MousePosition.X - gdvPoint.X, MousePosition.Y - gdvPoint.Y).ColumnIndex;}
int[,] path ={
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
/*(A)*/{ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0},
{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},//(B)
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
List<string> arrayPath = new List<string>(30);
//A to B
for (int i = 1; i < path.GetLength(0) - 2; i++)
{
for (int j = 0; j < path.GetLength(1); j++)
{
if (path[i, j] == 1)
{
arrayPath.Add(i + "," + j);
}
}
}
foreach (string item in arrayPath)
{
Console.WriteLine(item);
}
arrayPath.Clear();
//B to A
for (int i = path.GetLength(0) - 2; i > 1; i--)
{
for (int j = path.GetLength(1) - 1; j >= 0; j--)
{
if (path[i, j] == 1)
{
arrayPath.Add(i + "," + j);
}
}
}
foreach (string item in arrayPath)
{
Console.WriteLine(item);
}