2013-11-20 4 views
-1

클립 보드를 잘못 생각한 것 같습니다. 그러나 이것을 구현하는 방법을 알고 싶거나 팁을 얻고 싶습니다. 이 프로그램을 백그라운드에서 실행시키고 싶을 때마다 프로그램에서 끝나는 무언가를 복사 할 때마다. 그렇게하면 10 분 전에 내가 뭘 복사했는지 시간에 맞추어 다시보고 싶으면 프로그램에서 찾을 것입니다. 그래서 나는 그것을 텍스트 파일에 저장해야한다고 생각한다. 어떻게 구현할 수 있습니까?ctrl + c를 눌러 texbox에 저장하십시오.

+2

이 질문은 너무 지저분 :)) 자신을 삭제하는 것이 가장 좋은 방법이며, 두 번째로 좋은 방법은 그것을 개선하는 것입니다. –

+0

그 프로그램은 클립 보드에있는 내용을 지속적으로 폴링해야합니다. 또한 비 텍스트 콘텐츠를 준비해야합니다. 가능해야합니다. –

+0

이미 이렇게하는 일부 오픈 소스 프로그램을 확인하십시오. http://ditto-cp.sourceforge.net/ –

답변

1

클립 보드가 변경되면 일반적으로 C#이 이벤트를 발생시킬 수 없습니다. 클립 보드에서 데이터를 읽을 수 있으며, 클립 보드를 폴링하는 동안 기다릴 수는 있지만, 나에게 적합하지 않은 것으로 보입니다.

그러나 조금씩 extern 사용법을 사용하면 원하는 것을 얻을 수 있습니다. Form을 하위 클래스 클래스에서 :

/// <summary> 
/// Message id for data being copied to the clipboard 
/// </summary> 
/// <value>776</value> 
private const int WM_DRAWCLIPBOARD = 0x0308; 
/// <summary> 
/// Message id for a window being removed from the viewer chain 
/// </summary> 
/// <value>781</value> 
private const int WM_CHANGECBCHAIN = 0x030D; 
/// <summary> 
/// Message id for the window being destroyed 
/// </summary> 
/// <value>2</value> 
private const int WM_DESTROY = 0x0002; 
/// <summary> 
/// The next window in the clipboard viewer chain 
/// </summary> 
private IntPtr nextClipboardViewer; 

/// <summary> 
/// Adds the specified window to the chain of clipboard viewers. Clipboard viewer windows receive a <c>WM_DRAWCLIPBOARD</c> 
/// message whenever the content of the clipboard changes. 
/// </summary> 
/// <param name="hWnd">A handle to the window to be added to the clipboard chain.</param> 
/// <returns>If the function succeeds, the return value identifies the next window in the clipboard viewer chain. If an 
/// error occurs or there are no other windows in the clipboard viewer chain, the return value is <c>null</c>.</returns> 
[DllImport("User32.dll", CharSet = CharSet.Auto)] 
public static extern IntPtr SetClipboardViewer(IntPtr hWnd); 
/// <summary> 
/// Removes a specified window from the chain of clipboard viewers. 
/// </summary> 
/// <param name="hWndRemove">A handle to the window to be removed from the chain. The handle must have been passed to the 
/// <see cref="SetClipboardViewer"/> function.</param> 
/// <param name="hWndNewNext">A handle to the window that follows the <paramref name="hWndRemove"/> window in the clipboard 
/// viewer chain. (This is the handle returned by <see cref="SetClipboardViewer"/>, unless the sequence was changed in response 
/// to a <c>WM_CHANGECBCHAIN</c> message.)</param> 
/// <returns>The return value indicates the result of passing the <c>WM_CHANGECBCHAIN</c> message to the windows in the 
/// clipboard viewer chain. Because a window in the chain typically returns <c>false</c> when it processes <c>WM_CHANGECBCHAIN</c>, 
/// the return value from <see cref="ChangeClipboardChain"/> is typically <c>false</c>. If there is only one window in the chain, 
/// the return value is typically <c>true</c>.</returns> 
[DllImport("User32.dll", CharSet = CharSet.Auto)] 
public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext); 
/// <summary> 
/// Sends the specified message to a window or windows. The <c>SendMessage</c> function calls the window 
/// procedure for the specified window and does not return until the window procedure has processed the message. 
/// </summary> 
/// <param name="hwnd">A handle to the window whose window procedure will receive the message.</param> 
/// <param name="wMsg">The message to be sent.</param> 
/// <param name="wParam">Additional message-specific information.</param> 
/// <param name="lParam">Additional message-specific information.</param> 
/// <returns>The return value specifies the result of the message processing; it depends on the message sent.</returns> 
[DllImport("User32.dll", CharSet = CharSet.Auto)] 
public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); 

/// <inheritdoc/> 
protected override void WndProc(ref Message m) 
{ 
    if (m.Msg == WM_DRAWCLIPBOARD) 
    { 
     // The user copied something to the clipboard 
     IDataObject clipData = Clipboard.GetDataObject(); 
     if (clipData.GetDataPresent(DataFormats.Text)) 
     { 
      // Copied data is text 
     } 

     SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam); 
    } 
    // Handle necessary native clipboard stuff 
    else if (m.Msg == WM_DESTROY) 
    { 
     // Remove MyForm from the clipboard chain 
     ChangeClipboardChain(this.Handle, nextClipboardViewer); 
    } 
    else if (m.Msg == WM_CHANGECBCHAIN) 
    { 
     if (m.WParam == nextClipboardViewer) 
     { 
      nextClipboardViewer = m.LParam; 
     } 
     else 
     { 
      SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam); 
     } 
    } 
    else 
    { 
     base.WndProc(ref m); 
    } 
} 

private void MyForm_Load(object sender, EventArgs e) 
{ 
    // Include MyForm in the clipboard chain 
    nextClipboardViewer = SetClipboardViewer(this.Handle); 
} 

MyForm_Load (대부분 쉽게 디자이너 창에서)을 Form에 적절한 이벤트로 추가되어 있는지 확인합니다.

+1

작은 메모. WndProc()에서 WM_DESTROY를 트래핑하고 거기에있는 체인에서 자신을 제거하면 FormClosing() 이벤트를 구독 할 필요가 없습니다. –

+0

잘 알고 있습니다. 나는이 코드를 필자가 작성한 기존 응용 프로그램에서 가져와 관련없는 비트를 제거했다. 내 응용 프로그램에서는'_Load' 및'_FormClosing' _anyway_에서 다른 작업을하고 있습니다. WM_DESTROY의 가치는 무엇입니까? –

+0

'private const int WM_DESTROY = 0x2;' –

관련 문제