PDA

View Full Version : سوال: وسط چین کردن در string grid دلفی



alaveh
سه شنبه 21 تیر 1390, 13:33 عصر
سلام

چطور نوشته های توی سلولهای استرینگ گرید رو وسط چین کنم ؟

یه کد پیدا کردم ولی همه رنگ و فونت و .... رو عوض می کرد وقتی هم رنگ و فونتش رو غیر فعال می کردم همش رو به هم می ریخت نتونستم استفاده کنم .

ممنون میشم اگر کسی یک کد خیلی ساده فقط و فقط برای وسط چین کردن بهم بده . هیچ کار دیگه ای نکنه این کد فقط نوشته های خودم رو وسط چین کنه .

اصلا نمیدونم کد می خواد یا توی تنظیمات خودش باید دست ببرم .

nsco_nsco
سه شنبه 21 تیر 1390, 19:29 عصر
سلام تو سایت یه سرچ بکن فکر کنم پیدا کنی .:قلب:

nsco_nsco
سه شنبه 21 تیر 1390, 19:29 عصر
سلام
یک چیزی پیدا کردم بچه ها گذاشته بودن ادرسش اینه :


http://www.swissdelphicenter.com/en/showcode.php?id=714

nsco_nsco
سه شنبه 21 تیر 1390, 19:37 عصر
سلام یکی دیگه تو یکی سایت پیدا کردم :



type
TTextAttributes = record
Font: TFont;
BackColor: TColor;
end;
{..}

procedure SetTextColor(oRichEdit: TRichEdit; sText: string; rAttributes:
TTextAttributes);
var
iPos: Integer;
iLen: Integer;

Format: CHARFORMAT2;
begin
FillChar(Format, SizeOf(Format), 0);
Format.cbSize := SizeOf(Format);
Format.dwMask := CFM_BACKCOLOR;
Format.crBackColor := rAttributes.BackColor;

iPos := 0;
iLen := Length(oRichEdit.Lines.Text);
iPos := oRichEdit.FindText(sText, iPos, iLen, []);

while iPos <> -1 do
begin
oRichEdit.SelStart := iPos;
oRichEdit.SelLength := Length(sText);
oRichEdit.SelAttributes.Color := rAttributes.Font.Color;
oRichEdit.SelAttributes.Size := rAttributes.Font.Size;
oRichEdit.SelAttributes.Style := rAttributes.Font.Style;
oRichEdit.SelAttributes.Name := rAttributes.Font.Name;

oRichEdit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));

iPos := oRichEdit.FindText(sText, iPos + Length(sText), iLen, []);
end;
end;

Example:

var
rAttrib: TTextAttributes;
begin
rAttrib.Font := TFont.Create;
rAttrib.Font.Color := clWhite;
rAttrib.Font.Size := 16;
rAttrib.Font.Style := [fsBold];
rAttrib.BackColor := clRed;

SetTextColor(RichEdit1, 'Delphi', rAttrib);

//Change another word attributes.
rAttrib.Font.Color := clYellow;
rAttrib.Font.Size := 10;
rAttrib.Font.Style := [fsBold, fsItalic];
rAttrib.BackColor := clBlue;

SetTextColor(RichEdit1, 'Is greate', rAttrib);

rAttrib.Font.Free; //Now free the font.
end;

alaveh
چهارشنبه 22 تیر 1390, 00:01 صبح
اووووووووووووووووووههههه چه کد خفنی

یعنی همه این کدها فقط برای وسط چین کردنه ؟

خیلی لطف کردی عزیز . ممنون . چک می کنم خبر میدم .

************************************************

عزیز زحمت کشیدی ولی نتونستم استفاده کنم . خیلی قر و قاطی بود . متوجه نشدم چطور تغییرش بدم .

کسی هست یه کد ساده بهم بده ؟ چرا اینقدر سخته وسط چین کردن توی استرینگ گرید ؟ آخه هر کسی از این استفاده کنه که مجبوره وسط چین کنه دیگه وگرنه فرمش زشت میشه که .

SAASTN
چهارشنبه 22 تیر 1390, 01:59 صبح
اینو یه نگاه بکن:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
Text: string;
TextExtents: TSize;
begin
with TStringGrid(Sender) do
begin
Text := Cells[ACol, ARow];
TextExtents := Canvas.TextExtent(Text);
Canvas.TextRect(Rect,
Rect.Left + (Rect.Right - Rect.Left - TextExtents.cx) div 2,
Rect.Top + (Rect.Bottom - Rect.Top - TextExtents.cy) div 2,
Text);
end;
end;

alaveh
چهارشنبه 22 تیر 1390, 03:34 صبح
ممنون دوست عزیز

این کد رو اینجا (http://stackoverflow.com/questions/3540570/delphi-how-to-make-cells-texts-in-tstringgrid-center-aligned/6671671#6671671)دیدم

درست کار میکنه . وسط چین میشه . ولی متاسفانه نوشته قبلی رو پاک نمیکنه . یعنی تو هر سلول هم یک چپ چین هست هم یک وسط چین .


72385

alaveh
چهارشنبه 22 تیر 1390, 05:07 صبح
خوب

مشکلم حل شد

برای بقیه که این تاپیک رو می خونن :

اول توی properties خود stringgrid خصوصیت defaultDrowing رو false کنید . بعد کد زیر رو توی event قسمت ondrowcell بنویسید :




procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
S: String;
begin
S := StringGrid1.Cells[ACol, ARow];
StringGrid1.Canvas.FillRect(Rect);
SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
StringGrid1.Canvas.TextRect(Rect,
Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
end;

mohammad_kerman65
دوشنبه 01 آبان 1391, 11:04 صبح
باسلام
سوالم در مورد وسط چین کردن نیست ولی هرچی سرچ کردم تاپیک مورد نظرم پیدا نکردم.
من میخوام بعضی از خطهای string grid یا Advstring grid رو ضخیم کنم.مثلا خطهای ستونها ضخیم و خط های سطرها نازک یا خطهای مربوط به یک سلول ضخیم شوند.
آیا راه حلی هست برای اینکار؟؟
با تشکر

mohammad_kerman65
سه شنبه 02 آبان 1391, 07:22 صبح
قبلنا تو این سایت زودتر به جوابمون می رسیدیم...:ناراحت:

AmirSky
سه شنبه 02 آبان 1391, 07:53 صبح
انگار هر کار که با stringgrid بخوای انجام بدی باید از Canvas استفاده کنی :لبخندساده:
فقط وسط چین کردن نیست. حتی برای چند خط بودن هم باید از Canvas و StringGrid1DrawCell استفاده بشه

AmirSky
سه شنبه 02 آبان 1391, 08:18 صبح
این یه سری کد که من برای وسط چین کردن سطر اول بکار میبرم

// وسط چين نمودن سطر اول
if ARow = 0 then
begin
S := StringGrid1.Cells[ACol, ARow];
SavedAlign := SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
StringGrid1.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
SetTextAlign(StringGrid1.Canvas.Handle, SavedAlign);
end;