📄 options.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Module Name: options.cpp
Abstract: Implements the Internet Options dialog
Functions:
Notes:
--*/
#include "Precomp.h"
#include "resource.h"
#include "Mainwnd.h"
#include "raserror.h"
#include <ras.h>
#include <Afdfunc.h>
#if !defined(NOCOMMANDBAR) && !defined(NOOPTIONS)
//proxy stuf
static TCHAR *pszProxyKey = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
static DWORD _dwAccessType = 0; // access direct/proxy
static DWORD _dwEnableAutoDial = 0; // enable Autodial?
static TCHAR _szProxy[MAXLEN] = {_T("")}; // proxy name
static TCHAR _szProxyByPass[MAXLEN] = {_T("<local>")}; // proxy bypass
static TCHAR _szRasConnection[MAXLEN]; // ras connection name
#define MAX_REG_VALUE_LEN 50
static TCHAR *pszKey = _T("Software\\Microsoft\\Internet Explorer\\Main");
static TCHAR *pszCacheKey = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\5.0\\Cache\\Content");
static TCHAR *pszAutoDialKey = _T("RemoteAccess");
static TCHAR *pszOptions[3] = {_T("no"), _T("yes"), _T("hover")};
static TCHAR *pszUseLanNoAutodial = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
void SaveOptionsToRegistry(HWND hDlg);
void LoadOptionsFromRegistry(HWND hDlg);
void SetProxyOption();
void GetProxyOption2();
BOOL CALLBACK CMainWnd::OptionsDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
LoadOptionsFromRegistry(hwnd);
break;
case WM_COMMAND:
{
if ((HIWORD(wParam) == BN_CLICKED))
{
if (LOWORD(wParam) == IDC_USEPROXY)
{
BOOL bEnable = SendMessage(((HWND )lParam), BM_GETCHECK, 0L, 0L);
EnableWindow(GetDlgItem(hwnd, IDC_ADDRESS), bEnable);
EnableWindow(GetDlgItem(hwnd, IDC_PORT), bEnable);
return TRUE;
}
else if (LOWORD(wParam) == IDC_LANNOAUTODIAL)
{
BOOL bEnable = SendMessage(((HWND )lParam), BM_GETCHECK, 0L, 0L);
EnableWindow(GetDlgItem(hwnd, IDC_AUTODIAL), !bEnable);
}
else if (LOWORD(wParam) == IDOK)
{
SaveOptionsToRegistry(hwnd);
EndDialog(hwnd, TRUE);
return TRUE;
}
else if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(hwnd, FALSE);
return TRUE;
}
}
}
break;
default:
break;
}
return FALSE;
}
void SaveOptionsToRegistry(HWND hDlg)
{
LONG lRes;
HKEY hKey = NULL;
DWORD dwDisp;
DWORD dwAccessType=0;
TCHAR szString[MAXLEN];
// start page, search page, underline anchors
lRes = RegCreateKeyEx(HKEY_CURRENT_USER, pszKey, 0, NULL, 0, 0, 0, &hKey, &dwDisp);
if (hKey && lRes==ERROR_SUCCESS)
{
GetDlgItemText(hDlg, IDC_START, szString, MAXLEN);
lRes = RegSetValueEx(hKey, _T("Start Page"), 0, REG_SZ, (BYTE *)szString, MAXLEN);
GetDlgItemText(hDlg, IDC_SEARCH, szString, MAXLEN);
lRes = RegSetValueEx(hKey, _T("Search Page"), 0, REG_SZ, (BYTE *)szString, MAXLEN);
for(UINT nID=IDC_UNDERLINENEVER; nID<=IDC_UNDERLINEHOVER; nID++)
{
DWORD dwState = SendMessage(GetDlgItem(hDlg, nID), BM_GETCHECK, 0L, 0L);
if (dwState == BST_CHECKED)
{
int nCheck;
nCheck = nID-IDC_UNDERLINENEVER;
_tcscpy(szString, pszOptions[nCheck]);
lRes = RegSetValueEx(hKey, _T("Anchor Underline"), 0, REG_SZ,(BYTE *)szString, MAX_REG_VALUE_LEN);
break;
}
}
RegCloseKey(hKey);
}
// cache size
hKey = NULL;
lRes = RegCreateKeyEx(HKEY_CURRENT_USER, pszCacheKey, 0, NULL, 0, 0, 0, &hKey, &dwDisp);
if (hKey && lRes==ERROR_SUCCESS)
{
BOOL bTrans;
DWORD dwVal = GetDlgItemInt(hDlg, IDC_CACHE, &bTrans, FALSE);
if (bTrans)
lRes = RegSetValueEx(hKey, _T("CacheLimit"), 0, REG_DWORD, (BYTE *)(&dwVal), sizeof(DWORD));
RegCloseKey(hKey);
}
// Use LAN or use Autodial?
hKey = NULL;
lRes = RegCreateKeyEx(HKEY_CURRENT_USER, pszUseLanNoAutodial, 0, NULL, 0, 0, 0, &hKey, &dwDisp);
if (hKey && lRes==ERROR_SUCCESS)
{
_dwEnableAutoDial = SendMessage(GetDlgItem(hDlg, IDC_LANNOAUTODIAL), BM_GETCHECK,0,0)==BST_UNCHECKED;
lRes = RegSetValueEx(hKey, _T("EnableAutodial"), 0, REG_DWORD, (BYTE*)&_dwEnableAutoDial , sizeof(DWORD));
DWORD dwNoNetDial=0;
lRes = RegSetValueEx(hKey, _T("NoNetAutodial"), 0, REG_DWORD, (BYTE*)&dwNoNetDial, sizeof(DWORD));
RegCloseKey(hKey);
}
// ras autodial
hKey = NULL;
lRes = RegCreateKeyEx(HKEY_CURRENT_USER, pszAutoDialKey, 0, NULL, 0, 0, 0, &hKey, &dwDisp);
if (hKey && lRes==ERROR_SUCCESS)
{
HWND hRasCombo = GetDlgItem(hDlg, IDC_AUTODIAL);
int nIndex = SendMessage(hRasCombo, CB_GETCURSEL, 0, 0L);
if (nIndex != CB_ERR)
{
SendMessage(hRasCombo, CB_GETLBTEXT, (WPARAM )nIndex, (LPARAM )szString);
TCHAR szRas[RAS_MaxEntryName + 1];
_tcsncpy(szRas, szString, RAS_MaxEntryName);
lRes = RegSetValueEx(hKey, _T("InternetProfile"), 0, REG_SZ, (BYTE *)szRas, RAS_MaxEntryName*sizeof(TCHAR));
}
RegCloseKey(hKey);
}
// proxy info
hKey = NULL;
dwAccessType = (SendMessage(GetDlgItem(hDlg, IDC_USEPROXY), BM_GETCHECK, 0L, 0L))?
PROXY_TYPE_PROXY : PROXY_TYPE_DIRECT;
if(PROXY_TYPE_PROXY==dwAccessType)
{
UINT cch;
TCHAR szProxy[MAXLEN];
cch = GetDlgItemText(hDlg, IDC_ADDRESS, szProxy, MAXLEN);
GetDlgItemText(hDlg, IDC_PORT, szString, MAXLEN);
// prevent buffer overrun
if (cch < (MAXLEN-1))
{
_tcsncat(szProxy, _T(":"), MAXLEN-1-cch);
cch += 1;
}
if (cch < (MAXLEN-1))
{
_tcsncat(szProxy, szString, MAXLEN-1-cch);
}
lstrcpy(_szProxy,szProxy);
_dwAccessType = dwAccessType;
lstrcpy(_szProxyByPass,_T("<local>"));
}
else
{
_dwAccessType = dwAccessType;
// don't erase the old set proxy
lstrcpy(_szProxyByPass,_T("<local>"));
}
SetProxyOption();
InternetSetOption(NULL,INTERNET_OPTION_SETTINGS_CHANGED,NULL,0);
SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, NULL, NULL);
}
void LoadOptionsFromRegistry(HWND hDlg)
{
LONG lRes;
DWORD dwLen = MAXLEN;
DWORD dwType = 0;
HKEY hKey = NULL;
TCHAR szString[MAXLEN];
// start page, search page, underline anchors
lRes = RegOpenKeyEx(HKEY_CURRENT_USER, pszKey, 0, KEY_QUERY_VALUE, &hKey);
if (hKey && lRes==ERROR_SUCCESS)
{
lRes = RegQueryValueEx(hKey, _T("Start Page"), 0, &dwType, (BYTE *)szString, &dwLen);
SendMessage(GetDlgItem(hDlg, IDC_START), EM_SETLIMITTEXT, MAXLEN, 0);
SetDlgItemText(hDlg, IDC_START, szString);
dwLen = MAXLEN;
lRes = RegQueryValueEx(hKey, _T("Search Page"), 0, &dwType, (BYTE *)szString, &dwLen);
SendMessage(GetDlgItem(hDlg, IDC_SEARCH), EM_SETLIMITTEXT, MAXLEN, 0);
SetDlgItemText(hDlg, IDC_SEARCH, szString);
dwLen = MAXLEN;
lRes = RegQueryValueEx(hKey, _T("Anchor Underline"), 0, &dwType, (BYTE *)szString, &dwLen);
UINT nID = IDC_UNDERLINENEVER;
while(nID <= IDC_UNDERLINEHOVER)
{
SendMessage(GetDlgItem(hDlg, nID), BM_SETCHECK, 0L, 0L);
nID++;
}
nID = (!_tcscmp(szString, _T("no"))) ? 0 :
(!_tcscmp(szString, _T("yes"))) ? 1 : 2;
nID += IDC_UNDERLINENEVER;
SendMessage(GetDlgItem(hDlg, nID), BM_SETCHECK, TRUE, 0L);
RegCloseKey(hKey);
}
// cache size 4 chars allows 9999 KB which is > 9 GB
SendMessage(GetDlgItem(hDlg, IDC_CACHE), EM_SETLIMITTEXT, 4, 0);
hKey = NULL;
lRes = RegOpenKeyEx(HKEY_CURRENT_USER, pszCacheKey, 0, KEY_QUERY_VALUE, &hKey);
if (hKey && lRes==ERROR_SUCCESS)
{
DWORD dwVal;
dwLen = sizeof(dwVal);
lRes = RegQueryValueEx(hKey, _T("CacheLimit"), 0, &dwType, (BYTE *)(&dwVal), &dwLen);
if(lRes== ERROR_SUCCESS)
SetDlgItemInt(hDlg, IDC_CACHE, dwVal, FALSE);
RegCloseKey(hKey);
}
// Use LAN or use Autodial?
hKey = NULL;
lRes = RegOpenKeyEx(HKEY_CURRENT_USER, pszUseLanNoAutodial, 0, KEY_QUERY_VALUE, &hKey);
if (hKey && lRes==ERROR_SUCCESS)
{
dwLen = sizeof(_dwEnableAutoDial);
lRes = RegQueryValueEx(hKey, _T("EnableAutodial"), 0, &dwType, (BYTE *)&_dwEnableAutoDial, &dwLen);
if (lRes == ERROR_SUCCESS && !_dwEnableAutoDial)
SendMessage(GetDlgItem(hDlg, IDC_LANNOAUTODIAL), BM_SETCHECK, BST_CHECKED, 0L);
else
SendMessage(GetDlgItem(hDlg, IDC_LANNOAUTODIAL), BM_SETCHECK, BST_UNCHECKED, 0L);
RegCloseKey(hKey);
}
// ras configs
DWORD cb = 0;
HWND hRasCombo = GetDlgItem(hDlg, IDC_AUTODIAL);
if ((ERROR_BUFFER_TOO_SMALL == RasEnumEntries(NULL, NULL, NULL, &cb, NULL)) && cb)
{
DWORD cEntries; // Number of Entries found
LPRASENTRYNAME lpRasEntryName = (LPRASENTRYNAME)LocalAlloc(LPTR, cb);
if (lpRasEntryName)
{
lpRasEntryName[0].dwSize = sizeof(RASENTRYNAME);
if (!RasEnumEntries(NULL, NULL, lpRasEntryName, &cb, &cEntries) && cEntries)
{
for (DWORD i=0; i<cEntries; i++)
{
SendMessage(hRasCombo, CB_ADDSTRING, 0, (LPARAM )lpRasEntryName[i].szEntryName);
}
}
LocalFree(lpRasEntryName);
}
LPRASCONN lpRasConn = (LPRASCONN) LocalAlloc(LPTR, sizeof(RASCONN));
if (lpRasConn)
{
lpRasConn->dwSize = sizeof(RASCONN);
DWORD cConnections;
DWORD nRet= RasEnumConnections(lpRasConn, &lpRasConn->dwSize, &cConnections);
if(nRet==0)
{
int i = SendMessage(hRasCombo, CB_FINDSTRINGEXACT,(WPARAM )-1, (LPARAM )lpRasConn->szEntryName);
if (i != CB_ERR)
SendMessage(hRasCombo, CB_SETCURSEL,(WPARAM)i, (LPARAM)0);
}
LocalFree(lpRasConn);
}
}
hKey = NULL;
lRes = RegOpenKeyEx(HKEY_CURRENT_USER, pszAutoDialKey, 0, KEY_QUERY_VALUE, &hKey);
if (hKey && lRes==ERROR_SUCCESS)
{
dwLen = MAXLEN;
lRes = RegQueryValueEx(hKey, _T("InternetProfile"), 0, &dwType, (BYTE *)_szRasConnection, &dwLen);
if(lRes==ERROR_SUCCESS)
{
int nSel = SendMessage(hRasCombo, CB_FINDSTRINGEXACT, (WPARAM )(-1), (LPARAM )(LPCSTR)_szRasConnection);
if (nSel != CB_ERR)
SendMessage(hRasCombo, CB_SETCURSEL, nSel, 0);
}
else
lstrcpy(_szRasConnection,_T(""));
RegCloseKey(hKey);
}
if(!_dwEnableAutoDial)
EnableWindow(hRasCombo,FALSE);
// wininet does save the proxy settings in registry
GetProxyOption2();
BOOL bUseProxy = (_dwAccessType&PROXY_TYPE_PROXY);
TCHAR lpszProxy[MAXLEN];
LPTSTR pszProxy;
SendMessage(GetDlgItem(hDlg,IDC_USEPROXY),BM_SETCHECK,bUseProxy,0L);
lstrcpy(lpszProxy,_szProxy);
pszProxy = _tcstok(lpszProxy, _T(":"));
SetDlgItemText(hDlg, IDC_ADDRESS, pszProxy);
pszProxy = _tcstok(NULL, _T(":"));
SetDlgItemText(hDlg, IDC_PORT, pszProxy);
if(!bUseProxy)
{
EnableWindow(GetDlgItem(hDlg, IDC_ADDRESS), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_PORT), FALSE);
}
}
void SetProxyOption()
{
INTERNET_PER_CONN_OPTION_LIST iOptionList;
INTERNET_PER_CONN_OPTION iOptions[3];
ULONG uSize = sizeof(iOptionList);
iOptionList.dwSize = uSize;
iOptionList.pszConnection = _dwEnableAutoDial? _szRasConnection:NULL;
iOptionList.dwOptionCount = 1;
iOptionList.pOptions = iOptions;
// set proxy type direct or proxy server
iOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
// set the PROXY_TYPE_PROXY and PROXY_TYPE_DIRECT or we won't connect to servers in bypass list
iOptions[0].Value.dwValue = _dwAccessType|PROXY_TYPE_DIRECT;
if(_dwAccessType&PROXY_TYPE_PROXY)
{
iOptionList.dwOptionCount = 3;
// set proxy server if exists
iOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
iOptions[1].Value.pszValue = _szProxy;
iOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
iOptions[2].Value.pszValue = _szProxyByPass;
}
InternetSetOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION ,(LPVOID)(&iOptionList),uSize);
}
void GetProxyOption2()
{
INTERNET_PER_CONN_OPTION_LIST iOptionList;
INTERNET_PER_CONN_OPTION iOptions[3];
ULONG uSize = sizeof(iOptionList);
iOptionList.dwSize = uSize;
iOptionList.pszConnection = _dwEnableAutoDial? _szRasConnection:NULL;
iOptionList.dwOptionCount = 3;
iOptionList.pOptions = iOptions;
// set proxy type direct or proxy server
iOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
iOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
iOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
if(InternetQueryOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION ,(LPVOID)(&iOptionList),&uSize))
{
_dwAccessType = iOptionList.pOptions[0].Value.dwValue;
if(_dwAccessType&PROXY_TYPE_PROXY)
{
lstrcpy(_szProxy,iOptionList.pOptions[1].Value.pszValue);
lstrcpy(_szProxyByPass,iOptionList.pOptions[2].Value.pszValue);
}
GlobalFree(iOptionList.pOptions[1].Value.pszValue);
GlobalFree(iOptionList.pOptions[2].Value.pszValue);
}
}
#endif //!defined(NOCOMMANDBAR) && !defined(NOOPTIONS)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -