Autor Beitrag
Ah-El-Eh-Iks
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 28

Win 3.11, Win ME, Win XP
Delphi 3 Prof., Delphi 2005 Pers.
BeitragVerfasst: Di 16.10.07 00:08 
So, nachdem ich hier verzweifelt gesucht habe und lauter Themen gefunden habe, in denen auf die Suchfunktion verwiesen wurde, musste ich mich selbst bemühen :lol:

Na ja, vielleicht kein falscher Ansatz :wink:

Es geht darum, Text in einem TStringGrid zentreirt darzustellen. Meine Lösung ist zwar recht umfangreich (also es werden viele Berechnungen durchgeführt), vielleicht geht es ja auch schneller, aber es funktioniert schonmal. Für Strg+C und Strg+V richt es allemal^^

Also:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
begin
 if (Col = 0) or (Row = 0) then
  begin
  StringGrid1.Canvas.Brush.Color := $0080FFFF;
  StringGrid1.Canvas.FillRect(Rect);
  StringGrid1.Canvas.TextOut((Rect.Left + (Rect.Right-Rect.Left) div 2)-(Canvas.TextWidth(StringGrid1.Cells[Col,Row]) div 2),Rect.Top+2,StringGrid1.Cells[Col,Row]);
  end;
end;


Nein sowas, was macht denn die Farbe da? :lol: Zu Vollständigkeit halber: Das gilt NUR für alle Rubrikenbeschriftungen (erste Zeile und erste Spalte), kann man aber auch für alle machen, indem man das if weglässt... Die Zelle wird erst schön gelb gemacht und dann der Textmittelpunkt ausgerechnet und der Text platziert.

Bin gerne für eine Diskussion offen 8)
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Di 16.10.07 01:05 
user profile iconAh-El-Eh-Iks hat folgendes geschrieben:

Bin gerne für eine Diskussion offen 8)


Von ES_CENTER usw. noch nichts gehört ? Die Farben sind schön und gut, aber warum soll da soviel doppelt rumgerechnet werden ?

_________________
Gruß
Hansa
Ah-El-Eh-Iks Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 28

Win 3.11, Win ME, Win XP
Delphi 3 Prof., Delphi 2005 Pers.
BeitragVerfasst: Di 16.10.07 01:07 
user profile iconhansa hat folgendes geschrieben:
Von ES_CENTER usw. noch nichts gehört ?

:nixweiss: Ähmm... nein...
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Di 16.10.07 02:30 
Die Steilvorlage ist jedenfalls gegeben. -> F1 Oder im WinSDK : da findet sich sicherlich was. -> suchen. Da es sich nur um eine Konstante handelt, ist selbst die komische WinApi relativ egal. 8)

_________________
Gruß
Hansa
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 16.10.07 07:51 
user profile iconAh-El-Eh-Iks hat folgendes geschrieben:
(Rect.Left + (Rect.Right-Rect.Left) div 2)
Wie wäre es mit:
((Rect.Left + Rect.Right) div 2) ? ;-)
Ein wenig Bruchrechnung 8) :
--> (Rect.Left + (Rect.Right-Rect.Left) div 2) = ((2 * Rect.Left) div 2 + (Rect.Right-Rect.Left) div 2)
= ((2 * Rect.Left + Rect.Right-Rect.Left) div 2) = ((Rect.Left + Rect.Right) div 2)
Ah-El-Eh-Iks Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 28

Win 3.11, Win ME, Win XP
Delphi 3 Prof., Delphi 2005 Pers.
BeitragVerfasst: Di 16.10.07 08:48 
@jaenicke: Ja, ich habs übernommen :) (s.u)

@hansa: Da kann ich leider nichts finden, in der Hilfe steht gar nichts zu ES_CENTER und im Forum finde ich nur was über Edits.

Hehe, mir ist da noch was aufgefallen: So toll die Prozedur auch funktioniert, tut sie das im Moment bei mir nur mit "MS Sans Serif", sobald ich auf "Courier New" umstelle, geht das nicht mehr so gut...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
begin
 if (Col = 0) or (Row = 0) then
  begin
  StringGrid1.Canvas.Font := StringGrid1.Font;
  StringGrid1.Canvas.Brush.Color := $0080FFFF;
  StringGrid1.Canvas.FillRect(Rect);
  StringGrid1.Canvas.TextOut((Rect.Left + (Rect.Right-Rect.Left) div 2)-(Canvas.TextWidth(StringGrid1.Cells[Col,Row]) div 2),Rect.Top+2,StringGrid1.Cells[Col,Row]);
  end;
end;
Einloggen, um Attachments anzusehen!
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Di 16.10.07 09:12 
Könnte es vielleicht, möglicherweise damit zusammenhängen, dass es sich einmal um Proportionalschrift handelt und im anderen Fall um Schrift mit fester Zeichenbreite?
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Di 16.10.07 10:44 
Hallo!

Erstmal erzeugst du über das direkte zuweisen von Fonts Speicherlöcher. Nimm dafür lieber Assign.
Und wenn du dir die Textbreite vom Canvas des Formulars sagen lässt (wo wahrscheinlich nicht Courier eingestellt ist) geht das natürlich in die Hose.
Und schlussendlich, wenn die Methode allgemein verwendbar sein sol, solltest du Stringgrid1 durch Sender (auf TStringgrid gecastet) ersetzen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
var
  MySender:TStringGrid;
