Zwischenablage



  • Hallo
    Kann mir jemand sagen, wie ich eine Variable in die Windows Zwischenablage schreiben kann, um dem Inhalt mit ctrl + V später zu verwenden?

    Vielen Dank
    Gruss Simu



  • LRESULT CMainDlg::OnOk(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
    {
      int textlength = ::GetWindowTextLength(GetDlgItem(IDC_PASSWORD)) + 1;
      if (textlength > 1) {
        if (::OpenClipboard(m_hWnd) && ::EmptyClipboard()) {
          HGLOBAL hClipboardText = GlobalAlloc(GMEM_DDESHARE, textlength);
          if (hClipboardText) {
            LPSTR  lpstrClipboardText = static_cast<LPSTR>(::GlobalLock(hClipboardText));
            if (lpstrClipboardText) {
              ::GetWindowText(GetDlgItem(IDC_PASSWORD), lpstrClipboardText, textlength);
              if (::GlobalUnlock(hClipboardText))
                ::SetClipboardData(CF_TEXT, hClipboardText);
            }
          } 
          ::CloseClipboard();
        }
      }
    
      CloseDialog(wID);
      return 0;
    }
    


  • Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum WinAPI verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • Danke für Deine Hilfe Redhead
    Bin noch Anfänger...
    könntest Du nicht den Code dokumentieren?
    Vieleicht schnall ich's dann...



  • LRESULT CMainDlg::OnOk(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) 
    {
      // Länge des Passwortes + 1 = benötigter Speicher
      int textlength = ::GetWindowTextLength(GetDlgItem(IDC_PASSWORD)) + 1; 
      // Wenn ein Passwort (Länge > 0) existiert =>
      if (textlength > 1) { 
        // Wenn das Öffnen des Clipboard und das Leeren des alten Inhaltes erfolgreich sind =>
        if (::OpenClipboard(m_hWnd) && ::EmptyClipboard()) { 
          HGLOBAL hClipboardText = GlobalAlloc(GMEM_DDESHARE, textlength); 
          // Wenn die Allokation des globalen Speicherblocks erfolgreich ist =>
          if (hClipboardText) { 
            LPSTR  lpstrClipboardText = static_cast<LPSTR>(::GlobalLock(hClipboardText)); 
            // Wenn das Casting auf "String" und die Sperrung erfolgreich ist =>
            if (lpstrClipboardText) { 
              // Passwort in den Speicherblock schreiben.
              ::GetWindowText(GetDlgItem(IDC_PASSWORD), lpstrClipboardText, textlength); 
              // Wenn die Aufhebung der Sperrung nach dem Schreiben erfolgreich ist =>
              if (::GlobalUnlock(hClipboardText))
                // Daten im Text-Format ins Clipboard schreiben.
                ::SetClipboardData(CF_TEXT, hClipboardText); 
            } 
          } 
          // Clipboard nach Gebrauch wieder Schliessen
          ::CloseClipboard(); 
        } 
      } 
    
      CloseDialog(wID); 
      return 0; 
    }
    

    Dringende Empfehlung MSDN zulegen bzw. nachschlagen.



  • Tipp : borland hat mal ne Win32.hlp veröffentlicht. Die musste nur mal suchen.
    Ist zwar afaik nur die API von Win9x/NT 4.0, aber alles drin beschrieben. Ist immer das Erste, was meine IDE als Zusatz bekommt...



  • Vielen Dank euch allen, werd das mal ausprobieren 🙂


Anmelden zum Antworten