📄 aclock.cpp
字号:
// AClock.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "gx.h"
//GAPI显示以及硬件按键结构体变量
GXDisplayProperties g_gxdp; // GX struct
GXKeyList g_gxkl; // GX struct
#pragma comment(lib,"gx.lib")
#define TRAY_NOTIFYICON WM_USER + 2001
#define WM_TRAYAPP WM_USER + 2002
#define ID_TRAY 5000
// === Function Prototypes ====================================================
BOOL WINAPI MainDlgProc(
HWND,
UINT,
WPARAM,
LPARAM
);
// === Global Variables =======================================================
HINSTANCE g_hInst;
BOOL g_bWinHide = FALSE;
BOOL g_bPalmTrayAppActive = FALSE;
unsigned short Back[3600];
BOOL m_bStart=FALSE;
BOOL m_bNeedRestore=FALSE;
// Tray Icons and Notification ------------------------------
BOOL TrayMessage(
HWND hwnd,
DWORD dwMessage,
UINT uID,
HICON hIcon,
PTSTR pszTip
)
{
BOOL res = FALSE;
NOTIFYICONDATA tnd;
tnd.cbSize = sizeof(NOTIFYICONDATA);
tnd.hWnd = hwnd;
tnd.uID = uID;
tnd.uFlags = NIF_MESSAGE|NIF_ICON;
tnd.uCallbackMessage = TRAY_NOTIFYICON;
tnd.hIcon = hIcon;
tnd.szTip[0] = '\0';
res = Shell_NotifyIcon(dwMessage, &tnd);
return res;
}
void TrayIconDelete(
HWND hwnd,
UINT uID,
HICON hIcon,
PTSTR pszTip
)
{
TrayMessage(hwnd, NIM_DELETE, uID, hIcon, NULL);
}
void TrayIconModify(
HWND hwnd,
UINT uID,
HICON hIcon,
PTSTR pszTip
)
{
//animate icons
TrayMessage(hwnd, NIM_MODIFY, uID, hIcon, NULL);
}
void TrayIconAdd(
HWND hwnd,
UINT uID,
HICON hIcon,
PTSTR pszTip
)
{
TrayMessage(hwnd, NIM_ADD, uID, hIcon, NULL);
}
__inline DWORD RGBto16bit565(unsigned char r,unsigned char g,unsigned char b)
{
return (((WORD)r<<8)&0xf800)|(((WORD)g<<3)&0x07e0)|((WORD)b>>3);
}
void Line(unsigned short *m_pwBuffer,int wWidth,int x1,int y1,int x2,int y2,unsigned char r,unsigned char g,unsigned char b)
{
int i;
DWORD color=RGBto16bit565(r,g,b);
if(x1==x2)
{
//为竖线
if(y1<=y2)
{
for(i=y1;i<=y2;i++)
{
//pDC->SetPixel(x1,i,m_lPenColor);
*(m_pwBuffer+i*wWidth+x1)=color;
}
}
else
{
for(i=y2;i<=y1;i++)
{
//pDC->SetPixel(x1,i,m_lPenColor);
*(m_pwBuffer+i*wWidth+x1)=color;
}
}
return;
}
//为横线
if(y1==y2)
{
if(x1<=x2)
{
for(i=x1;i<=x2;i++)
{
//pDC->SetPixel(i,y1,m_lPenColor);
*(m_pwBuffer+y1*wWidth+i)=color;
}
}
else
{
for(i=x2;i<=x1;i++)
{
//pDC->SetPixel(i,y1,m_lPenColor);
*(m_pwBuffer+y1*wWidth+i)=color;
}
}
return;
}
//为斜线
float m=(y2-y1)*1.0/(x2-x1);
float p;
p=2*m-1;
if(m>0 && m<=1)
{
if(x1<x2)
{
while(x1<=x2)
{
*(m_pwBuffer+y1*wWidth+x1)=color;
x1++;
//pDC->SetPixel(x1++,y1,m_lPenColor);
if(p>=0)
{
p+=2*m-2;
y1++;
}
else
{
p+=2*m;
}
}
}
else
{
while(x2<=x1)
{
*(m_pwBuffer+y2*wWidth+x2)=color;
x2++;
//pDC->SetPixel(x2++,y2,m_lPenColor);
if(p>=0)
{
p+=2*m-2;
y2++;
}
else
{
p+=2*m;
}
}
}
return;
}
p=-2*m-1;
if(m<0 && m>=-1)
{
if(x1<x2)
{
while(x1<=x2)
{
*(m_pwBuffer+y1*wWidth+x1)=color;
x1++;
//pDC->SetPixel(x1++,y1,m_lPenColor);
if(p>=0)
{
p+=-2*m-2;
y1--;
}
else
{
p+=-2*m;
}
}
}
else
{
while(x2<=x1)
{
*(m_pwBuffer+y2*wWidth+x2)=color;
x2++;
//pDC->SetPixel(x2++,y2,m_lPenColor);
if(p>=0)
{
p+=-2*m-2;
y2--;
}
else
{
p+=-2*m;
}
}
}
return;
}
p=2/m-1;
if(m>1)
{
if(y1<y2)
{
while(y1<=y2)
{
*(m_pwBuffer+y1*wWidth+x1)=color;
y1++;
//pDC->SetPixel(x1,y1++,m_lPenColor);
if(p>=0)
{
p+=2/m-2;
x1++;
}
else
{
p+=2/m;
}
}
}
else
{
while(y2<=y1)
{
*(m_pwBuffer+y2*wWidth+x2)=color;
y2++;
//pDC->SetPixel(x2,y2++,m_lPenColor);
if(p>=0)
{
p+=2/m-2;
x2++;
}
else
{
p+=2/m;
}
}
}
return;
}
p=-2/m-1;
if(y1<y2)
{
while(y1<=y2)
{
*(m_pwBuffer+y1*wWidth+x1)=color;
y1++;
//pDC->SetPixel(x1,y1++,m_lPenColor);
if(p>=0)
{
p+=-2/m-2;
x1--;
}
else
{
p+=-2/m;
}
}
}
else
{
while(y2<=y1)
{
*(m_pwBuffer+y2*wWidth+x2)=color;
y2++;
//pDC->SetPixel(x2,y2++,m_lPenColor);
if(p>=0)
{
p+=-2/m-2;
x2--;
}
else
{
p+=-2/m;
}
}
}
}
void SaveBack(unsigned short *pBuffer,int x,int y)
{
int i;
for(i=0;i<60;i++)
{
memcpy(Back+i*60,pBuffer+(y+i-30)*g_gxdp.cxWidth+x-30,120);
}
}
void RestoreBack(unsigned short *pBuffer,int x,int y)
{
int i;
for(i=0;i<60;i++)
{
memcpy(pBuffer+(y+i-30)*g_gxdp.cxWidth+x-30,Back+i*60,120);
}
}
void DrawClock(unsigned short *buffer,int x,int y)
{
_SYSTEMTIME t;
GetLocalTime(&t);
//看画面有无刷新
if(*(buffer+220*g_gxdp.cxWidth+180)==12345)
{
RestoreBack(buffer,x,y);
}
else
{
SaveBack(buffer,x,y);
}
//先画边框
double c;
int x1,y1,x2,y2;
x1=x+29;
y1=y;
for(c=0.1;c<=6.3;c+=0.1)
{
x2=x+29*cos(c);
y2=y-29*sin(c);
Line(buffer,240,x1,y1,x2,y2,0,0,255);
x1=x2;
y1=y2;
}
//画指针
x1=x+18*sin((float)(t.wHour+(float)t.wMinute/60.0)/12.0*6.28);
y1=y-18*cos((float)(t.wHour+(float)t.wMinute/60.0)/12.0*6.28);
Line(buffer,240,x,y,x1,y1,255,255,0);
x1=x+22*sin((float)(t.wMinute)/60.0*6.28);
y1=y-22*cos((float)(t.wMinute)/60.0*6.28);
Line(buffer,240,x,y,x1,y1,0,255,0);
x1=x+24*sin((float)(t.wSecond)/60.0*6.28);
y1=y-24*cos((float)(t.wSecond)/60.0*6.28);
Line(buffer,240,x,y,x1,y1,0,0,255);
*(buffer+220*g_gxdp.cxWidth+180)=12345; //检测画面刷新使用
}
void RefreshScreen()
{
//注意两种色深模式的不同.
switch(g_gxdp.cBPP)
{
//16位彩色模式;
case 16:
{
unsigned short * pusLine=(unsigned short *)GXBeginDraw();
if(pusLine==NULL)
{
return; //无法绘图
}
//保存时钟区域的背景画面
if(!m_bStart)
{
SaveBack(pusLine,180,220);
m_bStart=TRUE;
}
//进行绘图
//按照当前时间绘画时钟
DrawClock(pusLine,180,220);
GXEndDraw(); //绘图完毕一定要结束
}
break;
default:
break;
}
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
int retcode;
g_hInst = hInstance; // Save instance handle in global
InitCommonControls();
retcode = DialogBox( g_hInst, MAKEINTRESOURCE(IDD_SETUP_DIALOG), NULL, (DLGPROC)MainDlgProc );
return(retcode);
}
// === Main Window (dialog) Proc ==============================================
BOOL WINAPI MainDlgProc(
HWND hDlg,
UINT Msg,
WPARAM wParam,
LPARAM lParam
)
{
switch (Msg)
{
case WM_CLOSE:
//Clean up on Close and remove tray icons
TrayIconDelete(hDlg, ID_TRAY, LoadIcon( g_hInst, MAKEINTRESOURCE(APP_ICON) ), NULL);
EndDialog(hDlg, TRUE);
return(TRUE);
case WM_INITDIALOG:
//On Pocket PC devices you normally create all Dialog's as fullscreen dialog's
// with an OK button in the upper corner.
SHINITDLGINFO shidi;
// Create a Done button and size it.
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
shidi.hDlg = hDlg;
//initialzes the dialog based on the dwFlags parameter
SHInitDialog(&shidi);
//Set Tray Icon
TrayIconAdd(hDlg, ID_TRAY, LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_TRAYAPP)), NULL);
SetForegroundWindow(hDlg); // make us come to the front
//打开GAPI显示
if(GXOpenDisplay(hDlg,GX_FULLSCREEN)==0)
{
return FALSE; //无法进行GAPI绘图
}
else
{
//取得显示属性
g_gxdp = GXGetDisplayProperties();
SetTimer(hDlg,1234,500,0);
}
return(TRUE);
case WM_COMMAND:
switch (GET_WM_COMMAND_ID(wParam,lParam))
{
case IDCANCEL:
g_bPalmTrayAppActive = FALSE;
TrayIconDelete(hDlg, ID_TRAY, LoadIcon( g_hInst, MAKEINTRESOURCE(IDI_TRAYAPP)), NULL);
EndDialog( hDlg, TRUE );
KillTimer(hDlg,1234);
GXCloseDisplay();
return(TRUE);
break;
case IDOK:
//Hide window
if(FALSE == g_bWinHide)
{
ShowWindow(hDlg,SW_HIDE);
g_bWinHide = TRUE;
}
//Active state
PostMessage(hDlg, WM_COMMAND, (WPARAM)WM_TRAYAPP,0);
return(TRUE);
break;
}
case TRAY_NOTIFYICON:
switch (lParam)
{
case WM_LBUTTONDOWN:
if (ID_TRAY == wParam)
{
g_bPalmTrayAppActive = FALSE;
if (TRUE == g_bWinHide)
{
ShowWindow(hDlg, SW_SHOW);
SetForegroundWindow(hDlg); // make us come to the front
g_bWinHide = FALSE;
}
else
{
g_bWinHide = TRUE;
}
}
break;
}
break;
case WM_TIMER:
RefreshScreen();
break;
}
return( FALSE );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -