关于响应 WM_DRAWCLIPBOARD

此代码实现了一个对话框类 CClipSpyDlg,用于监视剪贴板内容的变化。当剪贴板数据变化时,它会在标题栏上显示所有可识别的数据格式。代码包括了 OnInitDialog、OnPaint、OnDrawClipboard 和其他消息处理函数,用于处理剪贴板事件和窗口操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这部分代码监视剪贴板中数据格式,只要剪贴板中数据变化,就在标题栏上显示出所有可识别的数据格式  
  下面是所有该部分的代码,我的至少在Release后可运行,不知能不能解决你的问题  
   
  //   ClipSpyDlg.h   :   header   file  
  //  
   
  #if   !defined(AFX_CLIPSPYDLG_H_INCLUDED_)  
  #define   AFX_CLIPSPYDLG_H_INCLUDED_  
   
  #if   _MSC_VER   >   1000  
  #pragma   once  
  #endif   //   _MSC_VER   >   1000  
   
  /  
  //   CClipSpyDlg   dialog  
   
  class   CClipSpyDlg   :   public   CDialog  
  {  
  //   Construction  
  public:  
  CClipSpyDlg(CWnd*   pParent   =   NULL); //   standard   constructor  
   
  //   Dialog   Data  
  //{{AFX_DATA(CClipSpyDlg)  
  enum   {   IDD   =   IDD_CLIPSPY_DIALOG   };  
  //   NOTE:   the   ClassWizard   will   add   data   members   here  
  //}}AFX_DATA  
   
  //   ClassWizard   generated   virtual   function   overrides  
  //{{AFX_VIRTUAL(CClipSpyDlg)  
  protected:  
  virtual   void   DoDataExchange(CDataExchange*   pDX); //   DDX/DDV   support  
  //}}AFX_VIRTUAL  
   
  //   Implementation  
  protected:  
  HICON   m_hIcon;  
   
  //   Generated   message   map   functions  
  //{{AFX_MSG(CClipSpyDlg)  
  virtual   BOOL   OnInitDialog();  
  afx_msg   void   OnPaint();  
  afx_msg   HCURSOR   OnQueryDragIcon();  
  afx_msg   void   OnDrawClipboard();  
  afx_msg   void   OnDestroy();  
  afx_msg   void   OnChangeCbChain(HWND   hWndRemove,   HWND   hWndAfter);  
  //}}AFX_MSG  
  DECLARE_MESSAGE_MAP()  
  private:  
  HWND   m_hWndNextCBViewer;  
  };  
   
  //{{AFX_INSERT_LOCATION}}  
  //   Microsoft   Visual   C++   will   insert   additional   declarations   immediately   before   the   previous   line.  
   
  #endif   //   !defined(AFX_CLIPSPYDLG_H_INCLUDED_)  
   
  //  
  //   ClipSpyDlg.cpp   :   implementation   file  
  //  
   
  #include   "stdafx.h"  
  #include   "ClipSpy.h"  
  #include   "ClipSpyDlg.h"  
   
  #ifdef   _DEBUG  
  #define   new   DEBUG_NEW  
  #undef   THIS_FILE  
  static   char   THIS_FILE[]   =   __FILE__;  
  #endif  
   
  /  
  //   CClipSpyDlg   dialog  
   
  CClipSpyDlg::CClipSpyDlg(CWnd*   pParent   /*=NULL*/)  
  :   CDialog(CClipSpyDlg::IDD,   pParent)  
  {  
  //{{AFX_DATA_INIT(CClipSpyDlg)  
  //   NOTE:   the   ClassWizard   will   add   member   initialization   here  
  //}}AFX_DATA_INIT  
  //   Note   that   LoadIcon   does   not   require   a   subsequent   DestroyIcon   in   Win32  
  m_hIcon   =   AfxGetApp()->LoadIcon(IDR_MAINFRAME);  
   
  //TODO:   变量初始化  
  m_hWndNextCBViewer=NULL;  
  }  
   
  void   CClipSpyDlg::DoDataExchange(CDataExchange*   pDX)  
  {  
  CDialog::DoDataExchange(pDX);  
  //{{AFX_DATA_MAP(CClipSpyDlg)  
  //   NOTE:   the   ClassWizard   will   add   DDX   and   DDV   calls   here  
  //}}AFX_DATA_MAP  
  }  
   
  BEGIN_MESSAGE_MAP(CClipSpyDlg,   CDialog)  
  //{{AFX_MSG_MAP(CClipSpyDlg)  
  ON_WM_PAINT()  
  ON_WM_QUERYDRAGICON()  
  ON_WM_DRAWCLIPBOARD()  
  ON_WM_DESTROY()  
  ON_WM_CHANGECBCHAIN()  
  //}}AFX_MSG_MAP  
  END_MESSAGE_MAP()  
   
  /  
  //   CClipSpyDlg   message   handlers  
   
  BOOL   CClipSpyDlg::OnInitDialog()  
  {  
  CDialog::OnInitDialog();  
   
  //   Set   the   icon   for   this   dialog.     The   framework   does   this   automatically  
  //     when   the   application's   main   window   is   not   a   dialog  
  SetIcon(m_hIcon,   TRUE); //   Set   big   icon  
  SetIcon(m_hIcon,   FALSE); //   Set   small   icon  
   
  //TODO:   将对话框加入剪贴板监视窗口链中  
  m_hWndNextCBViewer=SetClipboardViewer();  
   
  return   TRUE;     //   return   TRUE     unless   you   set   the   focus   to   a   control  
  }  
   
  //   If   you   add   a   minimize   button   to   your   dialog,   you   will   need   the   code   below  
  //     to   draw   the   icon.     For   MFC   applications   using   the   document/view   model,  
  //     this   is   automatically   done   for   you   by   the   framework.  
   
  void   CClipSpyDlg::OnPaint()    
  {  
  if   (IsIconic())  
  {  
  CPaintDC   dc(this);   //   device   context   for   painting  
   
  SendMessage(WM_ICONERASEBKGND,   (WPARAM)   dc.GetSafeHdc(),   0);  
   
  //   Center   icon   in   client   rectangle  
  int   cxIcon   =   GetSystemMetrics(SM_CXICON);  
  int   cyIcon   =   GetSystemMetrics(SM_CYICON);  
  CRect   rect;  
  GetClientRect(&rect);  
  int   x   =   (rect.Width()   -   cxIcon   +   1)   /   2;  
  int   y   =   (rect.Height()   -   cyIcon   +   1)   /   2;  
   
  //   Draw   the   icon  
  dc.DrawIcon(x,   y,   m_hIcon);  
  }  
  else  
  {  
  CDialog::OnPaint();  
  }  
  }  
   
  //   The   system   calls   this   to   obtain   the   cursor   to   display   while   the   user   drags  
  //     the   minimized   window.  
  HCURSOR   CClipSpyDlg::OnQueryDragIcon()  
  {  
  return   (HCURSOR)   m_hIcon;  
  }  
   
  void   CClipSpyDlg::OnDestroy()    
  {  
  //TODO:   从监视链中删除本监视窗口  
  ChangeClipboardChain(   m_hWndNextCBViewer   );  
   
  CDialog::OnDestroy();  
  }  
   
  void   CClipSpyDlg::OnDrawClipboard()    
  {  
  //TODO:   剪贴板内容发生变化  
  CString   sText("ClipSpy-");  
  UINT   CBFormat[]={  
  CF_BITMAP,  
  CF_DIB,  
  CF_DIF,  
  CF_DSPBITMAP,  
  CF_DSPENHMETAFILE,  
  CF_DSPMETAFILEPICT,  
  CF_DSPTEXT,  
  CF_ENHMETAFILE,  
  CF_HDROP,  
  CF_LOCALE,  
  CF_METAFILEPICT,  
  CF_OEMTEXT,  
  CF_OWNERDISPLAY,  
  CF_PALETTE,  
  CF_PENDATA,  
  CF_RIFF,  
  CF_SYLK,  
  CF_TEXT,  
  CF_WAVE,  
  CF_TIFF  
  };  
  char   *FormatName[]={  
  "CF_BITMAP",  
  "CF_DIB",  
  "CF_DIF",  
  "CF_DSPBITMAP",  
  "CF_DSPENHMETAFILE",  
  "CF_DSPMETAFILEPICT",  
  "CF_DSPTEXT",  
  "CF_ENHMETAFILE",  
  "CF_HDROP",  
  "CF_LOCALE",  
  "CF_METAFILEPICT",  
  "CF_OEMTEXT",  
  "CF_OWNERDISPLAY",  
  "CF_PALETTE",  
  "CF_PENDATA",  
  "CF_RIFF",  
  "CF_SYLK",  
  "CF_TEXT",  
  "CF_WAVE",  
  "CF_TIFF",  
  "未知格式或剪贴板中无数据"  
  };  
   
  //打开剪贴板查询格式类型  
  if(   OpenClipboard()   )  
  {  
  CString   sCBAvailableFormat("");  
  int   nFormatNum=sizeof(CBFormat)/sizeof(UINT);  
   
  //枚举类型  
  for(int   i=0;i<nFormatNum;i++)  
  {  
  if(   ::IsClipboardFormatAvailable(   CBFormat[i]   )   )  
  {  
  if(   sCBAvailableFormat.IsEmpty()   )  
  sCBAvailableFormat+=FormatName[i];  
  else  
  sCBAvailableFormat=sCBAvailableFormat+"   &   "+FormatName[i];  
  }  
  }  
   
  if(   !sCBAvailableFormat.IsEmpty()   )  
  sText+=sCBAvailableFormat;//列出有效格式  
  else  
  sText+=FormatName[i];//该格式未知  
   
  ::CloseClipboard();  
  }  
   
  //在标题栏中显示格式  
  SetWindowText(   sText   );  
   
  //传WM_DRAWCLIPBOARD给下一个监视窗口  
  ::SendMessage(m_hWndNextCBViewer,WM_DRAWCLIPBOARD,0,0);  
  }  
   
  void   CClipSpyDlg::OnChangeCbChain(HWND   hWndRemove,   HWND   hWndAfter)    
  {  
  //   TODO:   监视链中窗口发生变动  
  if(   m_hWndNextCBViewer==hWndRemove   )  
  m_hWndNextCBViewer=hWndAfter;  
   
  //传WM_CHANGECBCHAIN给下一个监视窗口  
  ::SendMessage(m_hWndNextCBViewer,WM_CHANGECBCHAIN,  
  (WPARAM)hWndRemove,(LPARAM)hWndAfter);  
  }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值