实例介绍
【实例简介】
接收手机端发送来的扫描信息,并发送到当前光标所在程序里面。还可以作为静态网页的的服务器
【实例截图】手机扫描枪电脑接收端源码,需与手机端配合使用
【核心代码】
function GetFocusHandle: HWND;
var
h: HWND;
c: array[0..1024*32] of Char;
idAttach: Cardinal;
begin
H := GetForegroundWindow;
idAttach := GetWindowThreadProcessId(h, nil);
if not AttachThreadInput(idAttach , GetCurrentThreadId(), True) then Exit;
Result := GetFocus;
AttachThreadInput(idAttach, GetCurrentThreadId(), false );
end;
procedure TWebModule1.HandleFileRequest(const FileName:string;Request:TWebRequest;
Response:TWebResponse);
var
FC,Ext:string;
FS:TFileStream;
SL:TStringList;
begin
{------------------------------------------------------------------------------
处理来自浏览器对静态页面文件的请求:
对于Standalone模式的WebBroker程序,这个默认Action可以用来当作一个WebServer的根:
如果请求的path是一个文件,则返回一个文件给客户端。这样,http://ip/WebBroker.exe/abc.html
就可以从这里返回。
------------------------------------------------------------------------------}
if FileExists(FileName)then
begin
//以下代码,测试通过。网页里面有图片链接。网页能够正确显示图片。
Ext:=LowerCase(ExtractFileExt(FileName));
if(Ext='.html')or(Ext='.htm')or(Ext='.css')or(Ext='.js')or(ext='.txt')then
begin
{-----------------------------------------------------------------------
下载文本文件给浏览器,不能将文本文件加载为字符串然后用字符串返回,
而是应该直接用Stream来返回。用字符串返回,可能会有问题。估计原因是文件里面的字符串有些字符的编码有问题导致。
----------------------------------------------------------------------}
FS:=TFileStream.Create(FileName,fmOpenRead);
Response.ContentStream:=FS;
if(Ext='.html')or(Ext='.htm')then
begin
Response.ContentType:='text/html;charset=utf-8';
end
else
if(Ext='.css')then
begin
Response.ContentType:='text/css;charset=utf-8';
end
else
if(Ext='.js')then
begin
Response.ContentType:='text/javascript;charset=utf-8';
end
else
if(ext='.txt')then
begin
Response.ContentType:='text/plain;charset=utf-8';
end;
end
else
if(Ext='.jpg')or(Ext='.jpeg')or(Ext='.png')or(Ext='.bmp')or(Ext='.gif')or(Ext='.ttf')or(Ext='.rar')then
begin
FS:=TFileStream.Create(FileName,fmOpenRead);
Response.ContentStream:=FS;
end;
end;
end;
procedure TWebModule1.sendtext(Atext: widestring);
var
h: hwnd;
i: integer;
begin
//myword := '你好啊'; //这里赋值上你要发送bai的话,可du以是中文
//h:=FindWindow('Notepad','无标题.txt - 记事本'); //查找窗口句柄
h := GetFocusHandle; //GetForegroundWindow;
if h <> 0 then
//PostMessage(h,WM_CHAR,Wparam('B'),0);
for i:=1 to length(Atext) do
PostMessage(h,WM_Char,Wparam(Atext[i]),0); //发送字符串给窗口
if mainform.cb4.checked then
Beep(800, 500);
if mainform.CB3.Checked then
PostMessage(h,WM_Char,Wparam(chr(13)),0);
//SendMessage(h,WM_SETTEXT,Length(Atext),Integer(PChar(Atext)));
end;
procedure TWebModule1.WebModule1accesstextAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var s:widestring;
begin
//S:=URLDecode(Request.Content);
//S := Request.QueryFields.Text;
S := Request.QueryFields.Values['text'];
try
mainform.writestring(s,mainform.CB1.Checked);
sendtext(s);
Response.ContentType:='text';
Response.Content:='ok';
finally
end;
end;
procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var S: string; Ext:string;
begin{ StandAlone 模式下,作为一个 WebServer,浏览器请求的静态文件,需要直接输出。
如果浏览器没有请求静态文件,则输出动态创建的默认页面给浏览器。}
S := Request.PathInfo;
if FileExists(Self.FMyRoot S) then
begin
Self.HandleFileRequest(Self.FMyRoot S, Request, Response);
Ext:=LowerCase(ExtractFileExt(Self.FMyRoot S));
if (Ext='.html') or (Ext='.htm')then
writeWorkLog(Request.RemoteAddr '访问' Self.FMyRoot S);
end
else if S = '/' then begin S := '';
S := '<html><head></head><body>这是默认页面</body></html>';
Response.Content := S;
end else
begin
Response.Content := '';
end;
Handled := True;
end;
procedure TWebModule1.WebModule1testAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
HTML:string;
begin
HTML:='<html><head><title>Web Broker范例</title></head>';
HTML:=HTML '<body><h1>这是一个Web Broker的示范程序</h1>';
HTML:=HTML '<hr><p>当前时间:</P>';
HTML:=HTML DateTimeToStr(Now()) '</body></html>';
Response.Content:=HTML;
end;
procedure TWebModule1.WebModuleCreate(Sender: TObject);
begin
myroot:='.';
end;
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论