// WifiSwitch.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "BTHSwitch.h"
#include <windows.h>
#include <commctrl.h>
#include "bthutil.h"
#pragma comment(lib, "bthutil.lib")
#define MAX_LOADSTRING 100
#define WM_CTLCENTER WM_USER+112
#define WM_BTHSwitch WM_USER+7229
#define BTH_STATUS_ON 1
#define BTH_STATUS_OFF 0
HINSTANCE g_hInst; // 当前实例
HWND hWndDes = NULL;
int nRet;
DWORD dwMode;
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
nCmdShow = SW_HIDE;
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_BTHSWITCH));
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
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_BTHSWITCH));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING] = _T("Bluetooth Switcher"); // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING] = _T("TBWidget"); // 主窗口类名
g_hInst = hInstance; // 将实例句柄存储在全局变量中
SHInitExtraControls();
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_BTHSWITCH, szWindowClass, MAX_LOADSTRING);
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
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;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hWndDes = FindWindow(_T("TBClass"), _T("TBMainWnd"));
SendMessage(hWnd, WM_BTHSwitch, 0, 0);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_BTHSwitch:
nRet = BthGetMode(&dwMode);
if (nRet == ERROR_SUCCESS)
{
if (dwMode == BTH_CONNECTABLE) // enabled
{
nRet = BthSetMode(BTH_POWER_OFF);
if (nRet == ERROR_SUCCESS)
{
//off
SendMessage(hWndDes, WM_CTLCENTER, WM_BTHSwitch, BTH_STATUS_OFF);
}
}
else if (dwMode == BTH_POWER_OFF)
{
nRet = BthSetMode(BTH_CONNECTABLE);
if (nRet == ERROR_SUCCESS)
{
//on
SendMessage(hWndDes, WM_CTLCENTER, WM_BTHSwitch, BTH_STATUS_ON);
}
}
}
exit(0);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}