RTF über Zwischenablage nach Word

Anfängerfragen zum Programmieren mit PureBasic.
Benutzeravatar
Velindos
Beiträge: 598
Registriert: 15.11.2010 10:56

RTF über Zwischenablage nach Word

Beitrag von Velindos »

Hallo,
will meine Daten über die Zwischenablage in WORD einfügen.
Das Problem ist das nach dem Image einfügen in Word nichts
mehr kommt, sprich es fehtl der Ende Text!

Code: Alles auswählen

;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #B_Exit
  #Notiz
  #Bild
  #Stichwort
  #Bildvorlage
  #myDisplay
  #MySave
EndEnumeration

Global text$
Global rtf$
Global rrtf$

RedrawPfad$="C:\Velindos\Notizblock.bmp"
  ReadFile(0, RedrawPfad$)
  Buffersize = Lof(0)
  Buffer = AllocateMemory(Buffersize)
  ReadData(0, Buffer, Buffersize)
  CloseFile(0)
  myDisplay = CatchImage(#myDisplay, Buffer)
  ;ResizeImage (#myDisplay,270,290)

  
Procedure.s BMP2RTF(hBmap)
  Protected rtf$, bitmap.BITMAP, screenDC, hdcMF, hMF, hdc
  Protected oldImage, widthTwips, heightTwips, numBytes, mem, *bytes.BYTE, *string.WORD, i, low, high
  If GetObject_(hBmap, SizeOf(BITMAP), bitmap)
    If bitmap\bmWidth And bitmap\bmHeight
      screenDC = GetDC_(0)
      ;Create an EMF to hold the bitmap.
        hdcMF = CreateMetaFile_(0)
      If hdcMF
        SetMapMode_(hdcMF, #MM_ANISOTROPIC)
        SetWindowOrgEx_(hdcMF, 0, 0, 0)
        SetWindowExtEx_(hdcMF, bitmap\bmWidth, bitmap\bmHeight, 0)
        hdc = CreateCompatibleDC_(screenDC)
        If hdc
          oldImage = SelectObject_(hdc, hBmap)
          BitBlt_(hdcMF, 0, 0, bitmap\bmWidth, bitmap\bmHeight, hdc, 0, 0, #SRCCOPY)
          SelectObject_(hdc, oldImage)
          DeleteDC_(hdc)
          hMF = CloseMetaFile_(hdcMF)
          If hMF
            ;Before creating the RTF header we need to calculate the image width/height in twips.
              widthTwips = MulDiv_(bitmap\bmWidth,1440,GetDeviceCaps_(screenDC, #LOGPIXELSX))
              heightTwips = MulDiv_(bitmap\bmHeight,1440,GetDeviceCaps_(screenDC, #LOGPIXELSY))
            ;Now the rtf header.
              ;rtf$ = "{\rtf1{\pict\wmetafile8\picw" + Str(bitmap\bmWidth) + "\pich" + Str(bitmap\bmHeight) + "\picwgoal" + Str(widthTwips) + "\pichgoal" + Str(heightTwips) + " "
              rtf$ = "{\pict\wmetafile8\picw" + Str(bitmap\bmWidth) + "\pich" + Str(bitmap\bmHeight) + "\picwgoal" + Str(widthTwips) + "\pichgoal" + Str(heightTwips) + " "
              ;Add the MF bits as double-character hex.
            ;First retrieve the MF bits.
              numBytes = GetMetaFileBitsEx_(hMF, 0, 0)
            mem = AllocateMemory(numBytes*3)
            If mem
              If GetMetaFileBitsEx_(hMF, numBytes, mem) = numBytes
                *bytes=mem : *string = mem + numBytes
                For i = 0 To numBytes-1
                  low = (*bytes\b)&$f + '0': high = (*bytes\b)>>4&$f + '0'
                  If low > '9'
                    low + 7
                  EndIf
                  If high > '9'
                    high + 7
                  EndIf
                  *string\w = low<<8 + high
                  *string + 2
                  *bytes + 1
                Next
                rtf$ + PeekS(mem + numBytes, numBytes<<1, #PB_Ascii) + "}"
              Else
                rtf$ = ""
              EndIf
              FreeMemory(mem)
            Else
              rtf$ = ""
            EndIf
            DeleteMetaFile_(hMF)
          EndIf     
        Else
          hMF = CloseEnhMetaFile_(hdcMF)
          DeleteEnhMetaFile_(hMF)
        EndIf
      EndIf
      ReleaseDC_(0, screenDC)
    EndIf
  EndIf
  ProcedureReturn rtf$
EndProcedure
Procedure.i CopyRichTextToClipboard(rtf.s)
  Protected format.i
  Protected *globalMemory
  Protected *lock
  Protected result.i
 
 
 
  format = RegisterClipboardFormat_("Rich Text Format")
  If Not format
    ProcedureReturn #False
  EndIf
 
 
  If Not OpenClipboard_(#Null)
    ProcedureReturn #False
  Else
    EmptyClipboard_()
     
   
    *globalMemory = GlobalAlloc_(#GMEM_MOVEABLE | #GMEM_DDESHARE, Len(rtf) + 1)
    If Not *globalMemory
      ProcedureReturn #False
    EndIf
   
    *lock = GlobalLock_(*globalMemory)
    If Not *lock
      GlobalFree_(*globalMemory)
      ProcedureReturn #False
    EndIf
   
   
    PokeS(*lock, rtf, -1, #PB_Ascii) ; Text in ASCII umwandeln (falls Unicode verwendet wird) und schreiben.
    GlobalUnlock_(*globalMemory)
   
    result = SetClipboardData_(format, *globalMemory)
   
    GlobalFree_(*globalMemory)
    CloseClipboard_()
   
    ProcedureReturn result
  EndIf
 
 
  ProcedureReturn #False
EndProcedure

OpenWindow(#Window_0, 266, 29, 667, 708, "TXT+Image to RTF-File",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
StringGadget(#Stichwort,20,20,100,20,"lkkölkölk")
ImageGadget(#Bild,20,50,100,100,00)    
EditorGadget(#Notiz,20,400,200,100)
ButtonGadget(#MySave,300,20,120,20,"Save!")

SetGadgetState(#Bild, myDisplay)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow: End
    Case #PB_Event_Gadget
      
      Select EventGadget()
          
          ;Case #B_Dateiladen: Open_Windows_1()
          ;Case #B_OpenDatei: OpenDatei()
          
        Case #MySave
          Debug "mysave gedrückt"
          ;rtf$ = BMP2RTF(ImageID(#myDisplay))
          rtf$ = BMP2RTF(myDisplay)
          Debug rtf$
          
          MeinText$="Stichwort "
          MeinNotiz$="Meine Notizen"
          text$ = "{\rtf1"
          text$ + " \f2\fs28 "
          text$ + MeinText$ 
          text$ + "\par "
          text$ + "\fs20 Arial"
          ;text$ + "Zeilenumbruch \f1 Arial\par "
          text$ + MeinNotiz$
          text$ + "\par "
          text$ + "\par "
          text$ +  rtf$ +Chr(13)
          text$ + "\par "
          text$ + " Das Ende."
          text$ + "}"
          CopyRichTextToClipboard(text$)
          

        Case #B_Exit: End
          
      EndSelect
  EndSelect
ForEver
Kann jemand sagen an was das liegt?
Gruss ... Velindos
Zuletzt geändert von Velindos am 04.11.2011 15:14, insgesamt 1-mal geändert.
Windows 7/8/8.1/10 (32/64-Bit) |Ubuntu 10.4 (64-Bit) |Purebasic 5.71 LTS (32/64-Bit)
Benutzeravatar
bobobo
jaAdmin
Beiträge: 3873
Registriert: 13.09.2004 17:48
Kontaktdaten:

Re: RTF über Zwischenablage nach Word

Beitrag von bobobo »

du bist nur etwas blind .. dein rtf$ schließt mit zwei } ab

wennste mal eins wegnimmst wirst du sicher glücklicher

! merke : debug alleine reicht manchmal nicht
ich schreibe sowas gerne mal in dateien und schau mir das Ergebnis genau an

ää warum ist das bild eingentlich so mickrig in word?
‮pb aktuel 6.2 windoof aktuell und sowas von 10
Ich hab Tinnitus im Auge. Ich seh nur Pfeifen.
Benutzeravatar
Velindos
Beiträge: 598
Registriert: 15.11.2010 10:56

Re: RTF über Zwischenablage nach Word

Beitrag von Velindos »

Hallo bobobo,
Danke für deinen Tip, jetzt läufts schon besser. Habe die "}" entfernt und nun funzt die Leerzeilen und der "Ende" Text!
Mit dem positionieren des Bildes und den Textlauf habe ich noch Probleme. Wie kann man das Bild nach rechts schieben und den
Text links platzieren?
Gruss ... Velindos
Windows 7/8/8.1/10 (32/64-Bit) |Ubuntu 10.4 (64-Bit) |Purebasic 5.71 LTS (32/64-Bit)
Benutzeravatar
bobobo
jaAdmin
Beiträge: 3873
Registriert: 13.09.2004 17:48
Kontaktdaten:

Re: RTF über Zwischenablage nach Word

Beitrag von bobobo »

In Word ein Bild einbinden und skalieren und positionieren und als rtf speichern
Das RTF mal genauer per Editor inspizieren. Irgendwo sollte da ja was von
der Positionierung und Skalierung zu "sehen" sein.

ansonsten
http://msdn.microsoft.com/de-de/library/aa140277.aspx
‮pb aktuel 6.2 windoof aktuell und sowas von 10
Ich hab Tinnitus im Auge. Ich seh nur Pfeifen.
Antworten