Exotische Frage um Klassen realisierung

  • VB.NET

Es gibt 15 Antworten in diesem Thema. Der letzte Beitrag () ist von Dodo.

    Exotische Frage um Klassen realisierung

    Mir ist leider kein guter Titel für mein Vorhaben eingefallen.

    Also ich möchte gerne eine Klasse erstellen die mir nach folgendem Prinzip etwas vom Typ Font zurück gibt

    VB.NET-Quellcode

    1. Dim Arial As New MeineKlasse("Arial")
    2. Dim f As Font = Arial(12).Regular


    Grund dafür ist, das ich nicht ständig

    VB.NET-Quellcode

    1. New Font("Arial", 12, FontStlye.Regular)


    schreiben möchte und auch nicht alle Schriftarten die ich verwenden möchte erst vordefinieren.

    Selbst bin ich soweit gekommen dass ich folgendes Prinzip habe

    VB.NET-Quellcode

    1. Dim f As Font = Arial.Size(12).Regular


    Würde aber wegen der Kürze gerne das .Size wegfallen lassen.
    Font.cls
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class MeineKlasse
    2. Private _fontName As String
    3. Public Sub New(ByVal Fontname As String)
    4. _fontName = Fontname
    5. End Sub
    6. Public Function Size(ByVal Fontsize As Integer) As FontStyle
    7. Return New FontStyle(_fontName, Fontsize)
    8. End Function
    9. Public Class FontStyle
    10. Private _fontName As String
    11. Private _fontSize As Integer
    12. Public Sub New(ByVal Fontname As String, ByVal Fontsize As Integer)
    13. _fontName = Fontname
    14. End Sub
    15. Public ReadOnly Property Bold() As Font
    16. Get
    17. Return New Font(_fontName, _fontSize, FontStyle.Bold)
    18. End Get
    19. End Property
    20. Public ReadOnly Property BoldItalic() As Font
    21. Get
    22. Return New Font(_fontName, _fontSize, FontStyle.BoldItalic)
    23. End Get
    24. End Property
    25. Public ReadOnly Property Italic() As Font
    26. Get
    27. Return New Font(_fontName, _fontSize, FontStyle.Italic)
    28. End Get
    29. End Property
    30. Public ReadOnly Property Regular() As Font
    31. Get
    32. Return New Font(_fontName, _fontSize, FontStyle.Regular)
    33. End Get
    34. End Property
    35. Public ReadOnly Property Strikeout() As XFont
    36. Get
    37. Return New XFont(_fontName, _fontSize, FontStyle.Strikeout)
    38. End Get
    39. End Property
    40. Public ReadOnly Property Underline() As Font
    41. Get
    42. Return New Font(_fontName, _fontSize, FontStyle.Underline)
    43. End Get
    44. End Property
    45. End Class
    46. End Class


    Ich habe mir schon überlegt ob IEnumerable dafür verwendet werden könnte aber ich denke nicht.

    PS: Ich würde gerne genau das Format haben wie ich oben genannt, also nicht sowas

    VB.NET-Quellcode

    1. Dim f As Font = MeineFunktion("Arial", 12).Regular
    Ganz kann ich den Sinn nicht erkennen. Dein Wunsch-Code ganz oben ist länger als die normale Font-Deklaration. Der Fontstyle existiert doch schon als Enum, also warum nochmal sowas austüfteln?
    Was vielleicht Sinn machen würde, wäre die vorhandenen (zu verwendenden) Fontnamen als Enum oder noch besser in einer List(Of T) einmalig anzulegen.
    Sonst wüßt ichs jetz nich besser...Sry
    :thumbsup: Seit 26.Mai 2012 Oppa! :thumbsup:

    VB.NET-Quellcode

    1. Dim Arial As New MeineKlasse("Arial") 'Die Deklaration gehört ja numa dazu
    2. Dim f As Font = Arial(12).Regular 'und dann das hier
    3. Dim f As Font = New Font("Arial", 12, FontStlye.Regular) 'und so isses Standard

    Dann würd ich mir lieber ne Collection an Schriftarten zusamenstellen und dann mit Font_1, Font_2 usw aufrufen (auch gern mit Font(Font_1,12)
    Vielleicht is auch FontFamilywas für dich?
    :thumbsup: Seit 26.Mai 2012 Oppa! :thumbsup:
    Hi,

    wie wäre es mit einem zweckentfremdeten Indexer für "MeineKlasse"?

    VB.NET-Quellcode

    1. Default Public Overloads ReadOnly Property Item(ByVal size As Integer) As FontStyle
    2. Get
    3. Return New FontStyle(_fontName, size)
    4. End Get
    5. End Property


    Damit kannst du Aufrufe wie "Arial(12).Bold" tätigen.
    Hat mit der ursprünglichen Funktion eines Indexers nur bedingt zu tun, aber egal...

    MfG
    Turtle10

    Vatter schrieb:

    VB.NET-Quellcode

    1. Dim Arial As New MeineKlasse("Arial") 'Die Deklaration gehört ja numa dazu
    2. Dim f As Font = Arial(12).Regular 'und dann das hier
    3. Dim f As Font = New Font("Arial", 12, FontStlye.Regular) 'und so isses Standard

    Dann würd ich mir lieber ne Collection an Schriftarten zusamenstellen und dann mit Font_1, Font_2 usw aufrufen (auch gern mit Font(Font_1,12)
    Vielleicht is auch FontFamilywas für dich?

    Naja aber jetzt mal die Anwendung:

    VB.NET-Quellcode

    1. g.DrawString("Text", Arial(12).Regular, Brushes.Black)
    2. g.DrawString("Text2", Arial(9).Regular, Brushes.Black)
    3. g.DrawString("Text3", Arial(10).Bold, Brushes.Black)
    4. g.DrawString("Text4", Arial(7).Regular, Brushes.Black)
    5. g.DrawString("Text5", Arial(13).Regular, Brushes.Black)
    6. g.DrawString("Text5", Arial(11).Regular, Brushes.Black)
    7. ' im gegensatz zu
    8. g.DrawString("Text", New Font("Arial", 12, FontStyles.Regular), Brushes.Black)
    9. g.DrawString("Text2", New Font("Arial", 9, FontStyles.Regular)r, Brushes.Black)
    10. g.DrawString("Text3", New Font("Arial", 10, FontStyles.Bold), Brushes.Black)
    11. g.DrawString("Text4", New Font("Arial", 7, FontStyles.Regular), Brushes.Black)
    12. g.DrawString("Text5", New Font("Arial", 13, FontStyles.Regular), Brushes.Black)
    13. g.DrawString("Text5", New Font("Arial", 11, FontStyles.Regular), Brushes.Black)


    @turtle10: achso vielen Dank, werde ich mal probieren, danke schön.
    Ginge statt dem

    VB.NET-Quellcode

    1. g.DrawString("Text", Arial(12).Regular, Brushes.Black)

    nich auch eine einfache Funktion?

    VB.NET-Quellcode

    1. g.DrawString("Text", Arial(12, Regular), Brushes.Black)

    Is nur ein Kamma statt Punkt und die Klammer eins weiter hinten... :huh:
    :thumbsup: Seit 26.Mai 2012 Oppa! :thumbsup:
    Das würde gehen, aber zum einen siehts nicht so schön aus, wegne Code übersichtlichkeit und zum zweiten was ist mit mehreren Schriftarten??

    VB.NET-Quellcode

    1. Dim Arial As New MeineKlasse("Arial")
    2. Dim Verdana As New MeineKlasse("Verdana")
    3. Dim TimesRoman As New MeineKlasse("Times New Roman")


    Als Funktionen

    VB.NET-Quellcode

    1. Private Function Arial (Size, Style) As Font
    2. Return New Font("Arial", Size, Style)
    3. End Function
    4. Private Function Verdana(Size, Style) As Font
    5. Return New Font("Verdana", Size, Style)
    6. End Function
    7. Private Function TimesRoman(Size, Style) As Font
    8. Return New Font("Times New Roman", Size, Style)
    9. End Function


    Jetzt könnte man noch sagen an EINE Funktion an die man Name, Size und Style übergibt. Klar ginge theoretisch, aber es sieht nicht so schön aus und bei mir soll der Code übersichtlich bleiben, gerade beim DrawString() würden viele Klammern und Anführungszeichen sehr stark die Übersichtlichkeit beeinflussen, deswegen will ich es als eine Art Enum haben.

    Am liebsten sogar so

    VB.NET-Quellcode

    1. Arial.12.Regular


    Aber das geht wohl nicht weil ich die 12 ja auslesen muss.
    Hi
    Machs doch so:

    VB.NET-Quellcode

    1. Public NotInheritable Class FontDescription
    2. Private ffDefaultFontFamily As FontFamily
    3. Private sngDefaultEmSize As Single
    4. Private fsDefaultStyle As FontStyle
    5. Public Sub New(ByVal fontName As String)
    6. ffDefaultFontFamily = New FontFamily(fontName)
    7. End Sub
    8. Public Sub New(ByVal fontName As String, ByVal emSize As Single)
    9. ffDefaultFontFamily = New FontFamily(fontName)
    10. sngDefaultEmSize = emSize
    11. End Sub
    12. Public Sub New(ByVal fontName As String, ByVal emSize As Single, ByVal defaultStyle As FontStyle)
    13. ffDefaultFontFamily = New FontFamily(fontName)
    14. sngDefaultEmSize = emSize
    15. fsDefaultStyle = defaultStyle
    16. End Sub
    17. Public Property DefaultFontFamily() As FontFamily
    18. Get
    19. Return ffDefaultFontFamily
    20. End Get
    21. Set(ByVal value As FontFamily)
    22. ffDefaultFontFamily = value
    23. End Set
    24. End Property
    25. Public Property DefaultEmSize() As Single
    26. Get
    27. Return sngDefaultEmSize
    28. End Get
    29. Set(ByVal value As Single)
    30. sngDefaultEmSize = value
    31. End Set
    32. End Property
    33. Public Property DefaultStyle() As FontStyle
    34. Get
    35. Return fsDefaultStyle
    36. End Get
    37. Set(ByVal value As FontStyle)
    38. fsDefaultStyle = value
    39. End Set
    40. End Property
    41. Public ReadOnly Property DefaultFont() As Font
    42. Get
    43. Return New Font(ffDefaultFontFamily, sngDefaultEmSize, fsDefaultStyle)
    44. End Get
    45. End Property
    46. Default Public ReadOnly Property Font(ByVal emSize As Integer) As Font
    47. Get
    48. Return New Font(ffDefaultFontFamily, emSize, FontStyle.Regular)
    49. End Get
    50. End Property
    51. Default Public ReadOnly Property Font(ByVal emSize As Integer, ByVal style As FontStyle) As Font
    52. Get
    53. Return New Font(ffDefaultFontFamily, emSize, style)
    54. End Get
    55. End Property
    56. Default Public ReadOnly Property Font(ByVal fontName As String, ByVal emDSize As Integer, ByVal style As FontStyle) As Font
    57. Get
    58. Return New Font(fontName, emDSize, style)
    59. End Get
    60. End Property
    61. Default Public ReadOnly Property Font(ByVal fontFamily As FontFamily, ByVal emSize As Integer, ByVal style As FontStyle) As Font
    62. Get
    63. Return New Font(fontFamily, emSize, style)
    64. End Get
    65. End Property
    66. End Class


    Arial.12.Regular ist auf herkömmlichem Weg nicht möglich (und auch nicht zu empfehlen).

    Gruß
    ~blaze~
    Schau mal, ob das etwas für dich ist: (Obwohl ich die Standard-Methode nicht unübersichtlich finde...)
    Spoiler anzeigen


    VB.NET-Quellcode

    1. Imports System.Runtime.CompilerServices
    2. Public Class Form1
    3. Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    4. Dim g As Graphics = e.Graphics
    5. Dim fnt As Font = Font.CreateFont
    6. g.DrawString("laangerText", fnt.tofont("Wingdings"), Brushes.Black, New Point(29, 43))
    7. ' jetzt in Größe 23
    8. g.DrawString("grooßer Text", fnt.toSize(23), Brushes.Pink, New Point(9, 6))
    9. 'jetzt totaler Mix
    10. g.DrawString("Mix", fnt.toStrikeout.toSize(135), Brushes.Green, New Point(14, 14))
    11. g.DrawString("Re-Mix", fnt.toBold.toSize(148), Brushes.Red, New Point(46, 16))
    12. g.DrawString("Edit", fnt.toItalic.toSize(128), Brushes.Pink, New Point(88, 18))
    13. 'Alles zusammen:
    14. g.DrawString("(c) 2011 der_Kurt", fnt.tofont("tahoma").toItalic.toSize(18).toBold.toStrikeout, Brushes.Black, New Point(100, 350))
    15. '****************************************
    16. '****************************************
    17. '** BTW: Das ist die Standarddefinition, die ich kein bißchen unübersichtlich finde...
    18. g.DrawString("text2", _
    19. New Font("Arial", 9, FontStyle.Regular), _
    20. Brushes.Black, _
    21. New PointF(29, 39))
    22. End Sub
    23. End Class


    VB.NET-Quellcode

    1. Module gfx_erweiterung
    2. Dim theFont As Font
    3. Dim defaultsize As Single = 8
    4. Dim defaultFontstyle As FontStyle = FontStyle.Regular
    5. Dim defaultfont As New Font("Arial", defaultsize, defaultFontstyle)
    6. <Extension()> Function CreateFont(ByVal fnt As Font) As Font
    7. Return defaultfont
    8. End Function
    9. <Extension()> Public Function toRegular(ByVal fnt As Font) As Font
    10. Return New Font(fnt.FontFamily, fnt.Size, fnt.Style Or FontStyle.Regular)
    11. End Function
    12. <Extension()> Public Function toItalic(ByVal fnt As Font) As Font
    13. Return New Font(fnt.FontFamily, fnt.Size, fnt.Style Or FontStyle.Italic)
    14. End Function
    15. <Extension()> Public Function toStrikeout(ByVal fnt As Font) As Font
    16. Return New Font(fnt.FontFamily, fnt.Size, fnt.Style Or FontStyle.Strikeout)
    17. End Function
    18. <Extension()> Public Function toBold(ByVal fnt As Font) As Font
    19. Return New Font(fnt.FontFamily, fnt.Size, fnt.Style Or FontStyle.Bold)
    20. End Function
    21. <Extension()> Function toSize(ByVal fnt As Font, ByVal size As Single) As Font
    22. Return New Font(fnt.FontFamily, size, fnt.Style)
    23. End Function
    24. <Extension()> Function tofont(ByVal fnt As Font, ByVal newfont As String) As Font
    25. Dim newfontfamily As New FontFamily(newfont)
    26. Return New Font(newfontfamily, defaultsize)
    27. End Function
    28. End Module


    Ich empfehle noch Fehlerbehandlung (falls der Font nicht existiert). Code nur kurz getestet, sollte aber funktionieren.

    LG, Kurti
    Hi Dodo,
    nachdem ich nun (glaub ich) endlich verstanden hab, was du willst, werde ich mich nun ein letztes mal blamieren mit folgendem simplen Vorschlag:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class MeineSchrift
    2. Private _name As String
    3. Sub New(ByVal Name As String)
    4. _name = Name
    5. End Sub
    6. Public ReadOnly Property Bold(ByVal Size As Integer) As Font
    7. Get
    8. Return New Font(_name, Size, FontStyle.Bold)
    9. End Get
    10. End Property
    11. Public ReadOnly Property Italic(ByVal Size As Integer) As Font
    12. Get
    13. Return New Font(_name, Size, FontStyle.Italic)
    14. End Get
    15. End Property
    16. Public ReadOnly Property Regular(ByVal Size As Integer) As Font
    17. Get
    18. Return New Font(_name, Size, FontStyle.Regular)
    19. End Get
    20. End Property
    21. 'usw. usw.
    22. End Class


    Und Aufruf mit z.B.:

    VB.NET-Quellcode

    1. Dim Arial as new MeineSchrift("Arial")
    2. e.Graphics.DrawString("Hallo Schrift!", Arial.Bold(12), Brushes.Black, 20, 20)

    das kommt doch dem schon sehr nahe, was du willst oder? :rolleyes:
    :thumbsup: Seit 26.Mai 2012 Oppa! :thumbsup:
    Hallo,

    ja das kommt dem schon nah und sicher, wieso bin ich nicht auf diese Idee gekommen die Size einfach hinten mit anzugeben?
    Ich war so fixiert auf die Instanzierung der normalen Font wo erst Fontname, Size und Style angegeben wird das ich diese Reihenfolge beibehalten wollte.

    Ich denke mal ich werde deine Methode übernehmen. Vielen Dank nochmal an alle!