Delphi关于StringGrid的公用模块[转]
生活随笔
收集整理的這篇文章主要介紹了
Delphi关于StringGrid的公用模块[转]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Posted on 2011-04-02 11:36 xun*2 閱讀(353) 評論(0) 編輯 收藏 ?
| unit SGridFunction;??? {*******************************************?? 模塊名稱:StringGrid操作函數模塊?? 編寫者:Tony?? 開始日期:2006年12月05日?? 版本號:v1.0.7?? ********************************************}??? interface??? uses??? ??Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,SConnect,??? ??Dialogs, Global, Grids, DBGrids, DB, DBClient, WinSkinData, StdCtrls,??? ??Buttons, ExtCtrls, ComCtrls, ComObj, Clipbrd, ADODB;??? ??//成功提示框??? ??//AMsg: 成功提示信息??? ??procedure SuccessMsgbox(AMsg: String);??? ??//錯誤提示框??? ??//AMsg: 錯誤提示信息??? ??procedure ErrorMsgbox(AMsg: String);??? ??//詢問提示框??? ??//strMsg:詢問信息??? ??function AskMsgbox(AMsg: String): Boolean;??? ??//信息提示框??? ??//AMsg: 提示信息??? ??procedure InfoMsgbox(AMsg: String);??? ??//導出數據到Excel??? ??//ASGrid:需要導出的StringGrid??? ??//ExcelModalPath: 報表模版全路徑,為空則新建工作簿??? ??//ExcelFileName: Excel文件的默認文件名??? ??//AGridStartCol, AGridStartRow: StringGrid的起始列和行,從0開始??? ??//AExcelStartCol, AExcelStartRow: Excel的起始列和行,從1開始??? ??procedure ExportToExcel(ASGrid: TStringGrid; ExcelModalPath, ExcelFileName: String;??? ????AGridStartCol, AGridStartRow, AExcelStartCol, AExcelStartRow: Integer);??? ??//取得某一列數據的最大長度??? ??//ASgrid: 目標StringGrid??? ??//ACol: 目標列,從0開始??? ??//AStartRow: 起始行,從0開始??? ??function GetColMaxDataLength(ASGrid: TStringGrid; ACol, AStartRow: Integer): Integer;??? ??//根據數據長度自動設置指定列的列寬??? ??//ASgrid: 目標StringGrid??? ??//ACol: 目標列,從0開始??? ??procedure SetOneColWidth(ASGrid: TStringGrid; ACol: Integer);??? ??//根據數據長度自動設置全部列的列寬??? ??//ASgrid: 目標StringGrid??? ??procedure SetAllColWidth(ASGrid: TStringGrid);??? ??//顯示ClientDataSet中的數據??? ??//ASgrid: 目標StringGrid??? ??//ACDSet: 包含數據的ClientDataSet??? ??//AGridStartCol, AGridStartRow: StringGrid的起始列和行,從0開始??? ??procedure ShowClientDataSetData(ASGrid: TStringGrid; ACDSet: TClientDataSet;??? ????AGridStartCol, AGridStartRow: Integer);??? ??//顯示ADOQuery中的數據??? ??//ASgrid: 目標StringGrid??? ??//AQuery: 包含數據的ADOQuery??? ??//AGridStartCol, AGridStartRow: StringGrid的起始列和行,從0開始??? ??procedure ShowQueryData(ASGrid: TStringGrid; AQuery: TADOQuery;??? ????AGridStartCol, AGridStartRow: Integer);??? ??//判斷是否含有數據??? ??//true: 包含數據??? ??//false: 不包含數據??? ??//ASgrid: 目標StringGrid??? ??//AStartCol, AStartRow: 起始列和行,從0開始??? ??function HaveData(ASGrid: TStringGrid; AStartCol, AStartRow: Integer): Boolean;??? ??//取得9位以內整數位數??? ??//>=1: 該整型數的長度??? ??//0: 空值??? ??//-1: 長度超過9位??? ??//ANumber: 需要判斷位數的整型數??? ??function GetIntegerNumberLength(ANumber: Integer): Integer;??? ??//為指定的序號列賦值??? ??//序號編號從1開始??? ??//ASGrid: 目標StringGrid??? ??//ACol: 目標列,從0開始??? ??//AStartRow: 起始行,從0開始??? ??procedure SetNumberFields(ASGrid: TStringGrid; ACol, AStartRow: Integer);??? ??//設置指定的列的對齊方式為右對齊??? ??//實現方式為在數據左邊補空格,所以取數據時應注意去掉空格??? ??//ASGrid: 目標StringGrid??? ??//ACol: 目標列,從0開始??? ??//AStartRow: 起始行,從0開始??? ??procedure SetColAlignRight(ASGrid: TStringGrid; ACol, AStartRow: Integer);??? ??//設置指定行的左邊距??? ??//ASGrid: 目標StringGrid??? ??//ARow: 目標行,從0開始??? ??procedure SetRowLeftSpace(ASGrid: TStringGrid; ARow, SpaceLength: Integer);??? ??//設置指定行的最小右邊距??? ??//ASGrid: 目標StringGrid??? ??//ARow: 目標行,從0開始??? ??procedure SetRowMinRightSpace(ASGrid: TStringGrid; ARow, SpaceLength: Integer);??? ??//設置指定行的最小邊距??? ??//ASGrid: 目標StringGrid??? ??//ARow: 目標行,從0開始??? ??procedure SetRowMinSpaceWidth(ASGrid: TStringGrid; ARow, SpaceLength: Integer);??? ??//獲得當前X坐標所在的列??? ??//ASGrid: 目標StringGrid??? ??//AX: 坐標的X值??? ??function GetColByCX(ASGrid: TStringGrid; AX: Integer): Integer;???? ??//獲得當前Y坐標所在的行??? ??//ASGrid: 目標StringGrid??? ??//AY: 坐標的Y值??? ??function GetRowByCY(ASGrid: TStringGrid; AY: Integer): Integer;??? ??//獲得當前坐標所處的單元格的行列值??? ??//ASGrid: 目標StringGrid??? ??//AX, AY: 坐標的X,Y值??? ??//ACol, ARow: 單元格的列,行索引,從0開始??? ??procedure GetCellByCoordinate(ASGrid: TStringGrid; AX, AY: Integer;??? ????out ACol, ARow: Integer);??? ??//填充空的單元格為指定值??? ??//ASGrid: 目標StringGrid??? ??//AStartCol, AStartRow: 開始列和行,從0開始??? ??//AEndCol, AEndRow: 結束列和行??? ??//AValue: 填充值??? ??procedure SetSpaceCells(ASGrid: TStringGrid; AStartCol, AStartRow,??? ????AEndCol, AEndRow: Integer; AValue:String);??? implementation??? //----------------------------------------------------------------------------//??? //成功提示框??? //----------------------------------------------------------------------------//??? procedure SuccessMsgbox(AMsg: String);??? begin??? ??Application.MessageBox(Pchar(AMsg), '完成', MB_ICONINFORMATION + MB_OK);??? end;??? //----------------------------------------------------------------------------//??? //錯誤提示框??? //----------------------------------------------------------------------------//??? procedure ErrorMsgbox(AMsg: String);??? begin??? ??Application.MessageBox(Pchar(AMsg), '錯誤', MB_ICONSTOP + MB_OK);??? end;??? //----------------------------------------------------------------------------//??? //詢問提示框??? //----------------------------------------------------------------------------//??? function AskMsgbox(AMsg: String): Boolean;??? begin??? ??if Application.MessageBox(Pchar(AMsg), '確認',??? ????MB_ICONQUESTION + MB_YESNO) = IDYES then??? ??begin??? ????result := true;??? ??end??? ??else begin??? ????result := false;??? ??end;??? end;??? //----------------------------------------------------------------------------//??? //消息提示框??? //----------------------------------------------------------------------------//??? procedure InfoMsgbox(AMsg: String);??? begin??? ??Application.MessageBox(Pchar(AMsg), '提示', MB_ICONINFORMATION + MB_OK);??? end;??? //----------------------------------------------------------------------------//??? //導出數據到Excel??? //----------------------------------------------------------------------------//??? procedure ExportToExcel(ASGrid: TStringGrid; ExcelModalPath, ExcelFileName: String;??? ????AGridStartCol, AGridStartRow, AExcelStartCol, AExcelStartRow: Integer);??? var?????? ??ExcelApp: Variant;??? ??ColIndex, RowIndex: Integer;??? ??OneRowData: String; //單行數據??? ??DataList: TStringList; //所有數據??? ??SaveDlg: TSaveDialog; //保存對話框??? ??SaveExcelFilePath: String; //Excel文件的保存路徑??? begin??? ??try??? ????//沒有數據時直接退出??? ????if not HaveData(ASGrid, AGridStartCol, AGridStartRow) then??? ????begin??? ??????InfoMsgBox('沒有數據需要導出。');??? ??????exit;??? ????end;??? ????//選擇保存路徑??? ????try??? ??????SaveDlg := TSaveDialog.Create(ASGrid); //創建保存窗口對象??? ??????SaveDlg.InitialDir := ExtractFilePath(Application.ExeName); //文件保存在當前目錄??? ??????SaveDlg.Filter := 'Excel Files(*.xls)| *.xls'; //文件類型過濾??? ??????SaveDlg.FileName := ExcelFileName + VarToStr(date); //定義默認文件名??? ??????if SaveDlg.Execute then??? ??????begin??? ????????SaveExcelFilePath := SaveDlg.FileName; //保存文件路徑??? ??????end??? ??????else begin??? ????????exit; //放棄導出??? ??????end;??? ????finally??? ??????SaveDlg.Free; //釋放對象??? ????end;??? ????//創建Excel對象??? ????try??? ??????ExcelApp := CreateOleObject( 'Excel.Application' ); //創建新Excel對象??? ????except??? ??????ErrorMsgBox('請確認您的機器已經安裝 Microsoft Excel 。');??? ??????Exit;??? ????end;??? ????try??? ??????//打開Excel工作簿??? ??????try??? ????????//打開報表模版??? ????????if (excelModalPath <> null) and (excelModalPath <> '') then??? ????????begin??? ??????????ExcelApp.WorkBooks.Open(ExcelModalPath);??? ????????end??? ????????//添加新工作簿??? ????????else begin??? ??????????ExcelApp.WorkBooks.Add;??? ??????????//設置列寬??? ??????????for ColIndex := 0 to ASGrid.ColCount - AGridStartCol - 1 do??? ??????????begin??? ????????????//此處不能使用ASGrid.ColWidths[AGridStartCol + ColIndex];??? ????????????ExcelApp.ActiveSheet.Columns[AExcelStartCol + ColIndex].ColumnWidth???? ??????????????:= GetColMaxDataLength(ASGrid, AGridStartCol + ColIndex, AGridStartRow);??? ??????????end;??? ??????????//數字 NumberFormatLocal?? =?? "0.00_?? "??? ??????????//日期 NumberFormatLocal?? =?? "yyyy-m-d"??? ??????????//時間 NumberFormatLocal?? =?? "[$-F400]h:mm:ss?? AM/PM"??? ??????????//文本 NumberFormatLocal?? =?? "@"??? ??????????ExcelApp.Cells.NumberFormatLocal := '@'; //設置單元格為文本格式??? ????????end;??? ????????ExcelApp.WorkSheets[1].Activate; //設置第1個工作表為活動工作表??? ????????ExcelApp.Cells.Item[AExcelStartRow, AExcelStartCol].Select; //設定Excel起始單元格??? ??????except??? ????????ErrorMsgBox('無法打開報表模版:' + #13 + ExcelModalPath); //#13為回車換行??? ????????exit;??? ??????end;??? ??????try??? ????????//通過剪切板導出數據??? ????????try??? ??????????//初始化DataList??? ??????????DataList := TStringList.Create;??? ??????????DataList.Clear;??? ??????????//將數據保存到DataList??? ??????????with ASGrid do??? ??????????begin??? ????????????//行??? ????????????for RowIndex := AGridStartRow to RowCount - 1 do??? ????????????begin??? ??????????????OneRowData := '';??? ??????????????//列??? ??????????????for ColIndex := AGridStartCol to ColCount - 1 do??? ??????????????begin??? ????????????????OneRowData := OneRowData + Trim(Cells[ColIndex, RowIndex]) + #9; //#9為制表符??? ??????????????end;??? ??????????????DataList.Add(OneRowData); //將該行數據添加到DataList??? ????????????end;??? ??????????end;??? ??????????ClipBoard.AsText := DataList.Text; //將DataList中的數據拷貝到剪切板??? ??????????ExcelApp.ActiveSheet.Paste; //將剪切板中的數據拷貝到Excel??? ????????finally??? ??????????DataList.Free; //釋放DataList??? ??????????ClipBoard.Clear; //清空剪切板??? ????????end;?????? ????????//保存Excel文件??? ????????ExcelApp.ActiveWorkbook.SaveAs(SaveExcelFilePath); //另存為到指定目錄??? ????????SuccessMsgBox('成功將文件保存到:' + #13 + SaveExcelFilePath); //保存成功??? ??????finally??? ????????ExcelApp.DisplayAlerts := false; //不彈出保存提示對話框??? ????????ExcelApp.WorkBooks.Close; //關閉工作簿??? ??????end;??? ????finally??? ??????ExcelApp.Quit; //關閉Excel進程??? ??????ExcelApp:= Unassigned; //釋放ExcelApp??? ????end;??? ??except????? ????On e: Exception do??? ????begin??? ??????ErrorMsgbox(e.Message);??? ????end;??? ??end;????? end;??? //----------------------------------------------------------------------------//??? //取得某一列數據的最大長度??? //----------------------------------------------------------------------------//??? function GetColMaxDataLength(ASGrid: TStringGrid; ACol, AStartRow: Integer): Integer;??? var??? ??ColIndex, RowIndex: Integer;??? ??MaxColLength: Integer; //列數據的最大長度??? begin??? ??MaxColLength := 0;??? ??with ASGrid do??? ??begin??? ????//取得列數據的最大長度??? ????for RowIndex := AStartRow to RowCount - 1 do??? ????begin??? ??????if length(Cells[ACol, RowIndex]) > MaxColLength then??? ??????begin??? ????????MaxColLength:= length(Cells[ACol, RowIndex]);??? ??????end;??? ????end;??? ??end;??? ??result := MaxColLength;??? end;??? //----------------------------------------------------------------------------//??? //根據數據長度自動設置指定列的列寬??? //----------------------------------------------------------------------------//??? procedure SetOneColWidth(ASGrid: TStringGrid; ACol: Integer);??? var??? ??OneCharPixel: Integer; //一個字符所占的像素數??? ??RightSpaceWidth: Integer; //右邊距空隙??? begin??? ??RightSpaceWidth := 3; //設置為3達到和左邊距一致的效果??? ??OneCharPixel := 6; //6對應9號字[*此處最好寫成一個根據字號獲得像素值的函數*]??? ??ASGrid.ColWidths[ACol] := GetColMaxDataLength(ASGrid, ACol, 0) * OneCharPixel??? ??+ RightSpaceWidth;??? end;??? //----------------------------------------------------------------------------//??? //根據數據長度自動設置全部列的列寬??? //----------------------------------------------------------------------------//??? procedure SetAllColWidth(ASGrid: TStringGrid);??? var??? ??ColIndex: Integer; //需要設置的列??? begin??? ??for ColIndex := 0 to ASGrid.ColCount - 1 do??? ??begin??? ????SetOneColWidth(ASGrid, ColIndex);??? ??end;??? end;??? //----------------------------------------------------------------------------//??? //顯示ClientDataSet中的數據??? //----------------------------------------------------------------------------//??? procedure ShowClientDataSetData(ASGrid: TStringGrid; ACDSet: TClientDataSet;??? ????AGridStartCol, AGridStartRow: Integer);??? var??? ??ColIndex: Integer;??? ??RowIndex: Integer;??? begin??? ??try??? ????with ASGrid do??? ????begin??? ??????//沒有記錄時,清空StringGrid并返回??? ??????if ACDSet.RecordCount <= 0 then??? ??????begin??? ????????RowCount := 2;??? ????????for ColIndex := 0 to ColCount - 1 do??? ????????begin??? ??????????Cells[ColIndex, 1] := '';??? ????????end;??? ????????exit;??? ??????end;??? ??????RowCount := AGridStartRow + ACDSet.RecordCount; //StringGrid行數??? ??????ColCount := AGridStartCol + ACDSet.FieldCount; //StringGrid列數??? ??????RowIndex := AGridStartRow; //當前行為起始行??? ??????while not ACDSet.Eof do??? ??????begin??? ????????//顯示數據??? ????????for ColIndex := AGridStartCol to ColCount - 1 do??? ????????begin??? ??????????Cells[ColIndex, RowIndex]??? ????????????:= ACDSet.Fields.Fields[ColIndex - AGridStartCol].AsString??? ????????end;??? ????????//轉到下一行??? ????????RowIndex := RowIndex + 1;??? ????????ACDSet.Next;??? ??????end;??? ????end;??? ??except??? ????On e: Exception do??? ????begin??? ??????ErrorMsgBox(e.Message);??? ????end;??? ??end;??? end;??? //----------------------------------------------------------------------------//??? //顯示ADOQuery中的數據??? //----------------------------------------------------------------------------//??? procedure ShowQueryData(ASGrid: TStringGrid; AQuery: TADOQuery;??? ????AGridStartCol, AGridStartRow: Integer);??? var??? ??ColIndex: Integer;??? ??RowIndex: Integer;??? begin??? ??try??? ????with ASGrid do??? ????begin??? ??????//沒有記錄時,清空StringGrid并返回??? ??????if AQuery.RecordCount <= 0 then??? ??????begin??? ????????RowCount := 2;??? ????????for ColIndex := 0 to ColCount - 1 do??? ????????begin??? ??????????Cells[ColIndex, 1] := '';??? ????????end;??? ????????exit;??? ??????end;??? ??????RowCount := AGridStartRow + AQuery.RecordCount; //StringGrid行數??? ??????ColCount := AGridStartCol + AQuery.FieldCount; //StringGrid列數??? ??????RowIndex := AGridStartRow; //當前行為起始行???? ??????while not AQuery.Eof do??? ??????begin??? ????????//顯示數據??? ????????for ColIndex := AGridStartCol to ColCount - 1 do??? ????????begin??? ??????????Cells[ColIndex, RowIndex]??? ????????????:= AQuery.Fields.Fields[ColIndex - AGridStartCol].AsString??? ????????end;??? ????????//轉到下一行??? ????????RowIndex := RowIndex + 1;??? ????????AQuery.Next;??? ??????end;??? ????end;??? ??except??? ????On e: Exception do??? ????begin??? ??????ErrorMsgBox(e.Message);??? ????end;??? ??end;??? end;??? //----------------------------------------------------------------------------//??? //判斷是否含有數據??? //----------------------------------------------------------------------------//??? function HaveData(ASGrid: TStringGrid; AStartCol, AStartRow: Integer): Boolean;??? var??? ??ColIndex, RowIndex: Integer;??? begin??? ??with ASgrid do??? ??begin??? ????for ColIndex := AStartCol to ColCount - 1 do??? ????begin??? ??????for RowIndex := AStartRow to RowCount - 1 do??? ??????begin??? ????????//包含數據,返回true??? ????????if Cells[ColIndex, RowIndex] <> '' then??? ????????begin??? ??????????result := true;??? ??????????exit;??? ????????end;??? ??????end;??? ????end;??? ??end;??? ??//沒有數據,返回false??? ??result := false;??? end;??? //----------------------------------------------------------------------------//??? //取得9位以內整數位數??? //----------------------------------------------------------------------------//??? function GetIntegerNumberLength(ANumber: Integer): Integer;??? var??? ??IsNegativeNumber: Boolean; //參數的正負,負數為true??? ??LoopIndex: Integer; //循環變量??? ??ComporeNumber: Integer; //用于比較的數??? ??NumberLength: Integer; //返回值,長度大于10返回-1??? begin??? ??if ANumber = null then??? ??begin??? ????result := 0; //空值返回0??? ????exit;??? ??end;??? ??//判斷參數的正負??? ??if ANumber < 0 then??? ??begin??? ????ANumber := 0 - ANumber; //轉換成正數用于計算長度??? ????IsNegativeNumber := true; //是負數??? ??end??? ??else begin??? ????if ANumber = 0 then??? ????begin??? ??????result := 1; //是0,直接返回1??? ??????exit;??? ????end;??? ????IsNegativeNumber := false; //是正數??? ??end;??? ??//開始比較??? ??ComporeNumber:= 10;??? ??for LoopIndex:= 1 to 9 do??? ??begin??? ????//長度符合要求??? ????if (ComporeNumber div ANumber) > 0 then??? ????begin??? ??????//得到長度??? ??????if ComporeNumber = ANumber then NumberLength := LoopIndex + 1??? ??????else NumberLength := LoopIndex;??? ??????//如果是負數,則長度加1,即包含負號??? ??????if IsNegativeNumber then result:= NumberLength + 1??? ??????else result := NumberLength;??? ??????exit;??? ????end;??? ????//增大1位繼續比較??? ????ComporeNumber := ComporeNumber * 10;??? ????continue;??? ??end;??? ??result := -1; //長度大于9,返回-1??? end;??? //----------------------------------------------------------------------------//??? //為指定的序號列賦值??? //----------------------------------------------------------------------------//??? procedure SetNumberFields(ASGrid: TStringGrid; ACol, AStartRow: Integer);??? var??? ??RowIndex: Integer; //當前序號??? begin??? ??with ASGrid do??? ??begin??? ????for RowIndex := 1 to RowCount - AStartRow do??? ????begin??? ??????//添加序號??? ??????Cells[ACol, AStartRow + RowIndex - 1] := VarToStr(RowIndex);??? ????end;??? ??end;??? end;??? //----------------------------------------------------------------------------//??? //設置指定的列的對齊方式為右對齊??? //----------------------------------------------------------------------------//??? procedure SetColAlignRight(ASGrid: TStringGrid; ACol, AStartRow: Integer);??? var??? ??RowIndex: Integer;??? ??MaxDataLength: Integer; //該列最大的數據長度??? begin??? ??MaxDataLength := GetColMaxDataLength(ASGrid, ACol, 0); //取得該列最大的數據長度??? ??with ASGrid do??? ??begin??? ????for RowIndex := AStartRow to RowCount - 1 do??? ????begin??? ??????while length(Cells[ACol, RowIndex]) < MaxDataLength do??? ??????begin??? ????????Cells[ACol, RowIndex] := ' ' + Cells[ACol, RowIndex]; //在前面補空格??? ??????end;??? ????end;??? ??end;??? end;??? //----------------------------------------------------------------------------//??? //設置指定行的左邊距??? //----------------------------------------------------------------------------//??? procedure SetRowLeftSpace(ASGrid: TStringGrid; ARow, SpaceLength: Integer);??? var??? ??ColIndex, LoopIndex: Integer;??? begin??? ??with ASGrid do??? ??begin??? ????for ColIndex := 0 to ColCount - 1 do??? ????begin??? ??????Cells[ColIndex, ARow] := TrimLeft(Cells[ColIndex, ARow]); //去掉左邊空格??? ??????for LoopIndex := 1 to SpaceLength do??? ??????begin??? ????????Cells[ColIndex, ARow] := ' ' + Cells[ColIndex, ARow]; //在左邊補空格??? ??????end;??? ????end;??? ??end;??? end;??? //----------------------------------------------------------------------------//??? //設置指定行的最小右邊距??? //----------------------------------------------------------------------------//??? procedure SetRowMinRightSpace(ASGrid: TStringGrid; ARow, SpaceLength: Integer);??? var??? ??ColIndex, LoopIndex: Integer;??? begin??? ??with ASGrid do??? ??begin??? ????for ColIndex := 0 to ColCount - 1 do??? ????begin??? ??????Cells[ColIndex, ARow] := TrimRight(Cells[ColIndex, ARow]); //去掉右邊空格??? ??????for LoopIndex := 1 to SpaceLength do??? ??????begin??? ????????Cells[ColIndex, ARow] := Cells[ColIndex, ARow] + ' '; //在右邊補空格??? ??????end;??? ????end;??? ??end;??? end;??? //----------------------------------------------------------------------------//??? //設置指定行的最小邊距??? //----------------------------------------------------------------------------//??? procedure SetRowMinSpaceWidth(ASGrid: TStringGrid; ARow, SpaceLength: Integer);??? var??? ??ColIndex, LoopIndex: Integer;??? begin??? ??with ASGrid do??? ??begin??? ????for ColIndex := 0 to ColCount - 1 do??? ????begin??? ??????Cells[ColIndex, ARow] := Trim(Cells[ColIndex, ARow]); //去掉兩邊空格??? ??????for LoopIndex := 1 to SpaceLength do??? ??????begin??? ????????Cells[ColIndex, ARow] := ' ' + Cells[ColIndex, ARow] + ' '; //在兩邊補空格??? ??????end;??? ????end;??? ??end;??? end;??? //----------------------------------------------------------------------------//??? //獲得當前X坐標所在的列??? //----------------------------------------------------------------------------//??? function GetColByCX(ASGrid: TStringGrid; AX: Integer): Integer;??? var??? ??ColIndex: Integer;??? ??CurCellRect: TRect; //當前列的矩形區域??? begin??? ??with ASGrid do??? ??begin??? ????for ColIndex := 0 to ColCount - 2 do??? ????begin??? ??????CurCellRect := CellRect(ColIndex, 0);??? ??????//當前列被隱藏,繼續判斷下一列??? ??????if CurCellRect.Left = CurCellRect.Right then continue;??? ??????//X坐標在當前列的范圍內??? ??????if (AX >= CurCellRect.Left) and (AX < CurCellRect.Right) then??? ??????begin??? ????????result := ColIndex;??? ????????exit;??? ??????end;??? ????end;??? ????result := ColCount - 1; //返回最后一列的索引??? ??end;??? end;??? //----------------------------------------------------------------------------//??? //獲得當前Y坐標所在的行??? //----------------------------------------------------------------------------//??? function GetRowByCY(ASGrid: TStringGrid; AY: Integer): Integer;??? var??? ??RowIndex: Integer;??? ??CurCellRect: TRect; //當前行的矩形區域??? begin??? ??with ASGrid do??? ??begin??? ????for RowIndex := 0 to RowCount - 2 do??? ????begin??? ??????CurCellRect := CellRect(0, RowIndex);??? ??????//當前行被隱藏,繼續判斷下一行??? ??????if CurCellRect.Top = CurCellRect.Bottom then continue;??? ??????//Y坐標在當前行的范圍內??? ??????if (AY > CurCellRect.Top) and (AY < CurCellRect.Bottom) then??? ??????begin??? ????????result := RowIndex;??? ????????exit;???? ??????end;??? ????end;??? ????result := RowCount - 1; //返回最后一行的索引??? ??end;??? end;??? //----------------------------------------------------------------------------//??? //獲得當前坐標所處的單元格的行列值??? //----------------------------------------------------------------------------//??? procedure GetCellByCoordinate(ASGrid: TStringGrid; AX, AY: Integer;??? ????out ACol, ARow: Integer);??? begin??? ??ACol := GetColByCX(ASGrid, AX); //取得列索引??? ??ARow := GetRowByCY(ASGrid, AY); //取得行索引??? end;??? //----------------------------------------------------------------------------//??? //填充空的單元格為指定值??? //----------------------------------------------------------------------------//??? procedure SetSpaceCells(ASGrid: TStringGrid; AStartCol, AStartRow,??? ????AEndCol, AEndRow: Integer; AValue:String);??? var??? ??ColIndex, RowIndex: Integer;??? begin??? ??with ASGrid do??? ??begin??? ????for ColIndex := AStartCol to AEndCol do??? ????begin??? ??????for RowIndex := AStartRow to AEndRow do??? ??????begin??? ????????//單元格為空(不含空格)時填充??? ????????if Trim(Cells[ColIndex, RowIndex]) = '' then??? ????????begin??? ??????????Cells[ColIndex, RowIndex] := AValue;??? ????????end;??? ??????end;??? ????end;??? ??end;??? end;??? end. |
轉載于:https://www.cnblogs.com/zsdentist/articles/2283663.html
總結
以上是生活随笔為你收集整理的Delphi关于StringGrid的公用模块[转]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 狗窝里的小日子 ...
- 下一篇: 《Effective Java》 第一讲