📄 btsquirt.cpp
字号:
//======================================================================
// BtSquirt - A demonstration of a Bluetooth application
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 Douglas Boling
//======================================================================
#include <windows.h> // For all that Windows stuff
#include <commdlg.h> // For common dialogs
#include <winsock2.h> // Socket functions
#include <ws2bth.h> // Bluetooth extensions
#include <Msgqueue.h>
#include <aygshell.h> // Add WinMobile includes
#include "BtSquirt.h" // Program-specific stuff
#include "MyBTUtil.h" // My Bluetooth routines
#pragma comment( lib, "aygshell" ) // Link WinMobile lib for menubar
#pragma comment( lib, "ws2" ) // Link WinSock 2.0 lib
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
#include <bthutil.h> // Bluetooth Util API
#pragma comment( lib, "bthutil") // Link BT util lib
#endif
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("BtSquirt");
TCHAR szTitleText[128];
// Be sure to create your own GUID with GuidGen!
// {23EABC54-6923-480c-AC59-CDD83C154D87}
static GUID guidBtSquirt =
{ 0x23eabc54, 0x6923, 0x480c, { 0xac, 0x59, 0xcd, 0xd8,
0x3c, 0x15, 0x4d, 0x87 } };
HINSTANCE hInst; // Program instance handle
HWND hMain; // Main window handle
BOOL fContinue = TRUE; // Server thread cont. flag
BOOL fFirstSize = TRUE; // First WM_SIZE flag after lb
BOOL fFirstTime = TRUE; // First WM_SIZE flag
SOCKET s_sock;
#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
SHACTIVATEINFO sai; // Needed for PPC helper funcs
#endif
HANDLE hQRead = 0; // Used for thread safe print
HANDLE hQWrite = 0;
CRITICAL_SECTION csPrintf;
#define MAX_DEVICES 32
MYBTDEVICE btd[MAX_DEVICES]; // List of BT devices
int nDevs = 0; // Count of BT devices
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_SIZE, DoSizeMain,
WM_CHAR, DoCharMain,
WM_COMMAND, DoCommandMain,
MYMSG_ENABLESEND, DoEnableSendMain,
MYMSG_PRINTF, DoPrintfNotifyMain,
MYMSG_NEWDEV, DoAddDeviceMain,
WM_SETTINGCHANGE, DoPocketPCShell,
WM_ACTIVATE, DoPocketPCShell,
WM_DESTROY, DoDestroyMain,
};
// Command Message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
IDOK, DoMainCommandExit,
#else
IDOK, DoMainCommandSend,
#endif
IDCANCEL, DoMainCommandExit,
IDD_SENDFILE, DoMainCommandSend,
IDD_SCAN, DoMainCommandScan,
};
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPCMDLINE lpCmdLine, int nCmdShow) {
MSG msg;
int rc = 0;
// Initialize this instance.
hMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hMain == 0)
return TermInstance (hInstance, 0x10);
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
if ((hMain == 0) || !IsDialogMessage (hMain, &msg)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPCMDLINE lpCmdLine,
int nCmdShow){
WNDCLASS wc;
HWND hWnd;
HANDLE hThread;
int rc;
hInst = hInstance; // Save program instance handle.
// For all systems, if previous instance, activate it instead of us.
hWnd = FindWindow (szAppName, NULL);
if (hWnd) {
SetForegroundWindow ((HWND)((DWORD)hWnd | 0x01));
return 0;
}
// Init Winsock
WSADATA wsaData;
rc = WSAStartup (0x0202, &wsaData);
if (rc) {
MessageBox (NULL,TEXT("Error in WSAStartup"), szAppName, MB_OK);
return 0;
}
InitializeCriticalSection (&csPrintf);
// Create message queue. First for read access
MSGQUEUEOPTIONS mqo;
mqo.dwSize = sizeof (mqo);
mqo.dwFlags = MSGQUEUE_ALLOW_BROKEN;
mqo.dwMaxMessages = 16;
mqo.cbMaxMessage = 512;
mqo.bReadAccess = TRUE;
hQRead = CreateMsgQueue (NULL, &mqo);
// Open it again for write access
mqo.bReadAccess = FALSE;
hQWrite = OpenMsgQueue (GetCurrentProcess(), hQRead, &mqo);
// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = DLGWINDOWEXTRA; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL; // Application icon
wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (LTGRAY_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 0;
// Create main window.
hWnd = CreateDialog (hInst, szAppName, NULL, NULL);
// Return fail code if window not created.
if (!IsWindow (hWnd)) return 0;
GetWindowText (hWnd, szTitleText, dim (szTitleText));
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
// See if Bluetooth radio on.
DWORD dwBTStatus;
rc = BthGetMode (&dwBTStatus);
if (rc != ERROR_SUCCESS)
Add2List (hWnd, TEXT("Error querying BT radio status %d"), GetLastError());
else {
if (dwBTStatus == BTH_POWER_OFF) {
rc = MessageBox (hWnd, TEXT("The Bluetooth radio is currently off. ")
TEXT("do you want to turn it on?"), szAppName, MB_YESNO);
if (rc == IDYES) {
BthSetMode (BTH_DISCOVERABLE); // Make discoverable
BthGetMode (&dwBTStatus); // Update status
}
}
if (dwBTStatus == BTH_POWER_OFF)
Add2List (hWnd, TEXT("Bluetooth radio *** off ***"));
else if (dwBTStatus == BTH_CONNECTABLE)
Add2List (hWnd, TEXT("Bluetooth radio on, not discoverable!"));
else if (dwBTStatus == BTH_DISCOVERABLE)
Add2List (hWnd, TEXT("Bluetooth radio on and discoverable"));
}
#endif
// Create secondary thread for server function.
hThread = CreateThread (NULL, 0, ServerThread, hWnd, 0, 0);
if (hThread == 0) {
DestroyWindow (hWnd);
return 0;
}
CloseHandle (hThread);
// Post a message to have device discovery start
PostMessage (hWnd, WM_COMMAND, MAKEWPARAM (IDD_SCAN, BN_CLICKED),0);
ShowWindow (hWnd, nCmdShow); // Standard show and update calls
UpdateWindow (hWnd);
SetFocus (GetDlgItem (hWnd, IDD_SENDFILE));
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
WSACleanup ();
Sleep (0);
CloseMsgQueue (hQRead);
CloseMsgQueue (hQWrite);
return nDefRC;
}
//======================================================================
// Message handling procedures for main window
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
INT i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
SHINITDLGINFO shidi;
SHMENUBARINFO mbi; // For Pocket PC, create
memset(&mbi, 0, sizeof(SHMENUBARINFO)); // menu bar so that we
mbi.cbSize = sizeof(SHMENUBARINFO); // have a sip button
mbi.dwFlags = SHCMBF_EMPTYBAR;
mbi.hwndParent = hWnd;
SHCreateMenuBar(&mbi);
SendMessage(mbi.hwndMB, SHCMBM_GETSUBMENU, 0, 100);
// For Pocket PC, make dialog box full screen with PPC
// specific call.
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIZEDLG | SHIDIF_SIPDOWN;
shidi.hDlg = hWnd;
SHInitDialog(&shidi);
sai.cbSize = sizeof (sai);
SHHandleWMSettingChange(hWnd, wParam, lParam, &sai);
#endif
return 0;
}
//----------------------------------------------------------------------
// DoCharMain - Process WM_CHAR message for window.
//
LRESULT DoCharMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
if (wParam == '1')
PostMessage (hWnd, WM_COMMAND, MAKELONG (IDD_SCAN, 0),
(LPARAM)GetDlgItem (hWnd, IDD_SCAN));
else if (wParam == '2')
PostMessage (hWnd, WM_COMMAND, MAKELONG (IDD_SENDFILE, 0),
(LPARAM)GetDlgItem (hWnd, IDD_SENDFILE));
else if (wParam == '9')
PostMessage (hWnd, WM_COMMAND, MAKELONG (IDCANCEL, 0),
(LPARAM)GetDlgItem (hWnd, IDCANCEL));
return 0;
}
//----------------------------------------------------------------------
// DoSizeMain - Process WM_SIZE message for window.
//
LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
static RECT rectListbox;
RECT rect;
GetClientRect (hWnd, &rect);
if (fFirstTime) {
// First time through, get the position of the listbox for
// resizeing later. Store the distance from the sides of
// the listbox control to the side of the parent window
if (IsWindow (GetDlgItem (hWnd, IDD_INTEXT))) {
GetWindowRect (GetDlgItem (hWnd, IDD_INTEXT), &rectListbox);
MapWindowPoints (HWND_DESKTOP, hWnd, (LPPOINT)&rectListbox,2);
rectListbox.right = rect.right - rectListbox.right;
rectListbox.bottom = rect.bottom - rectListbox.bottom;
SetWindowPos (GetDlgItem (hWnd, IDD_INTEXT), 0, rect.left+5,
rectListbox.top, rect.right-10,
rect.bottom - rectListbox.top - 5,
SWP_NOZORDER);
fFirstTime = FALSE;
}
}
#endif
if (fFirstSize) {
EnableWindow (GetDlgItem (hWnd, IDD_SENDFILE), FALSE);
EnableWindow (GetDlgItem (hWnd, IDD_SCAN), FALSE);
fFirstSize = FALSE;
}
//SetFocus (hWnd);
return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
WORD idItem, wNotifyCode;
HWND hwndCtl;
INT i;
// Parse the parameters.
idItem = (WORD) LOWORD (wParam);
wNotifyCode = (WORD) HIWORD (wParam);
hwndCtl = (HWND) lParam;
// Call routine to handle control message.
for (i = 0; i < dim(MainCommandItems); i++) {
if (idItem == MainCommandItems[i].Code)
return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
wNotifyCode);
}
return 0;
}
//----------------------------------------------------------------------
// DoEnableSendMain - Process user message to enable send button
//
LRESULT DoEnableSendMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
int i;
EnableWindow (GetDlgItem (hWnd, IDD_SENDFILE), lParam);
EnableWindow (GetDlgItem (hWnd, IDD_SCAN), TRUE);
i = (int)SendDlgItemMessage (hWnd, IDD_DEVICES, CB_GETCURSEL, 0, 0);
if (i == -1)
SendDlgItemMessage (hWnd, IDD_DEVICES, CB_SETCURSEL, 0, 0);
SetWindowText (hWnd, szTitleText);
return 0;
}
//----------------------------------------------------------------------
// DoAddDeviceMain - Process user message to add to device list
//
LRESULT DoAddDeviceMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
SendDlgItemMessage (hWnd, IDD_DEVICES, CB_ADDSTRING, 0, lParam);
return 0;
}
//----------------------------------------------------------------------
// DoPrintfNotifyMain - Process printf notify message
//
LRESULT DoPrintfNotifyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
TCHAR szBuffer[512];
int rc;
DWORD dwLen = 0;
DWORD dwFlags = 0;
memset (szBuffer, 0, sizeof (szBuffer));
rc = ReadMsgQueue (hQRead, (LPBYTE)szBuffer, sizeof (szBuffer),
&dwLen, 0, &dwFlags);
if (rc) {
if (dwFlags & MSGQUEUE_MSGALERT)
SetWindowText (hWnd, szBuffer);
else {
rc = SendDlgItemMessage (hWnd, IDD_INTEXT, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)szBuffer);
if (rc != LB_ERR)
SendDlgItemMessage (hWnd, IDD_INTEXT, LB_SETTOPINDEX,rc,
(LPARAM)(LPCTSTR)szBuffer);
}
}
return 0;
}
//----------------------------------------------------------------------
// DoPocketPCShell - Process Pocket PC required messages
//
LRESULT DoPocketPCShell (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
if (wMsg == WM_SETTINGCHANGE)
return SHHandleWMSettingChange(hWnd, wParam, lParam, &sai);
if (wMsg == WM_ACTIVATE)
return SHHandleWMActivate(hWnd, wParam, lParam, &sai, 0);
#endif
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
fContinue = FALSE; // Shut down server thread.
closesocket (s_sock);
Sleep (0); // Pass on timeslice.
PostQuitMessage (0);
return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -