Einzelnen Beitrag anzeigen

Benutzerbild von MaBuSE
MaBuSE

Registriert seit: 23. Sep 2002
Ort: Frankfurt am Main (in der Nähe)
1.837 Beiträge
 
Delphi 10 Seattle Enterprise
 
#8

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

  Alt 14. Jul 2005, 20:29
Zitat von Taladan:
Hallo. Ich möchte eine Klasser in .Net erstellen, die auch Hyperlinks anzeigen kann. Bisher habe ich keine entsprechende Komponente gefunden. Eine Komponente jedoch, die in C# geschrieben und auf der RichTextBox basiert, die so was kann. Leider kann ich kein C# und der Code ist für den Start doch ein wenig viel des guten.
Würde mich freuen, wenn jemand mir bei der Übersetzung hilft. Seien es nur Teile oder gar die ganze Komponente.
Warum denkt den niemand an den BabelCode ?

Es gibt einen sehr interesannten Artikel dazu im BDN.
Das Ganze wurde auch von Ua und Magin auf den Delphi Tagen in Kassel gezeigt.

BDN Artikel - BabelCode - C# to Delphi Converter (A tale of two CodeDOMs)

Hier ist der BabelCode direkt zum Ausprobieren !!!

Man gibt ihm etwas C# Code und er macht Delphi .net Code daraus.
OK er ist nicht perfekt, manche "Spezialitäten" kann er noch nicht übersetzen, aber das ist dann schon mal ein solider Grundstock auf dem man aufsetzen kann.

Ich hoffe diese Info hilft Euch weiter

----------schnipp---------------------------------

Hier ist der generierte Code:
[edit]
Den langen Code, durch den gekürzten von Robert_G ersetzt
[/edit]

Delphi-Quellcode:
//------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.1.4322.573
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

unit DP.RichTextEx;

interface

type
  RichTextBoxEx = class(RichTextBox)
  public
  type
    CharFormat2Mask = (Link);
    CharFormat2Effect = (_Link, None);
    TArrayOfWideChar = array of WideChar;
    [StructLayout(LayoutKind.Sequential)]
    CharFormat2 = class
    public
      cbSize: Cardinal;
      dwMask: CharFormat2Mask;
      dwEffects: CharFormat2Effect;
      yHeight: Integer;
      yOffset: Integer;
      crTextColor: Integer;
      bCharSet: Byte;
      bPitchAndFamily: Byte;
      szFaceName: TArrayOfWideChar;
      wWeight: System.UInt16;
      sSpacing: System.UInt16;
      crBackColor: Integer;
      lcid: Integer;
      dwReserved: Integer;
      sStyle: SmallInt;
      wKerning: SmallInt;
      bUnderlineType: Byte;
      bAnimation: Byte;
      bRevAuthor: Byte;
      bReserved1: Byte;
    end;
    
    Message = (User, GetCharFormat, SetCharFormat);
  strict private
    SCF_SELECTION: Integer;
  public
    constructor Create;
    function get_DetectUrls: Boolean; reintroduce;
    procedure set_DetectUrls(Value: Boolean); reintroduce;
    [DefaultValue(False)]
    property DetectUrls: Boolean read get_DetectUrls write set_DetectUrls;
  strict private
    [DllImport('user32.dll', CharSet=CharSet.Auto)]
    class function SendMessage(hWnd: IntPtr; msg: Message; wParam: IntPtr; lParam: IntPtr): IntPtr;static;
  public
    procedure InsertLink(text: string); overload;
    procedure InsertLink(text: string; position: Integer); overload;
    procedure InsertLink(text: string; hyperlink: string); overload;
    procedure InsertLink(text: string; hyperlink: string; position: Integer); overload;
    procedure SetSelectionLink(_link: Boolean);
    function GetSelectionLink: Integer;
  strict private
    procedure SetSelectionStyle(mask: CharFormat2Mask; effect: CharFormat2Effect);
    function GetSelectionStyle(mask: CharFormat2Mask; effect: CharFormat2Effect): Integer;
  end;
  
implementation

{$AUTOBOX ON}
{$HINTS OFF}
{$WARNINGS OFF}

constructor RichTextBoxEx.Create;
begin
  inherited Create;
  Self.DetectUrls := False;
end;

function RichTextBoxEx.get_DetectUrls: Boolean;
begin
  Result := inherited DetectUrls;
end;

procedure RichTextBoxEx.set_DetectUrls(Value: Boolean);
begin
  inherited DetectUrls := value;
end;

class function RichTextBoxEx.SendMessage(hWnd: IntPtr; msg: Message; wParam: IntPtr;
  lParam: IntPtr): IntPtr;
begin
end;

procedure RichTextBoxEx.InsertLink(text: string);
begin
  InsertLink(text, Self.SelectionStart);
end;

procedure RichTextBoxEx.InsertLink(text: string; position: Integer);
begin
  if ((position < 0) or (position > Self.Text.Length)) then
    raise ArgumentOutOfRangeException.Create('position');
  Self.SelectionStart := position;
  Self.SelectedText := text;
  Self.Select(position, text.Length);
  Self.SetSelectionLink(True);
  Self.Select((position + text.Length), 0);
end;

procedure RichTextBoxEx.InsertLink(text: string; hyperlink: string);
begin
  InsertLink(text, hyperlink, Self.SelectionStart);
end;

procedure RichTextBoxEx.InsertLink(text: string; hyperlink: string; position: Integer);
begin
  if ((position < 0) or (position > Self.Text.Length)) then
    raise ArgumentOutOfRangeException.Create('position');
  Self.SelectionStart := position;
  Self.SelectedRtf := (((('{\rtf1\ansi ' + text) + '\v #') + hyperlink) + '\v0}');
  Self.Select(position, ((text.Length + hyperlink.Length) + 1));
  Self.SetSelectionLink(True);
  Self.Select((((position + text.Length) + hyperlink.Length) + 1), 0);
end;

procedure RichTextBoxEx.SetSelectionLink(__link: Boolean);
begin
  SetSelectionStyle(CharFormat2Mask.Link, );
end;

function RichTextBoxEx.GetSelectionLink: Integer;
begin
  Result := GetSelectionStyle(CharFormat2Mask.Link, CharFormat2Effect.Link);
end;

procedure RichTextBoxEx.SetSelectionStyle(mask: CharFormat2Mask; effect: CharFormat2Effect);
var
  lpar: IntPtr;
  wpar: IntPtr;
  cf: CharFormat2;
begin
  cf := CharFormat2.Create;
  cf.cbSize := (Cardinal(Marshal.SizeOf(cf)));
  cf.dwMask := mask;
  cf.dwEffects := effect;
  wpar := IntPtr.Create(SCF_SELECTION);
  lpar := Marshal.AllocCoTaskMem(Marshal.SizeOf(cf));
  Marshal.StructureToPtr(cf, lpar, False);
  SendMessage(Self.Handle, Message.SetCharFormat, wpar, lpar);
  Marshal.FreeCoTaskMem(lpar);
end;

function RichTextBoxEx.GetSelectionStyle(mask: CharFormat2Mask; effect: CharFormat2Effect): Integer;
type
  TArrayOfArrayOfWideChar = array of array of WideChar;
var
  state: Integer;
  lpar: IntPtr;
  wpar: IntPtr;
  cf: CharFormat2;
begin
  cf := CharFormat2.Create;
  cf.cbSize := (Cardinal(Marshal.SizeOf(cf)));
  cf.szFaceName := New(TArrayOfArrayOfWideChar, 32);
  wpar := IntPtr.Create(SCF_SELECTION);
  lpar := Marshal.AllocCoTaskMem(Marshal.SizeOf(cf));
  Marshal.StructureToPtr(cf, lpar, False);
  SendMessage(Self.Handle, Message.GetCharFormat, wpar, lpar);
  cf := (CharFormat2(Marshal.PtrToStructure(lpar, TypeOf(CharFormat2))));
  
  if ((cf.dwMask and mask) = mask) then
    if ((cf.dwEffects and effect) = effect) then
      state := 1
    else
      state := 0
  else
    state := -1;
  Marshal.FreeCoTaskMem(lpar);
  Result := state;
end;

end.
(°¿°) MaBuSE - proud to be a DP member
(°¿°) MaBuSE - proud to be a "Rüsselmops" ;-)
  Mit Zitat antworten Zitat