在程式中開啟QQ對話方塊
以前就注意到在網頁上可以通過特殊的連結訪問程式,如tecent:// thunder://,卻不知道其中原理。
今天才學習到RegisterProtocol,在對於 Windows、Linux 和 OS X 作業系統都可以註冊這樣的協議。
比如說 Windows,其實只需寫入登錄檔,即可實現協議與執行程式的關聯。
例如騰訊的協議登錄檔如下:
[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\”"
直接引用原作者的圖片了。登錄檔內容在HKEY_CLASS_ROOT下,TENCENT即對應協議頭 tencent://,....\shell\open\command鍵值代表執行某exe,將第一個引數傳入程式,也就是網址本身。
所以得到2個有用的事情:
1.如果想通過自己的應用程式或網頁訪問到QQ或者迅雷,這種自定義了RegisterProtocol的程式,訪問起來都比較方便(網頁訪問還沒研究)。本地程式訪問可以直接在登錄檔中查詢“HKEY_CLASS_ROOT\xxx”。
2.自己寫的程式也可以定義RegisterProtocol
再記錄一下幾個事情
1.想獲得QQ的推廣連結需要在騰訊推廣的頁面上註冊 http://shang.qq.com/v3/index.html,需要通過授權才能讓陌生人在不加好友的情況下與你聊天。否則,這個功能現在只能與自己的好友對話
2.推廣連結是 http://wpa.qq.com/msgrd?v=3&uin=(QQ號碼)&site=qq&menu=yes
3.TencentProtocol的使用格式 tencent://message/?uin=(QQ號碼)&Menu=yes
最後是嘗試著實現功能的部分
今天使用Adobe AIR for Desktop實現了一下開啟與好友聊天視窗的功能,再次熟悉了一下NativeProcess的使用方法。
NativeProcess只能訪問exe。每個NativeProcess都會開啟一個程序。因此傳入cmd.exe的路徑即可使用命令列工具
var n:NativeProcess = new NativeProcess();
n.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutput);
n.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onError);
var info:NativeProcessStartupInfo = new NativeProcessStartupInfo();
info.executable = new File("c://windows//System32//cmd.exe");
n.start(info);
在onOutput函式接收到cmd的輸出後,命令列啟動完畢,就可以輸入下一步命令,查詢登錄檔了
var cmd:String = 'reg query "HKEY_CLASSES_ROOT\\Tencent" /v "URL Protocol" \n';
n.standardInput.writeMultiByte(cmd, "utf-8");
注意命令中的\n,沒有回車輸入,是不會產生輸出結果的
然後onOutput函式再次輸出一堆資訊,提取有用的輸出,獲取exe路徑再建立一個訪問qq程式的程序,將TencentProtocol的連結傳入,功能就完整實現了。
if (str.indexOf("Timwp.exe") > -1) {
var idx:int = str.lastIndexOf(":");
var exeIdx:int = str.lastIndexOf(".exe");
var path:String = str.substr(idx-1, exeIdx - idx + 5).replace(/\\/g, "//");
trace("Path:" + path);
var tencentFile:File = new File(path);
trace(tencentFile.exists);
if(tencentFile.exists) {
var startQQcmd:String = "tencent://message/?uin=123456789&Menu=yes";
var tencentProcess:NativeProcess = new NativeProcess();
var tencentInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
tencentInfo.executable = tencentFile;
tencentInfo.arguments.push(startQQcmd);
tencentProcess.start(tencentInfo);
}
}
試了一下直接在瀏覽器位址列中輸入
在IE下沒問題,在Chrome下卻不好用,還沒搞清楚為什麼,以後再補充吧