Autor Beitrag
Chryzler
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: So 17.06.07 13:02 
Hi,

wiedermal ein Problem. Baue mir gerade einen Texteditor und muss (oder eher will) die letzte verwendete Schriftart in der Registry abspeichern. Also schreibe ich per
ausblenden C#-Quelltext
1:
2:
Font TextFont; 
regKey.SetValue("TextFont", TextFont, RegistryValueKind.Unknown);

die Schriftart in die Registry. Klappt auch problemlos. In Regedit wird der Wert so dargestellt:
Zitat:
[Font: Name=Courier New, Size=9,75, Units=3, GdiCharSet=0, GdiVerticalFont=False]

Nun wollte ich den Wert wieder per
ausblenden C#-Quelltext
1:
2:
3:
RegistryKey regKey;  
Font TextFont; 
TextFont = (Font)regKey.GetValue("TextFont"0));

einlesen, jedoch kann er den String aus der Registry nicht mehr in einen Font casten, ist ja eigentlich logisch.

Wie kann ich nun am geschicktesten diese dumme Schriftart in der Registry abspeichern? Irgendeinen anderen RegistryValueKind nehmen oder so? :nixweiss:

Danke schonmal!
Chryzler
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 17.06.07 15:40 
Was spricht denn gegen die simple Verwendung einer Config-Datei statt des plattformabhängigen Registrierungszeugs?
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: So 17.06.07 16:11 
Unabhängig von der Frage Registry oder Config: Für die Konvertierung Font <-> String gibt es folgende Methoden:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
using System.ComponentModel.TypeDescriptor;
//  Deklaration
Font myFont;
string myFontString;
//  Konvertierungen (es gibt auch noch ein paar Varianten)
myFontString = GetConverter(typeof(Font)).ConvertToInvariantString(myFont);
myFont = GetConverter(typeof(Font)).ConvertFromInvariantString(myFontString);

Gruß Jürgen
Chryzler Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: So 17.06.07 17:34 
user profile iconKhabarakh hat folgendes geschrieben:
Was spricht denn gegen die simple Verwendung einer Config-Datei statt des plattformabhängigen Registrierungszeugs?

Nun, so richtig Plattformunabhängig ist .NET nun wirklich nicht. Allein für eine simple Konsolenanwendung, die ich für Windows geschrieben hab, musste ich die Hälfte Code löschen, weil Mono jede zweite Methode nicht kannte. Wie soll das dann bei einer WinForms-Anwendung aussehen?
user profile iconJüTho hat folgendes geschrieben:
Unabhängig von der Frage Registry oder Config: Für die Konvertierung Font <-> String gibt es folgende Methoden:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
using System.ComponentModel.TypeDescriptor;
//  Deklaration
Font myFont;
string myFontString;
//  Konvertierungen (es gibt auch noch ein paar Varianten)
myFontString = GetConverter(typeof(Font)).ConvertToInvariantString(myFont);
myFont = GetConverter(typeof(Font)).ConvertFromInvariantString(myFontString);

Gruß Jürgen

Sieht sehr interessant aus und funktioniert perfekt. Man kann also praktisch jeden Typ in einen String umwandeln? Ich werds jetzt erstmal so machen, vielleicht entscheide ich mich später doch nochmal um auf eine Config-Datei, mal sehen.

P.S: Der Codeschnipsel muss so lauten:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
using System.ComponentModel;  // ist standardmäßig sowieso in der Uses-Liste
//  Deklaration
Font myFont;
string myFontString;
//  Konvertierungen (es gibt auch noch ein paar Varianten)
myFontString = (string)TypeDescriptor.GetConverter(typeof(Font)).ConvertToInvariantString(myFont);
myFont = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromInvariantString(myFontString);
UGrohne
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Veteran
Beiträge: 5502
Erhaltene Danke: 220

Windows 8 , Server 2012
D7 Pro, VS.NET 2012 (C#)
BeitragVerfasst: So 17.06.07 17:40 
user profile iconChryzler hat folgendes geschrieben:
user profile iconKhabarakh hat folgendes geschrieben:
Was spricht denn gegen die simple Verwendung einer Config-Datei statt des plattformabhängigen Registrierungszeugs?

Nun, so richtig Plattformunabhängig ist .NET nun wirklich nicht. Allein für eine simple Konsolenanwendung, die ich für Windows geschrieben hab, musste ich die Hälfte Code löschen, weil Mono jede zweite Methode nicht kannte. Wie soll das dann bei einer WinForms-Anwendung aussehen?

.NET ist nicht richtig plattformabhängig, das ist korrekt. Die Registrierung ist es aber, denn die gibt es nur unter Windows.
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: So 17.06.07 19:53 
user profile iconChryzler hat folgendes geschrieben:

P.S: Der Codeschnipsel muss so lauten:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
using System.ComponentModel;  // ist standardmäßig sowieso in der Uses-Liste
//  Deklaration
Font myFont;
string myFontString;
//  Konvertierungen (es gibt auch noch ein paar Varianten)
myFontString = (string)TypeDescriptor.GetConverter(typeof(Font)).ConvertToInvariantString(myFont);
myFont = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromInvariantString(myFontString);

Sorry, den Cast auf Font hatte ich vergessen zu kopieren. Der Cast auf String ist aber überflüssig. Jürgen
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mo 18.06.07 14:35 
user profile iconChryzler hat folgendes geschrieben:
Nun, so richtig Plattformunabhängig ist .NET nun wirklich nicht.

Die Plattformunabhängigkeit ist auch nur das schwächste Argument für Configs. Erstens sind sie, wie ich schon schrob, um Einiges leichter einzusetzen, zweitens sind sie einfach der .nette Standard, der "Way to go" (s. z.B. ClickOnce & ASP.Net).
user profile iconChryzler hat folgendes geschrieben:
Man kann also praktisch jeden Typ in einen String umwandeln?
Jeden, der mit einem TypeConverterAttribute versehen ist, das sind aber schon einige. TypeConverters sind übrigens auch jene Klassen, die die XAML-Attribute parsen.