Einzelnen Beitrag anzeigen

Benutzerbild von Taladan
Taladan

Registriert seit: 5. Aug 2003
Ort: Osnabrück
505 Beiträge
 
Delphi 2005 Professional
 
#6

Re: Brauche hilfe bei übersetzung von C# in ObjectPascal (.N

  Alt 14. Jul 2005, 15:01
Zitat von Robert_G:
Nachtrag: Im Anhang ist die kompilierte Assembly. Du kannst es einfach direkt so verwenden, oder mit Hilfe des Reflectors den Code zu D.Net übersetzen.
Das hab ich getan, dabei kam aber ein ähm doch recht seltsamer Code heraus. Der so gar nicht funktionieren kann, denke ich.

Delphi-Quellcode:
unit DP.RichTextEx;
interface
type
public RichTextBoxEx = class(RichTextBox)
      // Methods
      public constructor Create;
      public function GetSelectionLink: Integer;
      strict private function GetSelectionStyle(mask: CharFormat2Mask; effect: CharFormat2Effect): Integer;
      public procedure InsertLink(text: string);
      public procedure InsertLink(text: string; position: Integer);
      public procedure InsertLink(text: string; hyperlink: string);
      public procedure InsertLink(text: string; hyperlink: string; position: Integer);
      [DllImport('user32.dll', CharSet=CharSet.Auto)]
      strict private class extern function SendMessage(hWnd: IntPtr; msg: Message; wParam: IntPtr; lParam: IntPtr): IntPtr; static;
      public procedure SetSelectionLink(link: boolean);
      strict private procedure SetSelectionStyle(mask: CharFormat2Mask; effect: CharFormat2Effect);

      // Properties
      [DefaultValue(false)]
      public property DetectUrls: boolean read get_DetectUrls write set_DetectUrls;

      // Fields
      strict private const SCF_SELECTION: Integer = 1;

      type // Nested Types
            [StructLayout(LayoutKind.Sequential)]
            strict private CharFormat2 = record
                  // Fields
                  public bAnimation: Byte;
                  public bCharSet: Byte;
                  public bPitchAndFamily: Byte;
                  public bReserved1: Byte;
                  public bRevAuthor: Byte;
                  public bUnderlineType: Byte;
                  public cbSize: Cardinal;
                  public crBackColor: Integer;
                  public crTextColor: Integer;
                  public dwEffects: CharFormat2Effect;
                  public dwMask: CharFormat2Mask;
                  public dwReserved: Integer;
                  public lcid: Integer;
                  public sSpacing: Word;
                  public sStyle: Smallint;
                  [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
                  public szFaceName: Char[];
                  public wKerning: Smallint;
                  public wWeight: Word;
                  public yHeight: Integer;
                  public yOffset: Integer;

            end;

            strict private CharFormat2Effect = (Link=32, None=0);
            strict private CharFormat2Mask = (Link=32);
            strict private Message = (GetCharFormat=1082, SetCharFormat=1092, User=1024);

end;
implementation
end.
Den habe ich ein wenig umstruktoriert und bis jetzt kam dieses heraus:
Delphi-Quellcode:
unit RichBoxHyperlink;

interface

uses
  System.Drawing, System.Collections, System.ComponentModel, System.Windows.Forms,
  System.Runtime.InteropServices;

type
   charformat2 = record
    bAnimation : System.Byte;
      bCharSet : system.Byte;
      bPitchAndFamily : system.Byte;
    bReserved1 : System.Byte;
    bRevAuthor: Byte;
    bUnderlineType : System.Byte;
    cbSize: Cardinal;
      crBackColor : integer;
      crTextColor : integer;
// dwEffects: CharFormat2Effect;
// dwMask: CharFormat2Mask;
      dwReserved : integer;
      lcid : integer;
    sSpacing : word;
    sStyle : Smallint;
     [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
     szFaceName : system.Char;
    wKerning : Smallint;
    wWeight : Smallint;
    yHeight : integer;
    yOffset : integer;
   end;


  TRichBoxHyperlink = class(System.Windows.Forms.Richtextbox)
  {$REGION 'Vom Designer verwalteter Code'}
  strict private
    /// <summary>
    /// Erforderliche Designervariable.
    /// </summary>
    Components: System.ComponentModel.Container;
    /// <summary>
    /// Erforderliche Methode zur Unterstützung des Designers -
    /// ändern Sie die Methode nicht mit dem Quelltext-Editor
    /// </summary>
    procedure InitializeComponent;
  {$ENDREGION}
  strict protected
    /// <summary>
    /// Ressourcen nach der Verwendung bereinigen
    /// </summary>
    procedure Dispose(Disposing: Boolean); override;
  private
    { Private-Deklarationen }
    FDetectUrls : boolean;
    function get_urls : boolean ;
    procedure set_urls(Value : boolean);

  public
    constructor Create; overload;
    constructor Create(Container: System.ComponentModel.IContainer); overload;
    procedure InsertLink(text : String); overload;
    procedure InsertLink(text : String; position : Integer); overload;
  published
    property DetectUrls : boolean read get_urls write set_urls default false;
  end;



implementation

uses
  System.Globalization;

{$AUTOBOX ON}

{$REGION 'Vom Windows Form-Designer erzeugter Code'}
/// <summary>
/// Erforderliche Methode zur Unterstützung des Designers -
/// ändern Sie die Methode nicht mit dem Quelltext-Editor
/// </summary>

procedure TRichBoxHyperlink.InitializeComponent;
begin
  Self.Components := System.ComponentModel.Container.Create;
end;

{$ENDREGION}

function TRichBoxHyperlink.get_urls : boolean;
begin
  result := FDetectUrls;
end;

procedure TRichBoxHyperlink.InsertLink(text : String);
begin
  InsertLink(Text, Self.SelectionStart);
end;


procedure TRichBoxHyperlink.InsertLink(text : String; position : Integer);
begin
  if (Position < 0) or (Position > self.Text.Length) then ArgumentOutOfRangeException.Create('Position') else
  begin
//         self.SelectionStart := position;
//         self.SelectedText := text;
//         self.Select(position, text.Length);
//         self.SetSelectionLink(true);
//         self.Select(position + text.Length, 0);
  end;
end;


procedure TRichBoxHyperlink.set_urls(Value : boolean);
begin
  FDetectUrls := Value;
end;

constructor TRichBoxHyperlink.Create;
begin
  inherited Create;
  //
  // Erforderlich für die Unterstützung des Windows Forms-Designers
  //
  InitializeComponent;
  //
  // TODO: Fügen Sie nach dem Aufruf von InitializeComponent Konstruktorcode hinzu.
  //
end;

constructor TRichBoxHyperlink.Create(Container: System.ComponentModel.IContainer);
begin
  inherited Create;
  //
  // Erforderlich für die Unterstützung des Windows Forms-Designers
  //
  Container.Add(Self);
  InitializeComponent;
  //
  // TODO: Fügen Sie nach dem Aufruf von InitializeComponent Konstruktorcode hinzu.
  //
end;

procedure TRichBoxHyperlink.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;

end.
Was ich nur überhaupt nicht zuordnen kann, und damit die Übersetzung übersetzten sind diese Zeilen:
Zitat:
strict private class extern function SendMessage(hWnd: IntPtr; msg: Message; wParam: IntPtr; lParam: IntPtr): IntPtr; static;

strict private CharFormat2Effect = (Link=32, None=0);

strict private CharFormat2Mask = (Link=32);

strict private Message = (GetCharFormat=1082, SetCharFormat=1092, User=1024);
PS: Ich habe mir vorgenommen die Komponente nicht nur zu verwenden, sondern möglichst auch zu verstehen, was ich verwende... (hoffentlich klappt das)
Marco
Wer Rechtschreibfehler findet, der darf sie behalten

Carpe Diem - Nutze den Tag (und zwar den ganzen!)
  Mit Zitat antworten Zitat