AJAX 流行之后,總想好好學習一下。但是眾多的框架實在難以選擇。說明一下 ASP.NET AJAX 并不包括在 AJAX 框架之中。
剛開始學了 JQuqery, 眾多的 $get(),...等等符號早已把我搞暈了。暫時就放棄了。
后來學習 ASP.NET AJAX ,在微軟的領導下,逐漸由服務器端轉向客戶端編程。 激起我客戶端編程的興趣,
才想起學習一下了 Jquery.
隨著WEB2.0及ajax思想在互聯網上的快速發展傳播,陸續出現了一些優秀的Js框架,其中比較著名的有Prototype、YUI、jQuery、mootools、Bindows以及國內的JSVM框架等,通過將這些JS框架應用到我們的項目中能夠使程序員從設計和書寫繁雜的JS應用中解脫出來,將關注點轉向功能需求而非實現細節上,從而提高項目的開發速度。
jQuery是繼prototype之后的又一個優秀的Javascript框架。它是由 John Resig 于 2006 年初創建的,它有助于簡化 JavaScript™ 以及Ajax 編程。有人使用這樣的一比喻來比較prototype和jQuery:prototype就像Java,而jQuery就像ruby. 它是一個簡潔快速靈活的JavaScript框架,它能讓你在你的網頁上簡單的操作文檔、處理事件、實現特效并為Web頁面添加Ajax交互。
它具有如下一些特點:
1、代碼簡練、語義易懂、學習快速、文檔豐富。
2、jQuery是一個輕量級的腳本,其代碼非常小巧,最新版的JavaScript包只有20K左右。
3、jQuery支持CSS1-CSS3,以及基本的xPath。
4、jQuery是跨瀏覽器的,它支持的瀏覽器包括IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+。
5、可以很容易的為jQuery擴展其他功能。
6、能將JS代碼和HTML代碼完全分離,便于代碼和維護和修改。
7、插件豐富,除了jQuery本身帶有的一些特效外,可以通過插件實現更多功能,如表單驗證、tab導航、拖放效果、表格排序、DataGrid,樹形菜單、圖像特效以及ajax上傳等。
jQuery的設計會改變你寫JavaScript代碼的方式,降低你學習使用JS操作網頁的復雜度,提高網頁JS開發效率,無論對于js初學者還是資深專家,jQuery都將是您的首選。
jQuery適合于設計師、開發者以及那些還好者,同樣適合用于商業開發,可以說jQuery適合任何JavaScript應用的地方,可用于不同的Web應用程序中。
官方站點:http://jquery.com/ 中文站點:http://jquery.org.cn/
1.2、目的
通過學習本文檔,能夠對jQuery有一個簡單的認識了解,清楚JQuery與其他JS框架的不同,掌握jQuery的常用語法、使用技巧及注意事項。
二、使用方法
在需要使用JQuery的頁面中引入JQuery的js文件即可。
例如:<script type="text/javascript" src="js/jquery.js"></script>
引入之后便可在頁面的任意地方使用jQuery提供的語法。
三、學習教程及參考資料
請參照《jQuery中文API手冊》和http://jquery.org.cn/visual/cn/index.xml
推薦兩篇不錯的jquery教程:《jQuery的起點教程》和《使用 jQuery 簡化 Ajax 開發》
四、語法總結和注意事項
1、關于頁面元素的引用
通過jquery的$()引用元素包括通過id、class、元素名以及元素的層級關系及dom或者xpath條件等方法,且返回的對象為jquery對象(集合對象),不能直接調用dom定義的方法。
2、jQuery對象與dom對象的轉換
只有jquery對象才能使用jquery定義的方法。注意dom對象和jquery對象是有區別的,調用方法時要注意操作的是dom對象還是jquery對象。
普通的dom對象一般可以通過$()轉換成jquery對象。
如:$(document.getElementById("msg"))則為jquery對象,可以使用jquery的方法。
由于jquery對象本身是一個集合。所以如果jquery對象要轉換為dom對象則必須取出其中的某一項,一般可通過索引取出。
如:$("#msg")[0],$("div").eq(1)[0],$("div").get()[1],$("td")[5]這些都是dom對象,可以使用dom中的方法,但不能再使用Jquery的方法。
以下幾種寫法都是正確的:
$("#msg").html();
$("#msg")[0].innerHTML;
$("#msg").eq(0)[0].innerHTML;
$("#msg").get(0).innerHTML;
3、如何獲取jQuery集合的某一項
對于獲取的元素集合,獲取其中的某一項(通過索引指定)可以使用eq或get(n)方法或者索引號獲取,要注意,eq返回的是jquery對象,而get(n)和索引返回的是dom元素對象。對于jquery對象只能使用jquery的方法,而dom對象只能使用dom的方法,如要獲取第三個<div>元素的內容。有如下兩種方法:
$("div").eq(2).html(); //調用jquery對象的方法
$("div").get(2).innerHTML; //調用dom的方法屬性
4、同一函數實現set和get
Jquery中的很多方法都是如此,主要包括如下幾個:
$("#msg").html(); //返回id為msg的元素節點的html內容。
$("#msg").html("<b>new content</b>");
//將“<b>new content</b>” 作為html串寫入id為msg的元素節點內容中,頁面顯示粗體的new content
$("#msg").text(); //返回id為msg的元素節點的文本內容。
$("#msg").text("<b>new content</b>");
//將“<b>new content</b>” 作為普通文本串寫入id為msg的元素節點內容中,頁面顯示<b>new content</b>
$("#msg").height(); //返回id為msg的元素的高度
$("#msg").height("300"); //將id為msg的元素的高度設為300
$("#msg").width(); //返回id為msg的元素的寬度
$("#msg").width("300"); //將id為msg的元素的寬度設為300
$("input").val("); //返回表單輸入框的value值
$("input").val("test"); //將表單輸入框的value值設為test
$("#msg").click(); //觸發id為msg的元素的單擊事件
$("#msg").click(fn); //為id為msg的元素單擊事件添加函數
同樣blur,focus,select,submit事件都可以有著兩種調用方法
5、集合處理功能
對于jquery返回的集合內容無需我們自己循環遍歷并對每個對象分別做處理,jquery已經為我們提供的很方便的方法進行集合的處理。
包括兩種形式:
$("p").each(function(i){this.style.color=['#f00','#0f0','#00f'][i]})
//為索引分別為0,1,2的p元素分別設定不同的字體顏色。
$("tr").each(function(i){this.style.backgroundColor=['#ccc','#fff'][i%2]})
//實現表格的隔行換色效果
$("p").click(function(){alert($(this).html())})
//為每個p元素增加了click事件,單擊某個p元素則彈出其內容
6、擴展我們需要的功能
$.extend({
min: function(a, b){return a < b?a:b; },
max: function(a, b){return a > b?a:b; }
}); //為jquery擴展了min,max兩個方法
使用擴展的方法(通過“$.方法名”調用):
alert("a=10,b=20,max="+$.max(10,20)+",min="+$.min(10,20));
7、支持方法的連寫
所謂連寫,即可以對一個jquery對象連續調用各種不同的方法。
例如:
$("p").click(function(){alert($(this).html())})
.mouseover(function(){alert('mouse over event')})
.each(function(i){this.style.color=['#f00','#0f0','#00f'][i]});
8、操作元素的樣式
主要包括以下幾種方式:
$("#msg").css("background"); //返回元素的背景顏色
$("#msg").css("background","#ccc") //設定元素背景為灰色
$("#msg").height(300); $("#msg").width("200"); //設定寬高
$("#msg").css({ color: "red", background: "blue" });//以名值對的形式設定樣式
$("#msg").addClass("select"); //為元素增加名稱為select的class
$("#msg").removeClass("select"); //刪除元素名稱為select的class
$("#msg").toggleClass("select"); //如果存在(不存在)就刪除(添加)名稱為select的class
9、完善的事件處理功能
Jquery已經為我們提供了各種事件處理方法,我們無需在html元素上直接寫事件,而可以直接為通過jquery獲取的對象添加事件。
如:
$("#msg").click(function(){alert("good")}) //為元素添加了單擊事件
$("p").click(function(i){this.style.color=['#f00','#0f0','#00f'][i]})
//為三個不同的p元素單擊事件分別設定不同的處理
jQuery中幾個自定義的事件:
(1)hover(fn1,fn2):一個模仿懸停事件(鼠標移動到一個對象上面及移出這個對象)的方法。當鼠標移動到一個匹配的元素上面時,會觸發指定的第一個函數。當鼠標移出這個元素時,會觸發指定的第二個函數。
//當鼠標放在表格的某行上時將class置為over,離開時置為out。
$("tr").hover(function(){
$(this).addClass("over");
},
function(){
$(this).addClass("out");
});
(2)ready(fn):當DOM載入就緒可以查詢及操縱時綁定一個要執行的函數。
$(document).ready(function(){alert("Load Success")})
//頁面加載完畢提示“Load Success”,相當于onload事件。與$(fn)等價
(3)toggle(evenFn,oddFn): 每次點擊時切換要調用的函數。如果點擊了一個匹配的元素,則觸發指定的第一個函數,當再次點擊同一元素時,則觸發指定的第二個函數。隨后的每次點擊都重復對這兩個函數的輪番調用。
//每次點擊時輪換添加和刪除名為selected的class。
$("p").toggle(function(){
$(this).addClass("selected");
},function(){
$(this).removeClass("selected");
});
(4)trigger(eventtype): 在每一個匹配的元素上觸發某類事件。
例如:
$("p").trigger("click"); //觸發所有p元素的click事件
(5)bind(eventtype,fn),unbind(eventtype): 事件的綁定與反綁定
從每一個匹配的元素中(添加)刪除綁定的事件。
例如:
$("p").bind("click", function(){alert($(this).text());}); //為每個p元素添加單擊事件
$("p").unbind(); //刪除所有p元素上的所有事件
$("p").unbind("click") //刪除所有p元素上的單擊事件
10、幾個實用特效功能
其中toggle()和slidetoggle()方法提供了狀態切換功能。
如toggle()方法包括了hide()和show()方法。
slideToggle()方法包括了slideDown()和slideUp方法。
11、幾個有用的jQuery方法
$.browser.瀏覽器類型:檢測瀏覽器類型。有效參數:safari, opera, msie, mozilla。如檢測是否ie:$.browser.isie,是ie瀏覽器則返回true。
$.each(obj, fn):通用的迭代函數。可用于近似地迭代對象和數組(代替循環)。
如
$.each( [0,1,2], function(i, n){ alert( "Item #" + i + ": " + n ); });
等價于:
var tempArr=[0,1,2];
for(var i=0;i<tempArr.length;i++){
alert("Item #"+i+": "+tempArr[i]);
}
也可以處理json數據,如
$.each( { name: "John", lang: "JS" }, function(i, n){ alert( "Name: " + i + ", Value: " + n ); });
結果為:
Name:name, Value:John
Name:lang, Value:JS
$.extend(target,prop1,propN):用一個或多個其他對象來擴展一個對象,返回這個被擴展的對象。這是jquery實現的繼承方式。
如:
$.extend(settings, options);
//合并settings和options,并將合并結果返回settings中,相當于options繼承setting并將繼承結果保存在setting中。
var settings = $.extend({}, defaults, options);
//合并defaults和options,并將合并結果返回到setting中而不覆蓋default內容。
可以有多個參數(合并多項并返回)
$.map(array, fn):數組映射。把一個數組中的項目(處理轉換后)保存到到另一個新數組中,并返回生成的新數組。
如:
var tempArr=$.map( [0,1,2], function(i){ return i + 4; });
tempArr內容為:[4,5,6]
var tempArr=$.map( [0,1,2], function(i){ return i > 0 ? i + 1 : null; });
tempArr內容為:[2,3]
$.merge(arr1,arr2):合并兩個數組并刪除其中重復的項目。
如:$.merge( [0,1,2], [2,3,4] ) //返回[0,1,2,3,4]
$.trim(str):刪除字符串兩端的空白字符。
如:$.trim(" hello, how are you? "); //返回"hello,how are you? "
12、解決自定義方法或其他類庫與jQuery的沖突
很多時候我們自己定義了$(id)方法來獲取一個元素,或者其他的一些js類庫如prototype也都定義了$方法,如果同時把這些內容放在一起就會引起變量方法定義沖突,Jquery對此專門提供了方法用于解決此問題。
使用jquery中的jQuery.noConflict();方法即可把變量$的控制權讓渡給第一個實現它的那個庫或之前自定義的$方法。之后應用Jquery的時候只要將所有的$換成jQuery即可,如原來引用對象方法$("#msg")改為jQuery("#msg")。
如:
jQuery.noConflict();
// 開始使用jQuery
jQuery("div p").hide();
// 使用其他庫的 $()
$("content").style.display = 'none';
Flex License:
1307-1581-4356-2616-4951-7949 (Commercial Version)
1307-1581-4356-2939-1231-4484 (Education Version)
Charting License:
1301-4581-4356-7349-9369-3351 (Commercial Version)
Today we shipped Visual Studio 2008 and .NET 3.5. You can download the final release using one of the links below:
If you are a MSDN subscriber, you can download your copy from the MSDN subscription site (note: some of the builds are just finishing being uploaded now - so check back later during the day if you don't see it yet).
If you are a non-MSDN subscriber, you can download a 90-day free trial edition of Visual Studio 2008 Team Suite here. A 90-day trial edition of Visual Studio 2008 Professional (which will be a slightly smaller download) will be available next week. A 90-day free trial edition of Team Foundation Server can also be downloaded here.
If you want to use the free Visual Studio 2008 Express editions (which are much smaller and totally free), you can download them here.
If you want to just install the .NET Framework 3.5 runtime, you can download it here.
Visual Studio 2008 and .NET 3.5 contain a ton of new functionality and improvements. Below are links to blog posts I've done myself as well as links to videos you can watch to learn more about it:
VS 2008 Multi-Targeting Support
VS 2008 enables you to build applications that target multiple versions of the .NET Framework. This means you can use VS 2008 to open, edit and build existing .NET 2.0 and ASP.NET 2.0 applications (including ASP.NET 2.0 applications using ASP.NET AJAX 1.0), and continue to deploy these application on .NET 2.0 machines. You can learn more about how this works from my blog post here:
ASP.NET AJAX and JavaScript Support
.NET 3.5 has ASP.NET AJAX built-in (no separate download required). In addition to including all of the features in ASP.NET AJAX 1.0, ASP.NET 3.5 also now includes richer support for UpdatePanels integrating with WebParts, ASP.NET AJAX integration with controls like <asp:menu> and <asp:treeview>, WCF support for JSON, and many other AJAX improvements.
VS 2008 and Visual Web Developer 2008 also now have great support for integrating JavaScript and AJAX into your applications. You can learn more about this from my blog posts here:
You can watch some videos that discuss ASP.NET AJAX and Visual Studio 2008 support for it here.
I also highly recommend the excellent ASP.NET AJAX in Action book to learn more about ASP.NET AJAX (both client-side and server-side).
VS 2008 Web Designer and CSS Support
VS 2008 and Visual Web Developer 2008 Express includes a significantly improved HTML web designer (the same one that ships with Expression Web). This delivers support for split-view editing, nested master pages, and great CSS integration. Below are some articles I've written that discuss this more:
ASP.NET 3.5 also has a new <asp:ListView> control that provides the ability to perform rich data scenarios with total control over the markup. It works nicely with the new CSS support in VS 2008. You can learn more about it from my article here:
You can watch some videos that discuss the new Visual Studio 2008 web designer features and the new ListView/DataPager controls here.
Language Improvements and LINQ
The new VB and C# compilers in VS 2008 deliver significant improvements to the languages. Both add functional programming concepts that enable you to write cleaner, terser, and more expressive code. These features also enable a new programming model we call LINQ (language integrated query) that makes querying and working with data a first-class programming concept with .NET.
Below are some of the articles I've written that explore these new language features using C#:
Here are a few additional blog posts I've written that show off some of the new VS 2008 code editing support and some cool ways to use these new language features:
The Visual Basic team has also created some great free videos that cover LINQ. You can watch them here.
Data Access Improvements with LINQ to SQL
LINQ to SQL is a built-in OR/M (object relational mapper) in .NET 3.5. It enables you to model relational databases using a .NET object model. You can then query the database using LINQ, as well as update/insert/delete data from it. LINQ to SQL fully supports transactions, views, and stored procedures. It also provides an easy way to integrate business logic and validation rules into your data model. Below are some of the articles I've written that explore how to use it:
I think you'll find that LINQ and LINQ to SQL makes it much easier to build much cleaner data models, and write much cleaner data code. I'll be adding more posts to my LINQ to SQL series in the weeks and months ahead (sorry for the delay in finishing them earlier - so much to-do and so little time to-do it all!).
Scott Stanfield is also working on creating some great LINQ to SQL videos for the www.asp.net site based on my article series above (all videos are in both VB and C#). You can watch the first 4 videos in this series here.
Browsing the .NET Framework Library Source using Visual Studio
As I blogged a few weeks ago, we will be releasing a reference version of the .NET Framework library source code as part of this release. Visual Studio 2008 has built-in debugger support to automatically step-into and debug this code on demand (VS 2008 can pull down the source for the appropriate .NET Framework library file automatically for you).
We are deploying the source servers to enable this right now, and will be publishing the steps to turn this feature on in the next few weeks.
Lots of other improvements
The list above is only a small set of the improvements coming. For client development VS 2008 includes WPF designer and project support. ClickOnce and WPF XBAPs now work with FireFox. WinForms and WPF projects can also now use the ASP.NET Application Services (Membership, Roles, Profile) for roaming user data.
Office development is much richer - including support for integrating with the Office 2007 ribbon, and with Outlook. Visual Studio Tools for Office support is also now built-into Visual Studio (you no longer need to buy a separate product).
New WCF and Workflow projects and designers are now included in VS 2008. Unit testing support is now much faster and included in VS Professional (and no longer just VSTS). Continuous Integration support is now built-in with TFS. AJAX web testing (unit and load) is now supported in the VS Test SKU. And there is much, much more...
People often ask me for suggestions on how best to upgrade from previous betas of Visual Studio 2008. In general I'd recommend uninstalling the Beta2 bits explicitly. As part of this you should uninstall Visual Studio 2008 Beta2, .NET Framework Beta2, as well as the Visual Studio Web Authoring Component (these are all separate installs and need to be uninstalled separately). I then usually recommend rebooting the machine after uninstalling just to make sure everything is clean before you kick off the new install. You can then install the final release of VS 2008 and .NET 3.5 on the machine.
Once installed, I usually recommend explicitly running the Tools->Import and Export Settings menu option, choosing the "Reset Settings" option, and then re-pick your preferred profile. This helps ensure that older settings from the Beta2 release are no longer around (and sometimes seems to help with performance).
Note that VS 2008 runs side-by-side with VS 2005 - so it is totally fine to have both on the same machine (you will not have any problems with them on the same box).
Two popular add-ins to Visual Studio are not yet available to download for the final VS 2008 release. These are the Silverlight 1.1 Tools Alpha for Visual Studio and the Web Deployment Project add-in for Visual Studio. Our hope is to post updates to both of them to work with the final VS 2008 release in the next two weeks. If you are doing Silverlight 1.1 development using VS 2008 Beta2 you'll want to stick with with VS 2008 Beta2 until this updated Silverlight Tools Add-In is available.
PYHYP-WXB3B-B2CCM-V9DX9-VDY8T
在開始>設置>控制面版>添加或刪除程序>卸載vs.net2008>出現卸載界面>點擊Next>輸入上面CD-key ->出現Success畫面。。激動ING
本人使用的是VS2008 RTM版..
Microsoft.Visual.Studio.Team.System.2008.Team.Suite-ZWTiSO
ed2k: Microsoft.Visual.Studio.Team.System.2008.Team.Suite-ZWTiSO.iso [3.83 Gb]
ed2k: Microsoft.Visual.Studio.Team.System.2008.Team.Suite-ZWTiSO.nfo [5.8 Kb]
[ Add all 2 links to your ed2k client ]
Microsoft.Visual.Studio.2008.Professional.Edition-ZWTiSO
ed2k: Microsoft.Visual.Studio.2008.Professional.Edition-ZWTiSO.iso [3.31 Gb]
ed2k: Microsoft.Visual.Studio.2008.Professional.Edition-ZWTiSO.nfo [5.5 Kb]
[ Add all 2 links to your ed2k client ]
Microsoft.Visual.Studio.Team.System.2008.Development.Edition
ed2k: Microsoft.Visual.Studio.Team.System.2008.Development.Edition-ZWTiSO.iso [3.81 Gb]
ed2k: Microsoft.Visual.Studio.Team.System.2008.Development.Edition-ZWTiSO.nfo [5.5 Kb]
[ Add all 2 links to your ed2k client ]
Microsoft.Visual.Studio.Team.System.2008.Team.Foundation.Server.Workgroup.Edition-ZWTiSO
ed2k: Microsoft.Visual.Studio.Team.System.2008.Team.Foundation.Server.Workgroup.Edition-ZWTiSO.iso [1.29 Gb]
ed2k: Microsoft.Visual.Studio.Team.System.2008.Team.Foundation.Server.Workgroup.Edition-ZWTiSO.nfo [5.7 Kb]
[ Add all 2 links to your ed2k client ]
Microsoft.Visual.Studio.Team.System.2008.Test.Load.Agent-ZWT
ed2k: Microsoft.Visual.Studio.Team.System.2008.Test.Load.Agent-ZWTiSO.iso [551.01 Mb]
ed2k: Microsoft.Visual.Studio.Team.System.2008.Test.Load.Agent-ZWTiSO.nfo [5.7 Kb]
[ Add all 2 links to your ed2k client ]
注冊機下載地址
有個系統隱藏的文件夾System Volume Information會達到1G甚至20G,這是系統還原的文件夾,這個目錄是WINDOWS對于大硬盤搜索方便的索引記錄文件!會在WINDOWS空閑時自動記錄,所以這個文件夾會越來越大,然后PF使用率不斷上升,導致機器卡住!&W&D.C g V b j [ a
我們可以禁用這個自動索引功能!打開搜索功能->改變首選項->不使用制作索引服務->不,不要啟用制作索引服務->確定。還有一件事,就是回到剛才的地方,下面還有一個“改變制作索引服務設置(高級)”,按下去,右鍵彈出的窗口中的那個索引服務->刪除,就好了!海騰數據中心服務器論壇 Y \ M z(R1{
O f ?&f t j9Y&] B ` SystemVolumeInformation\catalog.wci的文件用來存放索引文件,而且在系統空閑時,Windows會自動讀硬盤更新索引,安裝的文件越多,索引文件會越來越大。
D Y }&X 刪除索引服務:海騰數據、服務器論壇聯盟、win服務器、代理服務器,郵件服務器、代碼、程序、游戲下載、軟件、電腦技術、設計、圖片、信息發布 N i$y l z:M }
運行msconfig,然后選擇服務選項,找到IndexingService,將前面的小勾去掉,再刪掉文件夾即可。 |
Changing proxy settings of IE is a frequent requirement of mine. Then I got the idea of writing a tool by myself, at last. I have not found clear instructions on this. Many articles recommend to modify registry directly, but unfortunately their instruction is not enough. Most of them direct me to modify the following values in registry :-
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"ProxyEnable"=dword:00000001
"ProxyServer"=":"
"ProxyOverride"=""
"DisablePasswordCaching"=dword:00000001
I tested it and find that it does not work at least on my computer.( I access internet by ADSL connection.) So I backed up registry and modified proxy settings via Internet Explorer, finding that [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections] is also changed. So I wrote the following code snippet to change proxy settings:
void ShowError(long lerr) { LPVOID lpMsgBuf; if (!FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, lerr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL )) { return; } MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION ); LocalFree( lpMsgBuf ); } void CieproxyDlg::OnBnClickedOk() {//set proxy server UpdateData(); GetDlgItemText(IDC_EDIT1,m_sIEProxy); HKEY hk; LONG lret=RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", NULL,KEY_WRITE|KEY_SET_VALUE,&hk); if(lret==ERROR_SUCCESS&&NULL!=hk) { TCHAR* pbuf=m_sIEProxy.GetBuffer(1); lret=RegSetValueEx( hk,"ProxyServer",NULL,REG_SZ,pbuf,m_sIEProxy.GetLength()); DWORD dwenable=1; lret=RegSetValueEx(hk,"ProxyEnable",NULL,REG_DWORD, (LPBYTE)&dwenable,sizeof(dwenable)); RegCloseKey(hk); } const TCHAR* keyname3=_T( "software\\Microsoft\\windows\\currentversion\\Internet Settings\\Connections"); lret=RegOpenKeyEx(HKEY_CURRENT_USER,keyname3,NULL, KEY_READ|KEY_WRITE|KEY_SET_VALUE,&hk); if(lret==ERROR_SUCCESS&&NULL!=hk) { DWORD dwtype; char pbuf[256]; DWORD dwlen=sizeof(pbuf); constchar* valname="Connection to adsl3"; lret=RegQueryValueEx(hk,valname,NULL,&dwtype,pbuf,&dwlen); if(lret!=ERROR_SUCCESS) { ShowError(lret); } pbuf[8] = 3;//enable proxy pbuf[4]=pbuf[4]+1; constchar* p=m_sIEProxy.GetBuffer(1); memcpy(pbuf+16,p,m_sIEProxy.GetLength()); char c=0; for(int i=m_sIEProxy.GetLength();i<20;i++) pbuf[16+i]=c; m_sIEProxy.ReleaseBuffer(); lret=RegSetValueEx(hk,valname,NULL,REG_BINARY,pbuf,dwlen); RegCloseKey(hk); } DWORD dwret; SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE,NULL,NULL, SMTO_NORMAL,1000,&dwret); } void CieproxyDlg::OnBnClickedDisableProxy() { UpdateData(); GetDlgItemText(IDC_EDIT1,m_sIEProxy); HKEY hk; LONG lret=RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", NULL,KEY_WRITE|KEY_SET_VALUE,&hk); if(lret==ERROR_SUCCESS&&NULL!=hk) { DWORD dwenable=0; lret=RegSetValueEx(hk,"ProxyEnable",NULL,REG_DWORD, (LPBYTE)&dwenable,sizeof(dwenable)); RegCloseKey(hk); } const TCHAR* keyname3=_T( "software\\Microsoft\\windows\\currentversion\\Internet Settings\\Connections"); lret=RegOpenKeyEx(HKEY_CURRENT_USER,keyname3, NULL,KEY_READ|KEY_WRITE|KEY_SET_VALUE,&hk); if(lret==ERROR_SUCCESS&&NULL!=hk) { DWORD dwtype; char pbuf[256]; DWORD dwlen=sizeof(pbuf); constchar* valname="Connection to adsl3"; lret=RegQueryValueEx(hk,valname,NULL,&dwtype,pbuf,&dwlen); if(lret!=ERROR_SUCCESS) { ShowError(lret); } pbuf[8] = 1;//enable proxy pbuf[4]=pbuf[4]+1; lret=RegSetValueEx(hk,valname,NULL,REG_BINARY,pbuf,dwlen); RegCloseKey(hk); } DWORD dwret; SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE,NULL,NULL,SMTO_NORMAL, 1000,&dwret); }
Problem with above code is that existing Internet Explorer instances don't know the change of settings. What is more, changing registry directly is not an elegant method. Then the following must be more attractive :
BOOL SetConnectionOptions(LPCTSTR conn_name,LPCTSTR proxy_full_addr) { //conn_name: active connection name. //proxy_full_addr : eg "210.78.22.87:8000" INTERNET_PER_CONN_OPTION_LIST list; BOOL bReturn; DWORD dwBufSize = sizeof(list); // Fill out list struct. list.dwSize = sizeof(list); // NULL == LAN, otherwise connectoid name. list.pszConnection = conn_name; // Set three options. list.dwOptionCount = 3; list.pOptions = new INTERNET_PER_CONN_OPTION[3]; // Make sure the memory was allocated.if(NULL == list.pOptions) { // Return FALSE if the memory wasn't allocated. OutputDebugString("failed to allocat memory in SetConnectionOptions()"); return FALSE; } // Set flags. list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT | PROXY_TYPE_PROXY; // Set proxy name. list.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER; list.pOptions[1].Value.pszValue = proxy_full_addr;//"http://proxy:80";// Set proxy override. list.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; list.pOptions[2].Value.pszValue = "local"; // Set the options on the connection. bReturn = InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize); // Free the allocated memory.delete [] list.pOptions; InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); return bReturn; } BOOL DisableConnectionProxy(LPCTSTR conn_name) { //conn_name: active connection name. INTERNET_PER_CONN_OPTION_LIST list; BOOL bReturn; DWORD dwBufSize = sizeof(list); // Fill out list struct. list.dwSize = sizeof(list); // NULL == LAN, otherwise connectoid name. list.pszConnection = conn_name; // Set three options. list.dwOptionCount = 1; list.pOptions = new INTERNET_PER_CONN_OPTION[list.dwOptionCount]; // Make sure the memory was allocated.if(NULL == list.pOptions) { // Return FALSE if the memory wasn't allocated. OutputDebugString("failed to allocat memory in DisableConnectionProxy()"); return FALSE; } // Set flags. list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT ; // Set the options on the connection. bReturn = InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize); // Free the allocated memory.delete [] list.pOptions; InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); return bReturn; }
The usage is very straightforward:
//set proxy const char* connection_name="Connection to adsl3"; SetConnectionOptions(connection_name,"62.81.236.23:80"); //disable proxy DisableConnectionProxy(connection_name);
Existing Internet Explorer instances are notified by INTERNET_OPTION_SETTINGS_CHANGED
and INTERNET_OPTION_REFRESH