[转] 关于响应 WM_DRAWCLIPBOARD

这部分代码监视剪贴板中数据格式,只要剪贴板中数据变化,就在标题栏上显示出所有可识别的数据格式

 

  1   //   ClipSpyDlg.h   :   header   file   
  2   //   
  3     
  4   #if   !defined(AFX_CLIPSPYDLG_H_INCLUDED_)   
  5   #define   AFX_CLIPSPYDLG_H_INCLUDED_   
  6     
  7   #if   _MSC_VER   >   1000   
  8   #pragma   once   
  9   #endif   //   _MSC_VER   >   1000   
 10     
 11   /////////////////////////////////////////////////////////////////////////////   
 12   //   CClipSpyDlg   dialog   
 13     
 14   class   CClipSpyDlg   :   public   CDialog   
 15   {   
 16   //   Construction   
 17   public:   
 18   CClipSpyDlg(CWnd*   pParent   =   NULL); //   standard   constructor   
 19     
 20   //   Dialog   Data   
 21   //{{AFX_DATA(CClipSpyDlg)   
 22   enum   {   IDD   =   IDD_CLIPSPY_DIALOG   };   
 23   //   NOTE:   the   ClassWizard   will   add   data   members   here   
 24   //}}AFX_DATA   
 25     
 26   //   ClassWizard   generated   virtual   function   overrides   
 27   //{{AFX_VIRTUAL(CClipSpyDlg)   
 28   protected:   
 29   virtual   void   DoDataExchange(CDataExchange*   pDX); //   DDX/DDV   support   
 30   //}}AFX_VIRTUAL   
 31     
 32   //   Implementation   
 33   protected:   
 34   HICON   m_hIcon;   
 35     
 36   //   Generated   message   map   functions   
 37   //{{AFX_MSG(CClipSpyDlg)   
 38   virtual   BOOL   OnInitDialog();   
 39   afx_msg   void   OnPaint();   
 40   afx_msg   HCURSOR   OnQueryDragIcon();   
 41   afx_msg   void   OnDrawClipboard();   
 42   afx_msg   void   OnDestroy();   
 43   afx_msg   void   OnChangeCbChain(HWND   hWndRemove,   HWND   hWndAfter);   
 44   //}}AFX_MSG   
 45   DECLARE_MESSAGE_MAP()   
 46   private:   
 47   HWND   m_hWndNextCBViewer;   
 48   };   
 49     
 50   //{{AFX_INSERT_LOCATION}}   
 51   //   Microsoft   Visual   C++   will   insert   additional   declarations   immediately   before   the   previous   line.   
 52     
 53   #endif   //   !defined(AFX_CLIPSPYDLG_H_INCLUDED_)   
 54     
 55   //////////////////////////////////////////////////////////////////////////////////   
 56   //   ClipSpyDlg.cpp   :   implementation   file   
 57   //   
 58     
 59   #include   "stdafx.h"   
 60   #include   "ClipSpy.h"   
 61   #include   "ClipSpyDlg.h"   
 62     
 63   #ifdef   _DEBUG   
 64   #define   new   DEBUG_NEW   
 65   #undef   THIS_FILE   
 66   static   char   THIS_FILE[]   =   __FILE__;   
 67   #endif   
 68     
 69   /////////////////////////////////////////////////////////////////////////////   
 70   //   CClipSpyDlg   dialog   
 71     
 72   CClipSpyDlg::CClipSpyDlg(CWnd*   pParent   /*=NULL*/)   
 73   :   CDialog(CClipSpyDlg::IDD,   pParent)   
 74   {   
 75   //{{AFX_DATA_INIT(CClipSpyDlg)   
 76   //   NOTE:   the   ClassWizard   will   add   member   initialization   here   
 77   //}}AFX_DATA_INIT   
 78   //   Note   that   LoadIcon   does   not   require   a   subsequent   DestroyIcon   in   Win32   
 79   m_hIcon   =   AfxGetApp()->LoadIcon(IDR_MAINFRAME);   
 80     
 81   //TODO:   变量初始化   
 82   m_hWndNextCBViewer=NULL;   
 83   }   
 84     
 85   void   CClipSpyDlg::DoDataExchange(CDataExchange*   pDX)   
 86   {   
 87   CDialog::DoDataExchange(pDX);   
 88   //{{AFX_DATA_MAP(CClipSpyDlg)   
 89   //   NOTE:   the   ClassWizard   will   add   DDX   and   DDV   calls   here   
 90   //}}AFX_DATA_MAP   
 91   }   
 92     
 93   BEGIN_MESSAGE_MAP(CClipSpyDlg,   CDialog)   
 94   //{{AFX_MSG_MAP(CClipSpyDlg)   
 95   ON_WM_PAINT()   
 96   ON_WM_QUERYDRAGICON()   
 97   ON_WM_DRAWCLIPBOARD()   
 98   ON_WM_DESTROY()   
 99   ON_WM_CHANGECBCHAIN()   