begin
 if (Col = 0) or (Row = 0) then
 begin
  MySender:=(Sender as TStringgrid);
  MySender.Canvas.Font.Assign(MySender.Font);
  MySender.Canvas.Brush.Color := $0080FFFF;
  MySender.Canvas.FillRect(Rect);
  MySender.Canvas.TextOut((Rect.Left + (Rect.Right-Rect.Left) div 2)-(MySender.Canvas.TextWidth(MySender.Cells[Col,Row]) div 2),Rect.Top+2,MySender.Cells[Col,Row]);
  end;
end;


PS: Getestet 8)

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
Ah-El-Eh-Iks Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 28

Win 3.11, Win ME, Win XP
Delphi 3 Prof., Delphi 2005 Pers.
BeitragVerfasst: Di 16.10.07 12:25 
Funktioniert^^ :zustimm:

Achso, OK, ich hab nicht bedacht, dass das dann das Canvas von dem Formular ist. Da war natürlich noch die nicht-proportionale Schrift eingestellt. Hehe, OK...

Aber kannst du mir eins noch erklären: Wieso entstehen dadurch Speicherlöcher? Ich dachte, wenn man ein record übergibt (und TFont ist doch eins, oder?), dann werden alle Eigenschaften nacheinander der anderen zugewiesen (TFont.Size, TFont.Name, etc.) Wo geht denn dann was verloren? Oder denke ich falsch?
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Di 16.10.07 12:38 
Mann, Mann, hier muss man immer alles bis ins Kleinste erklären. 8) Weil es allerdings nicht ES_CENTER ist, sondern DT_CENTER (obwohl es auch gehen müsste) hier zum abkupfern :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure sgZentriert (sg: TStringGrid; Rect: TRect; ACol, ARow:Integer);
var st: String;
begin
  sg.Canvas.FillRect(Rect);
  st := sg.Cells[ACol,ARow];
  InflateRect(rect,-2,-2); //<<---- ansonsten nicht richtig positioniert wegen Zell-Rahmen
  DrawText(sg.Canvas.Handle, PChar(st), StrLen(PChar(st)),Rect,DT_CENTER);
end;


Diese Prozedur muss hier in Abhängigkeit von Cols und Rows aufgerufen werden :

ausblenden Delphi-Quelltext
1:
2:
procedure Tfrm.sgDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);

_________________
Gruß
Hansa
Ah-El-Eh-Iks Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 28

Win 3.11, Win ME, Win XP
Delphi 3 Prof., Delphi 2005 Pers.
BeitragVerfasst: Mi 17.10.07 13:27 
Nicht hauen :D , ich habe ein bisschen umgestellt... Aber es funktioniert...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
procedure TForm1.StringGridDrawText(StringGrid: TStringGrid; Rect: TRect; Col, Row, Format: Integer);
var s: String;
begin
  StringGrid.Canvas.FillRect(Rect);
  s := StringGrid.Cells[Col,Row];
  InflateRect(Rect,-2,-2);
  DrawText(StringGrid.Canvas.Handle, PChar(s), StrLen(PChar(s)),Rect,Format);
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
begin
 if (Col = 0) or (Row = 0) then
  begin
   StringGrid1.Canvas.Brush.Color := $0080FFFF;
   StringGridDrawText(StringGrid1, Rect, Col, Row, DT_CENTER);
  end;
end;


Der Vorteil, jetzt wird auch das Format übergeben an die Prozedur von Hansa, das heißt das ganze ist flexibler und die, die es geahnt haben, das soll eine Kassensoftware werden. Und die Preise kann man dann rechtsbündig ausrichten.
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Mi 17.10.07 13:49 
Aha, endlich ! Pixel halbieren, verdoppeln, DIV usw. ist wirklich Schwachsinn. :mrgreen: Du hast aber noch eine Alternative : Programm nur für Courier schreiben. :lol:

_________________
Gruß
Hansa
Ah-El-Eh-Iks Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 28

Win 3.11, Win ME, Win XP
Delphi 3 Prof., Delphi 2005 Pers.
BeitragVerfasst: Mi 17.10.07 13:59 
Hehe, OK... Das mit der WinAPI (ist es doch, oder?) sieht auch viel besser vom Quelltext her aus...

Jetzt bleibt für mich nur noch eine Frage: Was hat jakobwenzel mit den Speicherlöchern gemeint?
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Mi 17.10.07 14:14 
Wird das StringGrid und alles was dazugehört zur Laufzeit erzeugt ? Falls nein, dann ignoriere den Einwand komplett. Da ist aber noch was : wieso steht da im Klartext Stringgrid1 ? Soll das nur für ein einzelnes festes Stringgrid gelten ?

_________________
Gruß
Hansa


Zuletzt bearbeitet von hansa am Mi 17.10.07 14:22, insgesamt 1-mal bearbeitet
Ah-El-Eh-Iks Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 28

Win 3.11, Win ME, Win XP
Delphi 3 Prof., Delphi 2005 Pers.
BeitragVerfasst: Mi 17.10.07 14:18 
Ähm, ja also bei mir gibts nur eins, und das ist schon vorher da...

Ich weiß, man hätte noch eine Variable erzeugen können, und dann über Sender rausfinden können, welches StringGrid jetzt am zeichnen ist - das hab ich mir aber mal gespart (eben weils ja nur eins gibt).

Also Speicherlöcher für den aktuellen Fall vergessen? -OK, gelöscht^^