📄 cpadclock.cpp
字号:
// cPadClock.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "stdio.h"
// Maintain a clock display on the cPad.
class CcPadClock :
// Derive from Synaptics device and display events.
public _ISynDeviceEvents,
public _ISynDisplayEvents
{
public:
CcPadClock(HINSTANCE hInstance)
{
m_hInstance = hInstance;
}
bool OnCreate(HWND hWnd)
{
m_hWnd = hWnd;
long lHandle = -1;
// Set the taskbar icon
HICON hSmallIcon = (HICON) LoadImage (m_hInstance,
MAKEINTRESOURCE (IDI_CLOCK), IMAGE_ICON,
GetSystemMetrics (SM_CXSMICON), GetSystemMetrics (SM_CYSMICON),
LR_DEFAULTCOLOR);
SendMessage (hWnd, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM) hSmallIcon);
DeleteObject(hSmallIcon);
if (CoInitialize(0) ||
CoCreateInstance(_uuidof(SynAPI), 0,
CLSCTX_INPROC_SERVER, _uuidof(ISynAPI), (void **) &m_pAPI) ||
m_pAPI->Initialize() ||
m_pAPI->FindDevice(SE_ConnectionAny, SE_DevicecPad, &lHandle) ||
m_pAPI->CreateDevice(lHandle, &m_pDevice) ||
m_pDevice->CreateDisplay(&m_pDisplay) ||
m_pDisplay->Acquire(SE_AcquireCooperative))
{
return false; // Couldn't initialize properly.
}
// Setup to receive device packets.
m_pDevice->SetSynchronousNotification(this);
m_pDevice->CreatePacket(&m_pPacket);
// Setup to receive display acquisition notifications.
m_pDisplay->SetSynchronousNotification(this);
m_pDisplay->GetDC(&m_hdc);
m_hClockFont = CreateClockFont(m_hdc, 36);
// Act like a timer message just arrived.
OnTimer();
return true;
}
// Redraw the cPad display every second.
void OnTimer()
{
DrawClock();
m_pDisplay->FlushDC(SE_FlushSynchronous);
m_pDisplay->SetProperty(SP_BackLightState, 1);
}
// Examine device packets and if a tap occurs in the exit icon in the
// upper right hand corner, exit.
long STDMETHODCALLTYPE OnSynDevicePacket(long lSeq)
{
m_pDevice->LoadPacket(m_pPacket);
long lState;
m_pPacket->GetProperty(SP_FingerState, &lState);
if (lState & SF_FingerPresent)
{
// Store last finger down position.
m_pPacket->GetProperty(SP_X, &m_lLastX);
m_pPacket->GetProperty(SP_Y, &m_lLastY);
}
if (lState & SF_FingerTap)
{
// A tap has occured, now examine the last finger present position
// to see if the tap occured in the exit icon.
long lPixelX, lPixelY;
m_pDisplay->TouchToPixel(m_lLastX, m_lLastY, &lPixelX, &lPixelY);
if (lPixelX >= 208 && lPixelY <= 32) {
// Clear the display dc.
RECT rect;
rect.top = 0; rect.bottom = 160; rect.left = 0; rect.right = 240;
FillRect(m_hdc, &rect, (HBRUSH) GetStockObject(WHITE_BRUSH));
m_pDisplay->FlushDC(SE_FlushSynchronous);
DestroyWindow(m_hWnd);
}
}
return 0;
}
long STDMETHODCALLTYPE OnSynDisplayMessage(long lMessage)
{
switch(lMessage)
{
case SE_AcquisitionLost:
// The display is now controlled elsewhere, turn off finger data.
m_pDevice->SetSynchronousNotification(0);
break;
case SE_AcquisitionGained:
// The display is back, now look at finger data to potentially decide
// to exit.
m_pDevice->SetSynchronousNotification(this);
break;
default:
break;
}
return 0;
}
HFONT CreateClockFont(HDC hdc, int iPointSize)
{
LOGFONT lf;
GetObject(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT),
&lf);
lf.lfHeight = -MulDiv(iPointSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
HFONT hFont = CreateFont(lf.lfHeight, lf.lfWidth,
lf.lfEscapement, lf.lfOrientation, lf.lfWeight,
lf.lfItalic, lf.lfUnderline, lf.lfStrikeOut, lf.lfCharSet,
lf.lfOutPrecision, lf.lfClipPrecision, lf.lfQuality,
lf.lfPitchAndFamily, lf.lfFaceName);
return hFont;
}
void DrawClock()
{
RECT rect;
// Clear the display dc.
rect.top = 0; rect.bottom = 160; rect.left = 0; rect.right = 240;
FillRect(m_hdc, &rect, (HBRUSH) GetStockObject(WHITE_BRUSH));
// Draw the exit and Synaptics logo icons.
DrawBitmap(208, 0, 32, IDB_EXIT);
DrawBitmap(0, 144, 16, IDB_SYNLOGO);
// Draw the Synaptics SDK text.
rect.top = 144; rect.bottom = 160; rect.left = 20; rect.right = 240;
SelectObject(m_hdc, GetStockObject(SYSTEM_FONT));
DrawText(m_hdc, "Synaptics COM SDK", -1, &rect, DT_LEFT);
// Get the time and draw the clock.
SYSTEMTIME SystemTime;
GetLocalTime(&SystemTime);
SystemTime.wHour = SystemTime.wHour%12 ? SystemTime.wHour%12 : 12;
char buf[256];
sprintf(buf, "%d:%.2d:%.2d", SystemTime.wHour, SystemTime.wMinute,
SystemTime.wSecond);
rect.top = 0; rect.bottom = 160; rect.left = 0; rect.right = 240;
SelectObject(m_hdc, m_hClockFont);
DrawText(m_hdc, buf, -1, &rect,
DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
// Draw a square bitmap from our resource fork at a particular location.
void DrawBitmap(int iX, int iY, int iSize, int iResource)
{
HBITMAP hMap = LoadBitmap(m_hInstance, MAKEINTRESOURCE(iResource));
HDC m_hMapDC = CreateCompatibleDC(m_hdc);
SelectObject(m_hMapDC, hMap);
BitBlt(m_hdc, iX, iY, iSize, iSize, m_hMapDC, 0, 0, SRCCOPY);
DeleteDC(m_hMapDC);
DeleteObject(hMap);
}
HINSTANCE m_hInstance;
HWND m_hWnd;
ISynAPI *m_pAPI;
ISynDevice *m_pDevice;
ISynDisplay *m_pDisplay;
ISynPacket *m_pPacket;
HDC m_hdc;
HFONT m_hClockFont;
long m_lLastX;
long m_lLastY;
};
// Standard window procedure.
LRESULT APIENTRY WndProc(HWND hwnd, UINT uMsg, WPARAM wParam,
LPARAM lParam)
{
// Get the clock object that was saved when the window was created.
CcPadClock *pThis = (CcPadClock *) GetWindowLong(hwnd, GWL_USERDATA);
switch (uMsg) {
case WM_CREATE:
{
// Save away the clock object passed to CreateWindow.
CREATESTRUCT *pCreate = (CREATESTRUCT *) lParam;
SetWindowLong(hwnd, GWL_USERDATA,
(long) pCreate->lpCreateParams);
pThis = (CcPadClock *) pCreate->lpCreateParams;
}
// If clock initialization fails, exit.
if (!pThis->OnCreate(hwnd))
DestroyWindow(hwnd);
// Register for timer messages once a second.
SetTimer(hwnd, 1, 1000, 0);
break;
case WM_TIMER:
// Dispatch the timer message to the clock object.
pThis->OnTimer();
break;
case WM_DESTROY:
PostQuitMessage(0);
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
// Application entry point.
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
// Create the clock object.
CcPadClock clock(hInstance);
// Register the window class.
WNDCLASS WC;
WC.style = 0;
WC.lpfnWndProc = WndProc;
WC.cbClsExtra = 0;
WC.cbWndExtra = 0;
WC.hInstance = hInstance;
WC.hIcon = 0;
WC.hCursor = 0;
WC.hbrBackground = 0;
WC.lpszMenuName = NULL;
WC.lpszClassName = "cPadClockClass";
RegisterClass (&WC);
// Create the app window. It must be visible to get a taskbar button.
// It must have a system menu to get a taskbar button icon.
// Position it off the screen so that it won't be seen.
HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, "cPadClockClass",
"cPadClock", WS_VISIBLE | WS_SYSMENU,
-1000, -1000, 0, 0, 0, 0, hInstance, &clock);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
DispatchMessage(&msg);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -