当前位置: 首页 > news >正文

中国万网注册网站电商网站建设怎么样

中国万网注册网站,电商网站建设怎么样,网站买卖交易平台,seo建站推广前言#xff1a; 最近正好写一个程序#xff0c;需要操作剪切板 功能很简单#xff0c;只需要从剪切板内读取字符串#xff0c;然后清空剪切板#xff0c;然后再把字符串导入剪切板 我想当然的使用我最拿手的C#来完成这项工作#xff0c;原因无他#xff0c;因为.Net框架…前言 最近正好写一个程序需要操作剪切板 功能很简单只需要从剪切板内读取字符串然后清空剪切板然后再把字符串导入剪切板 我想当然的使用我最拿手的C#来完成这项工作原因无他因为.Net框架封装了能实现这种功能的方法 然后就有了如下代码 1 string Temp ;2 while (true)3 {4 string Tex Clipboard.GetText().ToString();5 if (!string.IsNullOrWhiteSpace(Tex) Temp ! Tex)6 {7 Clipboard.Clear();8 Clipboard.SetDataObject(Tex, false);9 Temp Tex; 10 } 11 Thread.Sleep(1); 12 } View Code 这段代码也是网页上广泛流传的使用.Net框架操作系统剪切板的方法当然这个方法在某些情况下很管用 不过在我这确发生了点问题主要的问题有两点 首先我对剪切板的操作需求有实时性也就是操作人员复制的一瞬间就应该截取到剪切板的数据处理完后再放入剪切板 结果 Clipboard.SetDataObject(Tex, false); 没想到上面这条设置剪切板的指令竟然会卡焦点窗口的线程比如说我在A软件执行了一次复制操作如果使用了上述代码那么A软件强制线程堵塞大概几百毫秒的样子反正很影响体验我推测是因为该命令会锁定内存导致的 那怎么办本着死马当活马医的态度我专门为该指令启用了一个线程 Task.Factory.StartNew(() {Clipboard.Clear();Clipboard.SetDataObject(Text, false);}); 使用了线程以后因为操作滞后(线程启动会延迟一会儿并不实时)了所以上述问题似乎解决了但是没想到出现了新的问题 string Tex Clipboard.GetText().ToString(); 上述从剪切板获得字符串的指令在默写情况下会卡滞住然后程序在一分钟之后因为超时而被系统吊销 emmmmm在经过几番努力之后我终于意识到虽然.Net封装了不少操作系统API的方法使得一些IO操作变简单不少但是带来的问题也是同样大的在遇到无法解决的问题的时候会有点束手无策 于是不得已我只能放弃使用过C#完成该项功能想着幸好功能简单而且操作WinAPI其实最好的还是使用C来写于是我用C复现了上述功能 1 #include stdafx.h2 #include windows.h3 #include iostream4 using namespace std;5 #pragma comment(linker,/subsystem:windows /entry:mainCRTStartup)6 7 int main(int argc, _TCHAR* argv[])8 {9 HANDLE THandle GlobalAlloc(GMEM_FIXED, 1000);//分配内存 10 char* Temp (char*)THandle;//锁定内存返回申请内存的首地址 11 while (true) 12 { 13 HWND hWnd NULL; 14 OpenClipboard(hWnd);//打开剪切板 15 if (IsClipboardFormatAvailable(CF_TEXT)) 16 { 17 HANDLE h GetClipboardData(CF_TEXT);//获取剪切板数据 18 char* p (char*)GlobalLock(h); 19 GlobalUnlock(h); 20 if (strcmp(Temp, p)) 21 { 22 EmptyClipboard();//清空剪切板 23 HANDLE hHandle GlobalAlloc(GMEM_FIXED, 1000);//分配内存 24 char* pData (char*)GlobalLock(hHandle);//锁定内存返回申请内存的首地址 25 strcpy(pData, p); 26 strcpy(Temp, p); 27 SetClipboardData(CF_TEXT, hHandle);//设置剪切板数据 28 GlobalUnlock(hHandle);//解除锁定 29 } 30 } 31 CloseClipboard();//关闭剪切板 32 Sleep(500); 33 } 34 return 0; 35 } View Code 不愧是C使用上述代码后完美实现我需要的功能而且不管是主程序还是我写的这个程序都不会出现卡滞或者不工作的情况了真是可喜可贺。 那么本教程就到此为止。     以下是正文 想着既然我能用C调用WinAPI完美实现我需要的功能而且C#也能调用非托管的代码来执行WinAPI那么我不是可以把上面C写的代码移植到C#里面执行说干就干 首先C#调用WinAPI需要先申明 [DllImport(User32)]internal static extern bool OpenClipboard(IntPtr hWndNewOwner);[DllImport(User32)]internal static extern bool CloseClipboard();[DllImport(User32)]internal static extern bool EmptyClipboard();[DllImport(User32)]internal static extern bool IsClipboardFormatAvailable(int format);[DllImport(User32)]internal static extern IntPtr GetClipboardData(int uFormat);[DllImport(User32, CharSet CharSet.Unicode)]internal static extern IntPtr SetClipboardData(int uFormat, IntPtr hMem); 操作剪切板需要调用的API大致就上面这些 有了API以后我们还需要自己手动封装方法 internal static void SetText(string text){if (!OpenClipboard(IntPtr.Zero))        {        SetText(text);        return;        }EmptyClipboard();SetClipboardData(13, Marshal.StringToHGlobalUni(text));CloseClipboard();}internal static string GetText(int format){string value string.Empty;OpenClipboard(IntPtr.Zero);if (IsClipboardFormatAvailable(format)){IntPtr ptr NativeMethods.GetClipboardData(format);if (ptr ! IntPtr.Zero){value Marshal.PtrToStringUni(ptr);}}CloseClipboard();return value;} 我们也就用到两个方法从剪切板获得文本和设置文本到剪切板哦关于SetClipboardData的第一个参数13是怎么来的问题其实这个剪切板的格式参数下面有一张表就是自从这里来的 public static class ClipboardFormat {/// summary/// Text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals/// the end of the data. Use this format for ANSI text./// /summarypublic const int CF_TEXT 1;/// summary/// A handle to a bitmap (cHBITMAP/c)./// /summarypublic const int CF_BITMAP 2;/// summary/// Handle to a metafile picture format as defined by the cMETAFILEPICT/c structure. When passing a/// cCF_METAFILEPICT/c handle by means of DDE, the application responsible for deleting chMem/c should/// also free the metafile referred to by the cCF_METAFILEPICT/c handle./// /summarypublic const int CF_METAFILEPICT 3;/// summary/// Microsoft Symbolic Link (SYLK) format./// /summarypublic const int CF_SYLK 4;/// summary/// Software Arts Data Interchange Format./// /summarypublic const int CF_DIF 5;/// summary/// Tagged-image file format./// /summarypublic const int CF_TIFF 6;/// summary/// Text format containing characters in the OEM character set. Each line ends with a carriage return/linefeed/// (CR-LF) combination. A null character signals the end of the data./// /summarypublic const int CF_OEMTEXT 7;/// summary/// A memory object containing a cBITMAPINFO/c structure followed by the bitmap bits./// /summarypublic const int CF_DIB 8;/// summary/// Handle to a color palette. Whenever an application places data in the clipboard that depends on or assumes/// a color palette, it should place the palette on the clipboard as well. If the clipboard contains data in/// the see crefCF_PALETTE/ (logical color palette) format, the application should use the/// cSelectPalette/c and cRealizePalette/c functions to realize (compare) any other data in the/// clipboard against that logical palette. When displaying clipboard data, the clipboard always uses as its/// current palette any object on the clipboard that is in the cCF_PALETTE/c format./// /summarypublic const int CF_PALETTE 9;/// summary/// Data for the pen extensions to the Microsoft Windows for Pen Computing./// /summarypublic const int CF_PENDATA 10;/// summary/// Represents audio data more complex than can be represented in a CF_WAVE standard wave format./// /summarypublic const int CF_RIFF 11;/// summary/// Represents audio data in one of the standard wave formats, such as 11 kHz or 22 kHz PCM./// /summarypublic const int CF_WAVE 12;/// summary/// Unicode text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character/// signals the end of the data./// /summarypublic const int CF_UNICODETEXT 13;/// summary/// A handle to an enhanced metafile (cHENHMETAFILE/c)./// /summarypublic const int CF_ENHMETAFILE 14;/// summary/// A handle to type cHDROP/c that identifies a list of files. An application can retrieve information/// about the files by passing the handle to the cDragQueryFile/c function./// /summarypublic const int CF_HDROP 15;/// summary/// The data is a handle to the locale identifier associated with text in the clipboard. When you close the/// clipboard, if it contains cCF_TEXT/c data but no cCF_LOCALE/c data, the system automatically sets/// the cCF_LOCALE/c format to the current input language. You can use the cCF_LOCALE/c format to/// associate a different locale with the clipboard text./// An application that pastes text from the clipboard can retrieve this format to determine which character/// set was used to generate the text./// Note that the clipboard does not support plain text in multiple character sets. To achieve this, use a/// formatted text data type such as RTF instead. /// The system uses the code page associated with cCF_LOCALE/c to implicitly convert from/// see crefCF_TEXT/ to see crefCF_UNICODETEXT/. Therefore, the correct code page table is used for/// the conversion./// /summarypublic const int CF_LOCALE 16;/// summary/// A memory object containing a cBITMAPV5HEADER/c structure followed by the bitmap color space/// information and the bitmap bits./// /summarypublic const int CF_DIBV5 17;/// summary/// Owner-display format. The clipboard owner must display and update the clipboard viewer window, and receive/// the see crefClipboardMessages.WM_ASKCBFORMATNAME/, see crefClipboardMessages.WM_HSCROLLCLIPBOARD/,/// see crefClipboardMessages.WM_PAINTCLIPBOARD/, see crefClipboardMessages.WM_SIZECLIPBOARD/, and/// see crefClipboardMessages.WM_VSCROLLCLIPBOARD/ messages. The chMem/c parameter must be cnull/c./// /summarypublic const int CF_OWNERDISPLAY 0x0080;/// summary/// Text display format associated with a private format. The chMem/c parameter must be a handle to data/// that can be displayed in text format in lieu of the privately formatted data./// /summarypublic const int CF_DSPTEXT 0x0081;/// summary/// Bitmap display format associated with a private format. The chMem/c parameter must be a handle to/// data that can be displayed in bitmap format in lieu of the privately formatted data./// /summarypublic const int CF_DSPBITMAP 0x0082;/// summary/// Metafile-picture display format associated with a private format. The chMem/c parameter must be a/// handle to data that can be displayed in metafile-picture format in lieu of the privately formatted data./// /summarypublic const int CF_DSPMETAFILEPICT 0x0083;/// summary/// Enhanced metafile display format associated with a private format. The chMem/c parameter must be a/// handle to data that can be displayed in enhanced metafile format in lieu of the privately formatted data./// /summarypublic const int CF_DSPENHMETAFILE 0x008E;/// summary/// Start of a range of integer values for application-defined GDI object clipboard formats. The end of the/// range is see crefCF_GDIOBJLAST/. Handles associated with clipboard formats in this range are not/// automatically deleted using the cGlobalFree/c function when the clipboard is emptied. Also, when using/// values in this range, the chMem/c parameter is not a handle to a GDI object, but is a handle allocated/// by the cGlobalAlloc/c function with the cGMEM_MOVEABLE/c flag./// /summarypublic const int CF_GDIOBJFIRST 0x0300;/// summary/// See see crefCF_GDIOBJFIRST/./// /summarypublic const int CF_GDIOBJLAST 0x03FF;/// summary/// Start of a range of integer values for private clipboard formats. The range ends with/// see crefCF_PRIVATELAST/. Handles associated with private clipboard formats are not freed/// automatically; the clipboard owner must free such handles, typically in response to the/// see crefClipboardMessages.WM_DESTROYCLIPBOARD/ message./// /summarypublic const int CF_PRIVATEFIRST 0x0200;/// summary/// See see crefCF_PRIVATEFIRST/./// /summarypublic const int CF_PRIVATELAST 0x02FF; } View Code 在C里面是不用指定数字的只需要用CF_UNICODETEXT就行不过.Net里面应该没有对应的索引表所以只能手动输入(我这里是为了说明用才专门用数字自己代码那是索引的枚举类) 上面两个工作做完以后就能实现功能了功能代码如下 var LastS string.Empty;while (!CancelInfoClipboard.IsCancellationRequested){var Temp ClipboardControl.GetText(ClipboardFormat.CF_UNICODETEXT);if (!string.IsNullOrEmpty(Temp) Temp ! LastS){ClipboardControl.SetText(Temp);LastS Temp;}Thread.Sleep(50);} 是不是和最开始展示的调用.Net框架的方法一模一样(笑)不过使用底层API实现的功能就没有那么多乱七八糟的Bug了自己也很清楚到底实现了啥功能同时也收获了不少新知识(主要是非托管代码调用的时候的注意事项什么的还有向非托管代码传递数据的时候最好多用Marshal类里面的方法不然可能会出错毕竟这个类就是专门为非托管代码而设立的) 接下来是新的发现 在研究MSDN上面关于剪切板的API的时候发现了一个函数 bool AddClipboardFormatListener(HWND hwnd); 根据描述来讲是添加一个剪切板的监控在剪切板有任何变动的时候通知你所指定的句柄的窗口我一想这不就是我所需要的么有了这么一个API以后其实我上面所展示的使用死循环轮询剪切板的方法就变得很傻逼而且也很容易出错了于是基于这个新发现的API我重新更改了全部的程序逻辑反而比原先的实现更加简单了。 首先我们需要一个新的窗口或者控件来接收Windows消息更新后所发来的消息只要New 一个form就行 public Form2(){InitializeComponent();AddClipboardFormatListener(this.Handle);} 然后我们在初始化组件的命令后面把使用添加剪切板监听的API把当前窗口的句柄发给系统这样系统在接收到剪切板改变的命令后会把消息发给当前窗口 然后我们需要复写WndProc方法 protected override void WndProc(ref Message m){if (m.Msg 0x031D Onice){var Temp ClipboardControl.GetText(ClipboardFormat.CF_UNICODETEXT);if (!string.IsNullOrEmpty(Temp)){ClipboardControl.SetText(Temp);Onice false;}}else if (!Onice){Onice true;}else{base.WndProc(ref m);}}   private bool Onice true; 首先WndProc如果是Form类下面一个专门用来接收系统发送过来的消息的方法 然后关于m.Msg 0x031D的0x031D在WinAPI定义上的意义是WM_CLIPBOARDUPDATE 也就是剪切板更新事件这个通过查找MSDN能够找到 下面没有特别奇怪的函数就是有一点需要注意我们这里设置了剪切板数据相当于进行了一次更新所以会在这一瞬间再次产生剪切板更新事件然后又会通知这个方法然后就会形成死循环我在这里用了一个布尔判断来通过布尔状态决定是否截取剪切板不只有有没有更好的办法来实现 以上转载于:https://www.cnblogs.com/ACDIV/p/9114472.html
http://www.zqtcl.cn/news/245065/