100   //}}AFX_MSG_MAP   
101   END_MESSAGE_MAP()   
102     
103   /////////////////////////////////////////////////////////////////////////////   
104   //   CClipSpyDlg   message   handlers   
105     
106   BOOL   CClipSpyDlg::OnInitDialog()   
107   {   
108   CDialog::OnInitDialog();   
109     
110   //   Set   the   icon   for   this   dialog.     The   framework   does   this   automatically   
111   //     when   the   application's   main   window   is   not   a   dialog   
112   SetIcon(m_hIcon,   TRUE); //   Set   big   icon   
113   SetIcon(m_hIcon,   FALSE); //   Set   small   icon   
114     
115   //TODO:   将对话框加入剪贴板监视窗口链中   
116   m_hWndNextCBViewer=SetClipboardViewer();   
117     
118   return   TRUE;     //   return   TRUE     unless   you   set   the   focus   to   a   control   
119   }   
120     
121   //   If   you   add   a   minimize   button   to   your   dialog,   you   will   need   the   code   below   
122   //     to   draw   the   icon.     For   MFC   applications   using   the   document/view   model,   
123   //     this   is   automatically   done   for   you   by   the   framework.   
124     
125   void   CClipSpyDlg::OnPaint()     
126   {   
127   if   (IsIconic())   
128   {   
129   CPaintDC   dc(this);   //   device   context   for   painting   
130     
131   SendMessage(WM_ICONERASEBKGND,   (WPARAM)   dc.GetSafeHdc(),   0);   
132     
133   //   Center   icon   in   client   rectangle   
134   int   cxIcon   =   GetSystemMetrics(SM_CXICON);   
135   int   cyIcon   =   GetSystemMetrics(SM_CYICON);   
136   CRect   rect;   
137   GetClientRect(&rect);   
138   int   x   =   (rect.Width()   -   cxIcon   +   1)   /   2;   
139   int   y   =   (rect.Height()   -   cyIcon   +   1)   /   2;   
140     
141   //   Draw   the   icon   
142   dc.DrawIcon(x,   y,   m_hIcon);   
143   }   
144   else   
145   {   
146   CDialog::OnPaint();   
147   }   
148   }   
149     
150   //   The   system   calls   this   to   obtain   the   cursor   to   display   while   the   user   drags   
151   //     the   minimized   window.   
152   HCURSOR   CClipSpyDlg::OnQueryDragIcon()   
153   {   
154   return   (HCURSOR)   m_hIcon;   
155   }   
156     
157   void   CClipSpyDlg::OnDestroy()     
158   {   
159   //TODO:   从监视链中删除本监视窗口   
160   ChangeClipboardChain(   m_hWndNextCBViewer   );   
161     
162   CDialog::OnDestroy();   
163   }   
164     
165   void   CClipSpyDlg::OnDrawClipboard()     
166   {   
167   //TODO:   剪贴板内容发生变化   
168   CString   sText("ClipSpy-");   
169   UINT   CBFormat[]={   
170   CF_BITMAP,   
171   CF_DIB,   
172   CF_DIF,   
173   CF_DSPBITMAP,   
174   CF_DSPENHMETAFILE,   
175   CF_DSPMETAFILEPICT,   
176   CF_DSPTEXT,   
177   CF_ENHMETAFILE,   
178   CF_HDROP,   
179   CF_LOCALE,   
180   CF_METAFILEPICT,   
181   CF_OEMTEXT,   
182   CF_OWNERDISPLAY,   
183   CF_PALETTE,   
184   CF_PENDATA,   
185   CF_RIFF,   
186   CF_SYLK,   
187   CF_TEXT,   
188   CF_WAVE,   
189   CF_TIFF   
190   };   
191   char   *FormatName[]={   
192   "CF_BITMAP",   
193   "CF_DIB",   
194   "CF_DIF",   
195   "CF_DSPBITMAP",   
196   "CF_DSPENHMETAFILE",   
197   "CF_DSPMETAFILEPICT",   
198   "CF_DSPTEXT",   
199   "CF_ENHMETAFILE",   
200   "CF_HDROP",   
201   "CF_LOCALE",   
202   "CF_METAFILEPICT",   
203   "CF_OEMTEXT",   
204   "CF_OWNERDISPLAY",   
205   "CF_PALETTE",   
206   "CF_PENDATA",   
207   "CF_RIFF",   
208   "CF_SYLK",   
209   "CF_TEXT",   
210   "CF_WAVE",   
211   "CF_TIFF",   
212   "未知格式或剪贴板中无数据"   
213   };   
214     
215   //打开剪贴板查询格式类型   
216   if(   OpenClipboard()   )   
217   {   
218   CString   sCBAvailableFormat("");   
219   int   nFormatNum=sizeof(CBFormat)/sizeof(UINT);   
220     
221   //枚举类型   
222   for(int   i=0;i<nFormatNum;i++)   
223   {   
224   if(   ::IsClipboardFormatAvailable(   CBFormat[i]   )   )   
225   {   
226   if(   sCBAvailableFormat.IsEmpty()   )   
227   sCBAvailableFormat+=FormatName[i];   
228   else   
229   sCBAvailableFormat=sCBAvailableFormat+"   &   "+FormatName[i];   
230   }   
231   }   
232     
233   if(   !sCBAvailableFormat.IsEmpty()   )   
234   sText+=sCBAvailableFormat;//列出有效格式   
235   else   
236   sText+=FormatName[i];//该格式未知   
237     
238   ::CloseClipboard();   
239   }   
240     
241   //在标题栏中显示格式   
242   SetWindowText(   sText   );   
243     
244   //传WM_DRAWCLIPBOARD给下一个监视窗口   
245   ::SendMessage(m_hWndNextCBViewer,WM_DRAWCLIPBOARD,0,0);   
246   }   
247     
248   void   CClipSpyDlg::OnChangeCbChain(HWND   hWndRemove,   HWND   hWndAfter)     
249   {   
250   //   TODO:   监视链中窗口发生变动   
251   if(   m_hWndNextCBViewer==hWndRemove   )   
252   m_hWndNextCBViewer=hWndAfter;   
253     
254   //传WM_CHANGECBCHAIN给下一个监视窗口   
255   ::SendMessage(m_hWndNextCBViewer,WM_CHANGECBCHAIN,   
256   (WPARAM)hWndRemove,(LPARAM)hWndAfter);   
257   }   

 

 

 

posted @ 2012-07-09 13:05  海螺一枚  阅读(891)  评论(0编辑  收藏  举报