来电铃声的设置在注册表中的HKEY_CURRENT_USER//ControlPanel//Sounds//RingTone0//Script下的值,就是所要播放的铃声文件。查看Windows Mobile下的注册表,用VS2005中Visual Studio Remote Tools->Remote Registry Editor工具就可以,不须其他的软件。
我们可以在电话来了后根据号码播放不同的铃声,然后在通话结束再将铃声恢复。下面是通过MAPI来实现
1.判断来电号码
VOID CALLBACK LineCallback(DWORD hDevice, DWORD dwMsg, DWORD dwCallbackInstance, DWORD dwParam1, DWORD dwParam2, DWORD dwParam3)
{
switch(dwMsg)
{
case LINE_CALLINFO://1L呼叫信息
{
switch(dwParam1)
{
case LINECALLINFOSTATE_CALLERID://0x00008000
{
LINECALLINFO *lpCallInfo;
lpCallInfo = (LINECALLINFO *)malloc(sizeof(LINECALLINFO)+1000);
memset(lpCallInfo, 0, sizeof(LINECALLINFO)+1000);
lpCallInfo->dwTotalSize = sizeof(LINECALLINFO)+1000;
while (1)
{
lineGetCallInfo( (HCALL)hDevice, lpCallInfo);
if (lpCallInfo->dwTotalSize < lpCallInfo->dwNeededSize)
lpCallInfo = (LINECALLINFO *)realloc(lpCallInfo,lpCallInfo->dwNeededSize);
else
break;
}
lstrcpy(szPhoneNumber,(LPTSTR)((LPSTR)((DWORD)lpCallInfo+(DWORD)lpCallInfo->dwCallerIDOffset)));
free(lpCallInfo);
// szPhoneNumber ---- 呼入的电话号码
//char strPhoneNumber[30 + 1] = {0};
//在这里根据号码来设置铃声
}
break;
case LINE_LINEDEVSTATE:
break;
case LINE_APPNEWCALL: //有呼叫进来23L
break;
case LINE_REPLY:
break;
case LINE_CALLSTATE: //呼叫状态2
{
case LINECALLSTATE_DISCONNECTED://挂机
{
RedoReg();
}
break;
}
break;
}
}
DWORD InitializeTAPI(void)
{
ReadReg();//读系统中默认的铃声设置
//在启动中加入初始化
if ( lineInitialize( &LineApp, g_hInst, (LINECALLBACK)LineCallback, g_szAppWndClass, &LineHandleCount) == 0)
{
LineHandles = new HLINE[LineHandleCount];
for(DWORD i = 0; i < LineHandleCount; i++)
{
int rc;
DWORD ver;
LINEEXTENSIONID extensionID;
if ( lineNegotiateAPIVersion( LineApp, i, 0x00010003, 0x00020000, &ver, &extensionID) == 0)
{
rc = lineOpen( LineApp,
i,
&LineHandles[i],
ver,
0,
(DWORD)0,
LINECALLPRIVILEGE_MONITOR|LINECALLPRIVILEGE_OWNER,
LINEMEDIAMODE_INTERACTIVEVOICE,
NULL);
}
}
}
return 0;
}
2,读取并保存系统铃声
void ReadReg(void)
{
HKEY hKey = NULL;
HKEY hSubKey = NULL;
LRESULT SS;
SS = RegOpenKeyEx(HKEY_CURRENT_USER ,L"//ControlPanel//Sounds//RingTone0", 0,0,&hKey);
if(ERROR_SUCCESS != SS)//打开注册表失败
RegCloseKey (hKey);
DWORD lpType = 1;
DWORD lpcbData = 80;
SS = RegQueryValueEx(hKey ,L"Script", (LPDWORD)NULL,NULL,(BYTE*)lpData,&lpcbData);
DWORD Result = GetLastError();
if(ERROR_SUCCESS != SS)//读键值失败
RegCloseKey (hKey);
}
3,更改铃声设置
void WriteReg(void)
{
LRESULT lr;
HRESULT hr = E_FAIL;
HKEY hKey = NULL;
DWORD dwDisposition;
TCHAR wszValue[60] = L"a";
lr = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("//ControlPanel//Sounds//RingTone0"),
0, NULL, 0, 0, NULL,
&hKey, &dwDisposition);
if (lr != ERROR_SUCCESS)
RegCloseKey(hKey);
lr = RegSetValueEx(hKey, L"Script", 0, REG_SZ, (LPBYTE) wszValue, (lstrlen(wszValue) + 1) * sizeof(TCHAR));
if (lr != ERROR_SUCCESS)
RegCloseKey(hKey);
RegCloseKey(hKey);
hKey = NULL;
}
4.通话结束,释放资源并恢复系统铃声
void ReleaseLine(void)
{
RedoReg();
for ( DWORD i=0; i<LineHandleCount; i++ )
{
lineClose( LineHandles[ i ] );
}
void ReadReg(void)
{
HKEY hKey = NULL;
HKEY hSubKey = NULL;
LRESULT SS;
SS = RegOpenKeyEx(HKEY_CURRENT_USER ,L"//ControlPanel//Sounds//RingTone0", 0,0,&hKey);
if(ERROR_SUCCESS != SS)//打开注册表失败
RegCloseKey (hKey);
DWORD lpType = 1;
DWORD lpcbData = 80;
SS = RegQueryValueEx(hKey ,L"Script", (LPDWORD)NULL,NULL,(BYTE*)lpData,&lpcbData);
DWORD Result = GetLastError();
if(ERROR_SUCCESS != SS)//读键值失败
RegCloseKey (hKey);
}
delete[] LineHandles;
lineShutdown( LineApp );
}
注意包含头文件的顺序,否则会出现很多编译错误;在EVC上表现的比较明显
原文地址:http://www.cppblog.com/zhangyq/archive/2009/02/22/74603.html