腾讯官方提供的QQ通信组件:http://wp.qq.com/index.html
html
前天在BruceZhang 的一篇博文《求助:如何在ASP页面中调用Winform程序呢?》中回答了他提出的问题,但细想下以为个人思路有误。 web
今天在试用WebQQ的时候,无聊中想起不少人的博客上都有这样的小玩意,
, 点击下就能够和博主进行对话,并且无需加博主为好友。 shell
哎,这样的方式不就正好是BruceZhang那个问题的解决方案吗?那么腾讯是怎么作到在Web页面中调用QQ程序的呢? 浏览器
先来看腾讯提供给咱们的代码: 函数
<
a
href
="tencent://message/?uin=88888888&Site=JooIT.com&Menu=yes"
>
<
img
border
="0"
SRC
='http://is.qq.com/webpresence/images/status/01_online.gif'
alt
="点击这里给我发消息"
>
</
a
>
很显然,奥妙就在“tencent://message/?uin=215555521&Site=JooIT.com&Menu=yes”这里,那这又究竟是什么原理呢? 测试
先扯开话题按本身的思路来想,要打开本地的QQ,确定要分两步走,首先是定位到QQ,而后是传递给它一些参数,也就是“uin=215555521&Site=JooIT.com&Menu=yes”这样的东西。定位的话,借助注册表是最明显的方式了。可怎么把QQ跑起来呢?要咱们本身去启动一个进程么?答案是否认的,Windows操做系统考虑了这一点,容许咱们为本身的应用程序注册为一个协议处理者,具体参见MSDN上的文章《Registering an Application to a URL Protocol》 ui
腾讯的Tencent://Message协议注册表以下: this
Windows Registry Editor Version
5.00
[HKEY_CLASSES_ROOT\Tencent]
@=
"
TencentProtocol
"
"
URL Protocol
"
=
"
D:\\Program Files\\Tencent\\QQ\\Timwp.exe
"
[HKEY_CLASSES_ROOT\Tencent\DefaultIcon]
@=
"
D:\\Program Files\\Tencent\\QQ\\Timwp.exe,1
"
[HKEY_CLASSES_ROOT\Tencent\shell]
[HKEY_CLASSES_ROOT\Tencent\shell\open]
[HKEY_CLASSES_ROOT\Tencent\shell\open\command]
@=
"
\
"
D:\\Program Files\\Tencent\\QQ\\Timwp.exe\
"
\
"
%
1
\
""
此注册表所实现的就是当浏览器(或其它)碰到 tencent://… 时,自动调用 Timwp.exe,并把 tencent://… 地址做为第一个参数传递给 Timwp.exe。 spa
废话很少说,下面就动手实验一个demo来讲明一切,源代码请在文章首部自行下载。 很简单的功能,就是显示传递给MFC Dialog程序的参数值。就只分析下我添加的代码: 操作系统
首先须要获取传入的参数,在控制台程序中咱们都知道main()函数的参数argv里带入了传入的参数,而在MFC程序中则须要在InitInstance()中进行命令行参数解析。
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
为了给对话框传入待显示的参数,加入了一个SetDisplayInfo方法。
CHelloWorldDlg dlg;
dlg.SetDisplayInfo(cmdInfo.m_strFileName);
//
设置待显示的信息
void
CHelloWorldDlg::SetDisplayInfo(CString
&
strInfo)
{
this
->
m_strInfo
=
strInfo;
}
最后在OnInitDialog函数中进行参数解析
//
解析传入的完整地址,e.g "helloworld:
//
hello world/"
int
pos
=
m_strInfo.Find(
"
//
"
);
//
找到分隔符
m_strInfo
=
m_strInfo.Mid(pos
+
2
);
//
取到传入的参数
m_strInfo.Delete(m_strInfo.GetLength()
-
1
);
//
去掉最后的'/'
m_edit_info.SetWindowText(m_strInfo);
好了,来到最关键的步骤了,在注册表中为咱们自定义的helloworld协议创建起注册表项,从而让HelloWorld应用程序支持此协议。将以下的注册表项加入便可,这里为了简单起见我直接用一个.reg文件来实现,也能够用其余方式进行:
Windows Registry Editor Version
5.00
[HKEY_CLASSES_ROOT
\
HelloWorld]
@
=
"
HelloWorld Protocol
"
"
URL Protocol
"
=
""
[HKEY_CLASSES_ROOT
\
HelloWorld
\
DefaultIcon]
@
=
"
D:\\My Documents\\Visual Studio 2005\\Projects\\HelloWorld\\release\\HelloWorld.exe,1
"
[HKEY_CLASSES_ROOT
\
HelloWorld
\
shell
]
@
=
""
[HKEY_CLASSES_ROOT
\
HelloWorld
\
shell
\
open]
@
=
""
[HKEY_CLASSES_ROOT
\
HelloWorld
\
shell
\
open
\
command
]
@
=
"
\
"
D:
\\
My Documents
\\
Visual Studio
2005
\\
Projects
\\
HelloWorld
\\
release
\\
HelloWorld
.
exe
\
"
\
"
%1
\
""
结果如图所示
好了,这下能够来测试helloworld协议了,在地址栏中输入:helloworld://hello world/,怎么样,下面的画面出来了吧,
再来到web页面进行测试,修改上面的html代码以下:
<
html
>
<
head
></
head
>
<
body
>
<
div
>
<
a
href
="helloworld://hello world"
>
<
img
border
="0"
SRC
='http://is.qq.com/webpresence/images/status/01_online.gif'
alt
="点击这里给我发消息"
>
</
a
>
</
div
>
</
body
>
</
html
>
如果要在web页面调用本地的winform程序,同理也是可行,不过我不大懂.net,有心的朋友请试试看。
参考资料:
1, Registering an Application to a URL Protocol,
2, Tencent://Message/协议的实现原理
3,仿腾讯 QQ 和 Skype 经过URL触发本身的程序
4,Register protocol