📄 rilsample.cpp
字号:
// RILSample.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "RILSample.h"
#include <windows.h>
#include <commctrl.h>
#include "ril.h"
#pragma comment(lib, "ril.lib")
#define MAX_LOADSTRING 100
#define WM_SQCHANGE WM_USER + 150
// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndMenuBar; // menu bar handle
HRIL g_hRil; // ril handle
RILRESULT g_srr; // ril result
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void CALLBACK RilResultProc(DWORD dwCode, HRESULT hrCmdID, const void* lpData, DWORD cbData, DWORD dwParam);
void CALLBACK RilNotifyProc(DWORD dwCode, const void* lpData, DWORD cbData, DWORD dwParam);
LPCTSTR UseString( UINT uID )
{
//Return a pointer to a const resource string
return (LPCTSTR)LoadString( g_hInst, uID, NULL, 0 );
}
DWORD GetSignalStrength(LPRILSIGNALQUALITY pRilSQ)
{
if ((pRilSQ->dwParams & RIL_PARAM_SQ_SIGNALSTRENGTH) &&
(pRilSQ->dwParams & RIL_PARAM_SQ_MAXSIGNALSTRENGTH) &&
(pRilSQ->dwParams & RIL_PARAM_SQ_MINSIGNALSTRENGTH))
{
return 100*(pRilSQ->nSignalStrength - pRilSQ->nMinSignalStrength) / (pRilSQ->nMaxSignalStrength - pRilSQ->nMinSignalStrength);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_RILSAMPLE));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_RILSAMPLE));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name
g_hInst = hInstance; // Store instance handle in our global variable
// SHInitExtraControls should be called once during your application's initialization to initialize any
// of the device specific controls such as CAPEDIT and SIPPREF.
SHInitExtraControls();
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_RILSAMPLE, szWindowClass, MAX_LOADSTRING);
//If it is already running, then focus on the window, and exit
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The "| 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}
if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
// When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;
GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL fRilOpen;
static BOOL fProcessSQNotify;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
DWORD dwWait;
LPRILSIGNALQUALITY pRilSQ = NULL;
TCHAR szResult[MAX_LOADSTRING];
static HWND s_hButton1, s_hButton2;
static HWND s_hWndStaticText;
static SHACTIVATEINFO s_sai;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDB_BUTTON1:
fProcessSQNotify = FALSE;
// 获取当前信号强度
g_srr.hrID = RIL_GetSignalQuality(g_hRil);
// 等待结果
dwWait = WaitForSingleObject(g_srr.hEvent, 5000);
if (dwWait == WAIT_TIMEOUT)
{
// RIL函数返回超时
DEBUGMSG(1, (TEXT("Timed out while waiting on RIL call!\r\n")));
SetWindowText(s_hWndStaticText, UseString(IDS_RIL_TIMEOUT));
}
if (ISRESULTANDDATAOK())
{
pRilSQ = (RILSIGNALQUALITY *)g_srr.pData;
wsprintf(szResult, UseString(IDS_SIGNALQUALITY_RESULT), GetSignalStrength(pRilSQ));
SetWindowText(s_hWndStaticText, szResult);
}
else
{
SetWindowText(s_hWndStaticText, UseString(IDS_RIL_RETURNERROR));
}
break;
case IDB_BUTTON2:
fProcessSQNotify = TRUE;
break;
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_SQCHANGE:
if (fProcessSQNotify)
{
pRilSQ = (RILSIGNALQUALITY *)lParam;
wsprintf(szResult, UseString(IDS_SIGNALQUALITY_RESULT), GetSignalStrength(pRilSQ));
SetWindowText(s_hWndStaticText, szResult);
}
break;
case WM_CREATE:
SHMENUBARINFO mbi;
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;
if (!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}
// 注册一个RIL Proxy, 注册成功后,可以调用RIL函数,可以接收指定类型的通知消息
g_srr.hrID = RIL_Initialize(1, RilResultProc, RilNotifyProc, RIL_NCLASS_MISC, (DWORD)hWnd, &g_hRil);
if (FAILED(g_srr.hrID))
{
// 注册RIL Proxy失败,无法操作RIL
DEBUGMSG(TRUE, (TEXT("Can't initialize RIL. hr = 0x%8x\r\n"), g_srr.hrID));
MessageBox(hWnd, UseString(IDS_RILINIT_FAILED), UseString(IDS_TIPS), MB_OK | MB_ICONINFORMATION);
}
else
{
fRilOpen = TRUE;
}
g_srr.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
// 创建按钮控件
s_hButton1 = CreateWindowEx(0,TEXT("BUTTON"),
UseString(IDS_GETSIGNALQUALITY),
WS_CHILD | BS_PUSHBUTTON | WS_TABSTOP,
20, 30, 200, 40,
hWnd,(HMENU)IDB_BUTTON1,
g_hInst,
NULL);
s_hButton2 = CreateWindowEx(0,TEXT("BUTTON"),
UseString(IDS_SIGNALQUALITY),
WS_CHILD | BS_PUSHBUTTON | WS_TABSTOP,
20, 100, 200, 40,
hWnd,(HMENU)IDB_BUTTON2,
g_hInst,
NULL);
ShowWindow(s_hButton1, SW_SHOWNORMAL);
ShowWindow(s_hButton2, SW_SHOWNORMAL);
// 创建静态文本控件,用来显示结果
s_hWndStaticText = CreateWindowEx( 0, TEXT("STATIC"), UseString(IDS_RESULT),
WS_VISIBLE | WS_CHILD, 20, 160, 400, 80, hWnd, NULL, g_hInst, NULL );
// Initialize the shell activate info structure
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(g_hWndMenuBar);
// 注销RIL Proxy
RIL_Deinitialize(g_hRil);
// 关闭事件量
CloseHandle(g_srr.hEvent);
PostQuitMessage(0);
break;
case WM_ACTIVATE:
// Notify shell of our activate message
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Create a Done button and size it.
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
case WM_CLOSE:
EndDialog(hDlg, message);
return TRUE;
}
return (INT_PTR)FALSE;
}
// RIL回调函数,用于接收RIL_XXX函数的结果,RIL_XXX函数是异步返回
void CALLBACK RilResultProc(DWORD dwCode, HRESULT hrCmdID, const void* lpData, DWORD cbData, DWORD dwParam)
{
delete g_srr.pData;
if (MAX_BUFFER_ALLOCATION > cbData)
{
g_srr.pData = new BYTE[cbData];
}
else
{
// Incoming buffer size from notification is way too big
// and may cause problems.
ASSERT(FALSE);
g_srr.pData = NULL;
}
if (g_srr.pData)
{
g_srr.dwResultCode = dwCode;
g_srr.dwDataSize = cbData;
memcpy(g_srr.pData, (LPBYTE) lpData, cbData);
}
else
{
// Out of memory
g_srr.hrID = E_OUTOFMEMORY;
}
SetEvent(g_srr.hEvent);
}
// RIL回调函数,用于接收RIL的通知消息
void CALLBACK RilNotifyProc(DWORD dwCode, const void* lpData, DWORD cbData, DWORD dwParam)
{
switch (dwCode)
{
// 我们只关心信号强度的改变
case RIL_NOTIFY_SIGNALQUALITY:
if (lpData && (cbData > 0))
{
SendMessage((HWND)dwParam, WM_SQCHANGE, 0, (DWORD)lpData);
}
break;
default:
// 其他通知消息不处理
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -