//设置位置 private void SetSelectionCardLocation() { /*以控件所在的窗体为参照对象定位选项卡位置 */ int x = this.Left; int y = this.Top + this.Height; System.Windows.Forms.Control ctrl = this.Parent; if (this.Parent == null) { return; } this.FindForm().AutoValidate = AutoValidate.Disable; if (!this.FindForm().Controls.Contains(this.cardDataGrid)) { this.FindForm().Controls.Add(cardDataGrid); } cardDataGrid.Width = ShowCardWidth < this.Width ? this.Width : ShowCardWidth; cardDataGrid.Height = ShowCardHeight < 200 ? 200 : ShowCardHeight; Point location = new Point(); location = this.Parent.PointToScreen(this.Location); location = this.FindForm().PointToClient(location); if (location.Y + cardDataGrid.Height < this.FindForm().Height - 40) { location.Y = location.Y + this.Height; } else { location.Y = location.Y - cardDataGrid.Height; if (location.Y < 0) { cardDataGrid.Height = cardDataGrid.Height + location.Y; location.Y = 0; } } cardDataGrid.Top = location.Y; Rectangle scrRect = Screen.GetBounds(this); if (location.X + cardDataGrid.Width < scrRect.Width) { cardDataGrid.Left = location.X; } else { cardDataGrid.Left = location.X + (scrRect.Width - (location.X + cardDataGrid.Width)); } cardDataGrid.BringToFront(); //if (ctrl is Form) //{ //} //else //{ // x = x + ctrl.Left; // y = y + ctrl.Top; // ctrl = ctrl.Parent; // while (true) // { // if (ctrl is System.Windows.Forms.Form) // { // break; // } // else // { // x = x + ctrl.Left; // y = y + ctrl.Top; // ctrl = ctrl.Parent; // } // } //} //int buttomLeft = containerForm.Height - y - this.Height;//选项卡当前位置的底部与窗体下边界的距离(留1个行高的距离) ////如果超出下边界 //if (cardDataGrid.Height > buttomLeft) //{ // //在上面显示选择卡 // int _y = y - this.Height - cardDataGrid.Height; // if (_y > 0) // y = _y - 0; //} //else //{ // y = y + 0; //} //int rightLeft = containerForm.Width - x; //选项卡当前位置的右部与窗体右边界的距离 //int tmpx = x;//暂存 ////如果超出右边界,选项卡的右部与窗体右边界对齐 //if (cardDataGrid.Width > rightLeft) //{ // x = x - cardDataGrid.Width + rightLeft - 10; // if (x < 0) // x = tmpx; // else // x = x - 0; //} //else //{ // x = x + 0; //} //Point pt = new Point(x, y); //cardDataGrid.Location = pt; //cardDataGrid.BringToFront(); }
/// <summary> /// 设置选择卡位置 /// </summary> private void SetSelectCardLocation(int ColumnIndex, CardDataGrid cardDataGrid) { int grdX = 0; int grdY = 0; System.Windows.Forms.Control pctrl = this.Parent; while (true) { if (pctrl is System.Windows.Forms.Form) break; else { grdX += pctrl.Left; grdY += pctrl.Top; pctrl = pctrl.Parent; if (pctrl is System.Windows.Forms.Form) { break; } } } //窗体 System.Windows.Forms.Form parentForm = (System.Windows.Forms.Form)pctrl; if (!parentForm.Controls.Contains(cardDataGrid)) { parentForm.Controls.Add(cardDataGrid); } //正常情况下的坐标 grdX = grdX + this.Left + (this.RowHeadersVisible == true ? this.RowHeadersWidth : 0); for (int i = 0; i < this.Columns.Count; i++) { if (this.Columns[i].Visible) { if (i == CurrentCell.ColumnIndex) break; else grdX = grdX + Columns[i].Width; } } //顶部Y坐标 grdY = grdY + this.Top + this.ColumnHeadersHeight; //计算相对可见行是第几行 int visiableRowIndex = this.CurrentRow.Index - this.FirstDisplayedCell.RowIndex + 1; grdY = grdY + this.RowTemplate.Height * visiableRowIndex; int buttomLeft = parentForm.Height - grdY - this.RowTemplate.Height;//选项卡当前位置的底部与窗体下边界的距离(留1个行高的距离) //如果超出下边界 if (cardDataGrid.Height > buttomLeft) { //在上面显示选择卡 grdY = grdY - this.RowTemplate.Height - cardDataGrid.Height; if (grdY < 0) grdY = 0; else grdY = grdY + 0; } else { grdY = grdY + 0; } int rightLeft = parentForm.Width - grdX; //选项卡当前位置的右部与窗体右边界的距离 int tmpx = grdX;//暂存 //如果超出右边界,选项卡的右部与窗体右边界对齐 if (cardDataGrid.Width > rightLeft) { grdX = grdX - cardDataGrid.Width + rightLeft - 10; if (grdX < 0) grdX = tmpx; else grdX = grdX - 0; } else { grdX = grdX + 0; } cardDataGrid.Location = new System.Drawing.Point(grdX, grdY); cardDataGrid.BringToFront(); }