📄 irdetect.cpp
字号:
// irdetect.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
#define MAX_PORTS 6
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
HANDLE hListBox;
HANDLE hBtnStart;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int DetectIRPort(void);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_IRDETECT, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_IRDETECT);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HANDLE, 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;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_BORDER | WS_SYSMENU | WS_CAPTION | WS_VISIBLE,
0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: int DetectIRPort(void)
//
// PURPOSE: Detects the RAW IR port on the device.
//
// RETURNS: int indicating port number of IR port
//
int DetectIRPort(void)
{
int i, result;
TCHAR * szPort;
TCHAR szNum[4];
HANDLE hPort;
result = 0; //zero indicates error, could not find port
szPort = (TCHAR *)LocalAlloc(LMEM_ZEROINIT, MAX_PATH);
for (i=1; i <= MAX_PORTS; i++)
{
_tcscpy(szPort, TEXT("COM"));
_itow(i, szNum, 10); //convert i to string
_tcscat(szPort, szNum);
_tcscat(szPort, TEXT(":"));
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)TEXT("Now testing port:"));
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)szPort);
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)TEXT("..."));
hPort = CreateFile(szPort, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hPort != INVALID_HANDLE_VALUE)
{
if (EscapeCommFunction(hPort, SETIR))
{
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)TEXT("IR Port Found!!!!!!!!!!!!!!!!!!"));
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)szPort);
result = i;
CloseHandle(hPort);
break;
}
}
CloseHandle(hPort);
}
LocalFree(szPort);
return result;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// 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)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
RECT rt;
switch (message)
{
case WM_CREATE:
GetClientRect(hWnd, &rt);
hListBox = CreateWindow(TEXT("LISTBOX"),TEXT(""), WS_BORDER | WS_VISIBLE | WS_VSCROLL,
0, 10, rt.right-90, rt.bottom-30, hWnd, NULL, hInst, 0);
hBtnStart = CreateWindow(TEXT("BUTTON"),TEXT("Detect"), WS_VISIBLE,
rt.right-90, 10, 50 , 21, hWnd, NULL, hInst, 0);
SetFocus(hBtnStart);
break;
case WM_COMMAND:
if ((HWND)lParam == hBtnStart)
{
DetectIRPort();
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
GetClientRect(hWnd, &rt);
rt.top = rt.bottom - 15;
DrawText(hdc, szHello, _tcslen(szHello), &rt, DT_LEFT);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -