2008年10月29日 星期三

判斷該滑鼠位置屬於哪個 Row

private int GetRowIndexFromPoint( DataGridView Dgrid, Point pt )
{
//檢查 Left
DataGridViewColumn col = Dgrid.Columns.GetFirstColumn( DataGridViewElementStates.Displayed, DataGridViewElementStates.None );

if ( col == null )
{
return -1;
}

// Left
else if ( pt.X <> {
return -1;
}

//檢查 Right
col = Dgrid.Columns.GetLastColumn( DataGridViewElementStates.Displayed, DataGridViewElementStates.None );

if ( pt.X > Dgrid.GetColumnDisplayRectangle( col.Index, true ).Right )
{
return -1;
}

//檢查 Bottom
int rowIndex = Dgrid.Rows.GetLastRow( DataGridViewElementStates.Displayed );

if ( rowIndex <>
{
return -1;
}
if ( pt.Y > Dgrid.GetRowDisplayRectangle( rowIndex, true ).Bottom )
{
return -1;
}

//檢查 Top
int toFind = -1;
rowIndex = Dgrid.Rows.GetFirstRow( DataGridViewElementStates.Displayed );

if ( rowIndex <>
{
return -1;
}
Rectangle rect = Dgrid.GetRowDisplayRectangle( rowIndex, true );

if ( pt.Y <>
{
return -1;
}
if ( pt.Y <= rect.Bottom )
{
return rowIndex;
}

rowIndex = Dgrid.Rows.GetNextRow( rowIndex, DataGridViewElementStates.Displayed );

while ( rowIndex >= 0 )
{
rect = Dgrid.GetRowDisplayRectangle( rowIndex, true );

if ( rect.Top <= pt.Y && rect.Bottom >= pt.Y )
{
toFind = rowIndex;
rowIndex = -1;
}
else
{
rowIndex = Dgrid.Rows.GetNextRow( rowIndex, DataGridViewElementStates.Displayed );
}
}

return toFind;
}

沒有留言:

關於我自己

我的相片
從股票、期貨、到選擇權,一路以來的經驗與努力絕對不是一言可以道盡,發現選擇權的穩定獲利純粹是在一個偶然之下,這幾年感謝我的女友,一路支持我,我也用穩定的報酬來回報,面對詭譎多變的股市,我不再徬徨無措,我不斷調整我的步伐,調整我的投資方式,讓投資更穩、更好!

我的網誌清單