StringGrid 的配色問題 |
缺席
|
P.D.
版主 發表:603 回覆:4038 積分:3874 註冊:2006-10-31 發送簡訊給我 |
請問各位,
StringGrid 有RowSelect 的功能, 但沒有 ColSelect 的功能, 所以我想做出如圖片中"藍色"的ColSelect 但我寫出來後, 選擇不同的 Col, 只有該Cell位置才會變色, 不會如圖整條變色, 可是我在StringGrid 的 DbClick設定 另外開一個新form, 只要我在Grid中Double Click開出我指定的新form, 再關閉該form, 回到原來StringGrid 畫面時, StringGrid 會整張表重繪, 就會出現下圖我要的東西, 請問, 要如何做才能在滑鼠點在不同Col時, 立即有這樣的效果 以下是我 onDrawCell 繪製的程式碼及圖片 [code delphi] procedure TForm_IOsend.stgSpecDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var xCol, xWidth: integer; xRow, xHeight: integer; begin with Sender as TStringGrid do begin // 目前所在位置 if (ARow=nRow) and (ACol=nCol) and (ARow>=3) and (ACol>=kCol) then begin Canvas.Brush.Color:= clNavy; Canvas.Font.color := clWhite; end // 拖曳 else if State=[gdSelected] then begin Canvas.Brush.Color:= $0080FFFF; Canvas.Font.Color:= clBlack; end // 結存CELL else if (ACol=kCol-1) and (ARow>=3) then begin Canvas.Brush.Color:= $00404080; // 暗紅色 Canvas.Font.Color:= clWhite; end // 進貨量CELL else if (ACol=kCol) and (ARow>=3) then begin Canvas.Brush.Color:= $00AFE7B9; // 淺錄色 Canvas.Font.Color:= clBlack; end // 門市配量小計CELL else if (ACol>=kCol) and (ARow=0) then begin Canvas.Brush.Color:= $00AFE7B9; // 淺錄色 Canvas.Font.Color:= clBlack; end else if State=[gdFixed] then begin Canvas.Brush.Color:= clBtnFace; Canvas.Font.Color:= clBlack; end // 雙色 else begin if ((ARow-FixedCols) mod 2) = 0 then begin Canvas.Brush.Color:= clWhite; Canvas.Font.color := clBlack; end else begin Canvas.Brush.Color:= $00DDF2E3; Canvas.Font.color := clBlack; end; end; if (ACol=nCol) and (ARow<>nRow) then begin Canvas.Brush.Color:= $00FFF8E6; Canvas.Font.color := clBlack; end; xRow:= Round(stgSpec.RowHeights[ARow]); xCol:= Round(stgSpec.ColWidths[ACol]); xHeight:= Abs(stgSpec.Font.Height); xWidth:= 9 * Length(Cells[ACol,ARow]) 2; xRow:= Round((xRow-xHeight)/2); // 數量置中(第5欄起) if (ACol>=kCol-2) then xCol:= Round((xCol-xWidth)/2) 1 else xCol:= kCol-2; Canvas.FillRect(Rect); //繪底色 Canvas.TextOut(Rect.Left xCol,Rect.Top xRow,Cells[ACol,ARow]); //output text end; end; [/code] 編輯記錄
P.D. 重新編輯於 2007-11-28 17:59:18, 註解 無‧
|
syntax
尊榮會員 發表:26 回覆:1139 積分:1258 註冊:2002-04-23 發送簡訊給我 |
不會如圖整條變色 <----???沒有看到整條藍色的圖,是不是貼錯圖了?
如果是希望「整條Col」變色 那我的看法是 1. 仍在 CustomDrawCell 做繪圖動作,但是使用通用的程式碼簡化一下,就不需要那麼多次 IF 2. 因為 Select 是針對 Row 做的,所以只有一個 ColCell 會有 Selected 狀態,不會所有的 ColCell 都有 Selected 的狀態,所以要自己處理 總和以上,我會這樣做 1. 加入一組 Array 大小就是 Col 的數量,用來指定每個 Col 的顏色,CellColor: array[A..B] of TColor 2. 在 DrawCell 內,簡單的填色即可,而顏色就使用至陣列中對應的數值,例如 .....xxx.Color := CellColor[x],這樣 DrawCell 的程式碼也比較簡單 3. 然後在 OnSelect 中,來處理顏色,當被選取後,更改被選的 Col 對應的陣列的顏色,這樣,就會是整條 Col 變色,DrawCell 內就不需要在去判斷誰有被選,誰沒有 更進一步 還可以讓每一 Col 都是不同的顏色 不過,要記得記錄原來的顏色,以便還原 這樣,你就不用去管何時重繪元件,反正會依據 ,CellColor 中你指定的顏色畫出 至於實際動手做,你應該比在下還強,我是出出一張嘴而以啦,不知是否有幫到什麼 ===================引 用 P.D. 文 章=================== 請問各位, StringGrid 有RowSelect 的功能, 但沒有 ColSelect 的功能, 所以我想做出如圖片中"藍色"的ColSelect 但我寫出來後, 選擇不同的 Col, 只有該Cell位置才會變色, 不會如圖整條變色, 可是我在StringGrid 的 DbClick設定 另外開一個新form, 只要我在Grid中Double Click開出我指定的新form, 再關閉該form, 回到原來StringGrid 畫面時, StringGrid 會整張表重繪, 就會出現下圖我要的東西, 請問, 要如何做才能在滑鼠點在不同Col時, 立即有這樣的效果 以下是我 onDrawCell 繪製的程式碼及圖片 [code delphi] procedure TForm_IOsend.stgSpecDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var xCol, xWidth: integer; xRow, xHeight: integer; begin with Sender as TStringGrid do begin // 目前所在位置 if (ARow=nRow) and (ACol=nCol) and (ARow>=3) and (ACol>=kCol) then begin Canvas.Brush.Color:= clNavy; Canvas.Font.color := clWhite; end // 拖曳 else if State=[gdSelected] then begin Canvas.Brush.Color:= $0080FFFF; Canvas.Font.Color:= clBlack; end // 結存CELL else if (ACol=kCol-1) and (ARow>=3) then begin Canvas.Brush.Color:= $00404080; // 暗紅色 Canvas.Font.Color:= clWhite; end // 進貨量CELL else if (ACol=kCol) and (ARow>=3) then begin Canvas.Brush.Color:= $00AFE7B9; // 淺錄色 Canvas.Font.Color:= clBlack; end // 門市配量小計CELL else if (ACol>=kCol) and (ARow=0) then begin Canvas.Brush.Color:= $00AFE7B9; // 淺錄色 Canvas.Font.Color:= clBlack; end else if State=[gdFixed] then begin Canvas.Brush.Color:= clBtnFace; Canvas.Font.Color:= clBlack; end // 雙色 else begin if ((ARow-FixedCols) mod 2) = 0 then begin Canvas.Brush.Color:= clWhite; Canvas.Font.color := clBlack; end else begin Canvas.Brush.Color:= $00DDF2E3; Canvas.Font.color := clBlack; end; end; if (ACol=nCol) and (ARow<>nRow) then begin Canvas.Brush.Color:= $00FFF8E6; Canvas.Font.color := clBlack; end; xRow:= Round(stgSpec.RowHeights[ARow]); xCol:= Round(stgSpec.ColWidths[ACol]); xHeight:= Abs(stgSpec.Font.Height); xWidth:= 9 * Length(Cells[ACol,ARow]) 2; xRow:= Round((xRow-xHeight)/2); // 數量置中(第5欄起) if (ACol>=kCol-2) then xCol:= Round((xCol-xWidth)/2) 1 else xCol:= kCol-2; Canvas.FillRect(Rect); //繪底色 Canvas.TextOut(Rect.Left xCol,Rect.Top xRow,Cells[ACol,ARow]); //output text end; end; [/code] |
P.D.
版主 發表:603 回覆:4038 積分:3874 註冊:2006-10-31 發送簡訊給我 |
感謝syntax兄指教
===================引 用 syntax 文 章=================== 不會如圖整條變色 <----???沒有看到整條藍色的圖,是不是貼錯圖了? >> 我看了一下, 圖沒貼錯, 我要的就是整條col 是藍色, 圖片中確實有整條"淡藍"色的哦, syntax兄摸螢幕不會太舊吧? 抱歉, 前篇手快少了一個"淡" 如果是希望「整條Col」變色 ....略 1. 加入一組 Array 大小就是 Col 的數量,用來指定每個 Col 的顏色,CellColor: array[A..B] of TColor >> 這個方法我曾有想過, 不過因為這張表是機動性, 可能10*10, 可能100*200, 而且是作業中隨時可以變動的, 我不敢使用Arry的原因怕資源吃的太兇, 所以才用if 方式來做, 另外機動性的關係, 加上還有雙色row處理, 要用陣列做隨時的異動反而程式要處理這段太過複雜, 因此還是希望使用if方式判斷 這樣,你就不用去管何時重繪元件,反正會依據 ,CellColor 中你指定的顏色畫出 ....略 至於實際動手做,你應該比在下還強,我是出出一張嘴而以啦,不知是否有幫到什麼 >>術業有專攻, 每人投入的領域不同, 所以不敢自稱"強或不強", 要向syntax兄請教的地方還是很多的啦, 到時希望兄台不要拒絶哦! |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |