[MFC] Cum aflu cand se schimba continutul clopboard-ului?

Despre MFC, ATL si alte biblioteci C++ de la Microsoft (forum moderat)
Post Reply
User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3778
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

[MFC] Cum aflu cand se schimba continutul clopboard-ului?

Post by Ovidiu Cucu » 05 Sep 2010, 16:16

Intrebare: Cum aflu cand se schimba continutul clopboard-ului?

Raspuns: In primul rand trebuie apelata functia SetClipboardViewer pentru a adauga fereastra in lantul de clipboard viewers.
In felul acesta, va primi mesajul WM_DRAWCLIPBOARD de fiecare data cand se schimba continutul clipboard-ului.
Acest mesaj trebuie trimis ferestrei urmatoare din lantul de viewers (handle-ul intors de SetClipboardViewer).
De asemenea, trebuie tratat mesajul WM_CHANGECBCHAIN pentru a seta viewer-ul urmator in cazul cand ce actual este distrus.
In fine, scoatem fereastra din lantul de clipboard viewers atunci cand nu mai este nevoie, de regula pe WM_DESTROY.

Exemplu

Code: Select all

class CSimpleClipboardViewer : public CEditView
{
   HWND m_hWndClipNext;     // keeps next window in the clipboard viewers chain
   BOOL m_bClipViewStarted; // skips clipboard change message when this window is added to the chain
//...
}

Code: Select all

CSimpleClipboardViewer::CSimpleClipboardViewer() 
   : m_hWndClipNext(NULL),
     m_bClipViewStarted(FALSE)
{
}

Code: Select all

int CSimpleClipboardViewer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
   if (CEditView::OnCreate(lpCreateStruct) == -1)
      return -1;
	
   // Add this window to the clipboard viewer chain.
   m_hWndClipNext = SetClipboardViewer();
	
   return 0;
}

Code: Select all

void CSimpleClipboardViewer::OnDrawClipboard() 
{
   if(m_bClipViewStarted)
   {
      TRACE0("The contents of the Clipboard changes.\n");
   }
   m_bClipViewStarted = TRUE;

   // Send WM_DRAWCLIPBOARD to the next window in the chain.
   if(::IsWindow(m_hWndClipNext))
   {
      ::SendMessage(m_hWndClipNext, WM_DRAWCLIPBOARD, 0, 0);
   }
}

Code: Select all

void CSimpleClipboardViewer::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter) 
{
   // If hWndRemove is the next window in the chain, 
   // the window specified by hWndAfter becomes the next window.
   if(m_hWndClipNext == hWndRemove)
   {
      m_hWndClipNext = hWndAfter;
   }
   
   // Send WM_CHANGECBCHAIN to the next window in the chain.
   if(::IsWindow(m_hWndClipNext))
   {
      ::SendMessage(m_hWndClipNext, WM_CHANGECBCHAIN, (WPARAM)hWndRemove, (LPARAM)hWndAfter);
   }
}

Code: Select all

void CSimpleClipboardViewer::OnDestroy() 
{
   // Remove this window from the clipboard viewers chain.
   ChangeClipboardChain(m_hWndClipNext);
   CEditView::OnDestroy();
}
Vezi si << Back to MFC index



Post Reply