相关文章:

  • 宁波网络推广制作seo关键词推广公司
  • 东莞市网站推广西安推广公司无网不胜
  • 全国网站建设有实力建筑人才网123
  • 海安网站设计公司网站开发好学嘛
  • 网站建设深圳公司上海贸易公司注册条件
  • 深圳市坪山新区建设局网站给别人做网站去掉版权
  • 怎么做监测网站的浏览量有没有专业做股指的评论网站
  • 济南微信网站开发网上效果代码网站可以下载吗
  • 门户网站的设计常见的管理信息系统有哪些
  • 网站添加悬浮二维码成都游戏网站开发
  • 用jquery做网站百度seo排名规则
  • 免备案手机网站室内设计说明
  • 网站被做站公司贩卖怎样将qq空间建设为个人网站
  • 网站开发有哪几类淮安app开发公司
  • 营销网站建设公司哪家好兵团第二师建设环保局网站
  • 做推广最好的网站是哪个深圳办公室装修招标
  • 郑州高端网站制作wordpress那个版本好
  • wordpress屏蔽右键f12奉化首页的关键词优化
  • cn域名做犯法网站做电影网站需要哪些证
  • 官方网站有哪些韶关做网站的公司
  • 商城网站设计公司怎么样网站制作预算
  • 在济南什么人想做网站网站建设后怎么做主页
  • 联合年检怎么做网站上国家备案查询
  • 社交网站wap模板wordpress网址导航插件
  • 沈阳快速建站公司有哪些国外做二手服装网站
  • 手机如何建立网站平台seo比较好的优化
  • 电商网站建设外包禅城南庄网站制作
  • 哈尔滨企业网站开发报价免费php网站源码
  • 东莞市公司网站建设淄博网站制作营销
  • 企业网站无线端怎么做手机网站做成app