关键词:工作记录、关闭浩方平台提示窗口、定时关机、蜂鸣器报警
我自己写的程序,就是为了方便自己,可能里面有些内容对很多人没有用处,但是还是应该对某些人群有用处的;在这里写出来是为了提供一个思路或者留下自己的想法。我们公司软件部要求每天写工作记录、明天计划,12点下班吃饭,下午17点30放工;平时我喜欢上浩方平台玩游戏,经常使用BT下载电影。为了方便以及提醒自己,写了下面所述的一个程序供自己使用。
以前,为了提醒自己中午下班前记住吃饭,在11点58分是使用电脑报警,而我的办公电脑是没有音箱的,同时也是为了不影响同事工作,因此使用蜂鸣器报警。我现在使用手机闹钟提醒吃饭、下班了。可以如果你的手机没有多个闹钟的话,还是可以使用这部分功能的。蜂鸣器发声主要是下面的代码实现:
/* blNT:NT、2K、XP系统 delay:发声时间间隔 freq:声音频率 */
void playWarnningSound(bool blNT, int delay, int freq)
{
if(blNT)
{ Beep(freq, delay); return; }
const int scale = 1193180;
WORD freqTemp = (WORD)(scale/freq);
#pragma warn -8002
asm /* 发声 */
{
in al, 0x61;
or al, 3;
out 0x61, al;
mov al, 0xb6;
out 0x43, al;
mov ax, freqTemp;
out 0x42,al;
mov al, ah;
out 0x42, al;
}
Sleep(delay);
asm /* 关闭声音 */
{
in al, 0x61;
and al, 0xfc;
out 0x61, al;
}
#pragma warn +8002
}
下班前需要编写工作记录、下个工作日计划,我的记录是编写WORD文档,为了简单,先将前一个工作日的文档拷贝为当前文档,然后打开修改。实现主要代码如下:
void __fastcall TfrmMain::OpenJobFile(void)
{
if(blOpenJobFile) return;
blOpenJobFile = true;
/* 周六、日不打开文件 没有处理 5.1 10.1 等情况,感兴趣的话可自己处理 */
if((1==Now().DayOfWeek()) || (7==Now().DayOfWeek())) return;
/* ShowMessage(IntToStr(Now().DayOfWeek())); */
AnsiString LastName, CurName;
if(2==Now().DayOfWeek()) /* 今天为星期一 */
LastName = FormatDateTime("yyyymmdd", (Now()-3))+ ".doc";
else LastName = FormatDateTime("yyyymmdd", (Now()-1))+ ".doc";
CurName = FormatDateTime("yyyymmdd", Now())+ ".doc";
if( ! FileExists(asFilePath+ "//"+ CurName))
{
if(FileExists(asFilePath+ "//"+ LastName))
CopyFile((asFilePath+ "//"+ LastName).c_str(), (asFilePath+ "//"+ CurName).c_str(), true);
else if(FileExists(asFilePath+ "//"+ asFileName))
CopyFile((asFilePath+ "//"+ asFileName).c_str(), (asFilePath+ "//"+ CurName).c_str(), true);
}
if(FileExists(asFilePath+ "//"+ CurName))
{
ShellExecute(NULL, "open", asFilePath.c_str(), NULL, NULL, SW_SHOWMAXIMIZED);
ShellExecute(NULL, "open", (asFilePath+ "//"+ CurName).c_str(), NULL, NULL, SW_SHOWMAXIMIZED);
}
else ShowMessage((AnsiString)"不能找到"+ asFilePath+ "//"+ CurName+ " 文件!");
}
实现打开BT程序就非常简单了,等下班之后或者周末自动打开BT程序,仅仅需要使用INI文件记录BT程序的完整路径即可。
我的浩方平台是免费的,经常会弹出一些提示窗口,令我非常讨厌,因此我就想办法自动关闭这些窗口。刚开始使用SPY查找这些窗口非常顺利,都是类名为:"Afx:610000:0:10011:1100064:0"、"Afx:620000:0:10011:1100063:0"之类的窗口,但是可能第二次启动计算机之后会有变化,我的解决方法如下:
int m, n;
char strClassName[100];
for(m=60; m<70; m++)
for(n=60; n<70; n++)
{
wsprintf(strClassName, "Afx:%d0000:0:10011:11000%d:0", m, n);
HWND hwnd = FindWindowEx(NULL, NULL, strClassName, NULL);
if(NULL != hwnd) PostMessage(hwnd, WM_CLOSE, NULL, NULL);
}
特别注意:我的方法比较极端,如果你使用了该代码可能会出现关闭另外的程序或者窗口,这个就不是我能限制的啦。不过我用了两年多都没有出现过,如果你发现了,只能是RPWT啦。^_^!
最后我的程序还有定时关机功能,这个对大家来说都是很简单的啦。
对于整个程序,我没有仔细调试,不知道是不是有很严重的错误,如果各位调试出来的话请告诉我一下:benleak at 126.com 谢谢!
我程序的所有代码如下:
/* JobRec.cpp Project文件*/
#include <vcl.h>
#pragma hdrstop
USERES("JobRec.res");
USEFORM("fmMain.cpp", frmMain);
AnsiString asCurrentDir;
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->Title = "记录工作文件";
asCurrentDir = ExtractFileDir(Application->ExeName);
Application->CreateForm(__classid(TfrmMain), &frmMain);
#ifndef _DEBUG /* Debug编译时显示主窗口 */
Application->ShowMainForm = false;
#endif
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
/* fmMain.h 主窗口头文件 */
//---------------------------------------------------------------------------
#ifndef fmMainH
#define fmMainH
//---------------------------------------------------------------------------
#define WM_PLAYMOVE WM_USER+9
#define WM_PLAYHIGH WM_USER+10
#define WM_PLAYLOW WM_USER+11
#define WM_PLAYEND WM_USER+12
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TfrmMain : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1;
TEdit *Edit2;
TTimer *Timer1;
TButton *btnReStart;
TButton *btnReLogin;
TButton *btnClose;
TCheckBox *CheckBox1;
TLabel *Label1;
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall Timer1Timer(TObject *Sender);
void __fastcall btnReStartClick(TObject *Sender);
void __fastcall btnReLoginClick(TObject *Sender);
void __fastcall btnCloseClick(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
private: // User declarations
UINT m_UStatus;
bool ifShow;
bool blOpenJobFile;
bool blOpenBT;
bool blOpenLunchWarn, blOpenOfficeWarn;
unsigned int iconmessage;/* 定义的消息 */
void __fastcall AddTrayIcon(void);
void __fastcall RemoveTrayIcon(void);
void __fastcall OpenJobFile(void);
void __fastcall OpenBT(void);
void __fastcall OpenLunchOffice(bool Lunch);
public: // User declarations
__fastcall TfrmMain(TComponent* Owner);
virtual void __fastcall WndProc(Messages::TMessage& Message);
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmMain *frmMain;
//---------------------------------------------------------------------------
#endif
/* fmMain.cpp 主窗口实现 */
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <shellapi.h>
#include <inifiles.hpp>
#include "fmMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
AnsiString asFilePath;
AnsiString asFileName;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
m_UStatus = -1;
blOpenBT = false;
blOpenJobFile = false;
blOpenLunchWarn = false;
blOpenOfficeWarn = false;
}
/***************************************************************************
* Function Name : playWarnningSound
* Description : PC 喇叭或者扬声器报警
* Return : -
* Parameters : delay - 持续时间
: freq - 频率
* Author : Benleak
* Date : 2004/03/24
***************************************************************************/
void playWarnningSound(bool blNT, int delay, int freq)
{
if(blNT)
{ Beep(freq, delay); return; }
const int scale = 1193180;
WORD freqTemp = (WORD)(scale/freq);
#pragma warn -8002 /* 去除BCB 编译内嵌汇编代码警告 */
asm /* 发声 */
{
in al, 0x61;
or al, 3;
out 0x61, al;
mov al, 0xb6;
out 0x43, al;
mov ax, freqTemp;
out 0x42,al;
mov al, ah;
out 0x42, al;
}
Sleep(delay);
asm /* 关闭声音 */
{
in al, 0x61;
and al, 0xfc;
out 0x61, al;
}
#pragma warn +8002
}
/***************************************************************************
* Function Name : PlayWarnSoundThread
* Description : 播放报警声音线程
* Return : -
* Parameters : -
* Author : Benleak
* Date : 2004/03/24
***************************************************************************/
HANDLE RunPlaySound = NULL; /* 为了移植方便,不使用类变量 */
WPARAM warnningSoundStatus;
bool blRunning;
DWORD WINAPI PlayWarnSoundThread(LPVOID pParam)
{
int i;
bool blNT;
blRunning = true;
DWORD dwVersion = GetVersion();
if (dwVersion < 0x80000000) blNT = true;
else blNT = false;
RunPlaySound = CreateEvent(NULL, false, false, NULL);
for(; blRunning; )
{
if(WaitForSingleObject(RunPlaySound, 1000)==WAIT_TIMEOUT)
continue;
switch(warnningSoundStatus)
{
case WM_PLAYMOVE:
for(i=0;i<25;i++) { playWarnningSound(blNT, 200, 500); Sleep(200); }
break;
case WM_PLAYHIGH:
for(i=0;i<50;i++) { playWarnningSound(blNT, 100, 700); Sleep(100); }
break;
case WM_PLAYLOW:
for(i=0;i<16;i++) { playWarnningSound(blNT, 300, 400); Sleep(300); }
break;
case WM_PLAYEND:
for(i=0;i<25;i++) { playWarnningSound(blNT, 200, 600); Sleep(200); }
break;
default :
break;
}
}
CloseHandle(RunPlaySound);
RunPlaySound = NULL;
return true;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormClose(TObject *Sender, TCloseAction &Action)
{
blRunning = false;
if( ! CheckBox1->Checked)
{
RemoveTrayIcon();
return;
}
RemoveTrayIcon();
DWORD dwVersion = GetVersion();
if (dwVersion < 0x80000000) /* NT */
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
ExitWindowsEx(m_UStatus, 0);
}
else /* 9X */
ExitWindowsEx(m_UStatus, 0);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::Timer1Timer(TObject *Sender)
{
/* HWND hwnd = FindWindowEx(NULL, NULL, "Afx:610000:0:10011:1100064:0", NULL);
if(NULL != hwnd)
{
PostMessage(hwnd, WM_CLOSE, NULL, NULL);
}
hwnd = FindWindowEx(NULL, NULL, "Afx:620000:0:10011:1100064:0", NULL);
if(NULL != hwnd)
{
PostMessage(hwnd, WM_CLOSE, NULL, NULL);
}
hwnd = FindWindowEx(NULL, NULL, "Afx:610000:0:10011:1100063:0", NULL);
if(NULL != hwnd)
{
PostMessage(hwnd, WM_CLOSE, NULL, NULL);
}
hwnd = FindWindowEx(NULL, NULL, "Afx:620000:0:10011:1100063:0", NULL);
if(NULL != hwnd)
{
PostMessage(hwnd, WM_CLOSE, NULL, NULL);
} */
int m, n;
char strClassName[100];
for(m=60; m<70; m++)
for(n=60; n<70; n++)
{
wsprintf(strClassName, "Afx:%d0000:0:10011:11000%d:0", m, n);
HWND hwnd = FindWindowEx(NULL, NULL, strClassName, NULL);
if(NULL != hwnd) PostMessage(hwnd, WM_CLOSE, NULL, NULL);
}
NOTIFYICONDATA icondata;
memset(&icondata, 0, sizeof (icondata));
icondata.cbSize = sizeof (icondata);
icondata.hWnd = Handle;
String s = "设置关机程序"; /* 定义提示文本 */
strncpy(icondata.szTip, s.c_str(), sizeof (icondata.szTip));
icondata.uFlags = NIF_TIP;
Shell_NotifyIcon(NIM_MODIFY, &icondata);
#ifndef _DEBUG
if( (FormatDateTime("h", Now())=="17") && (FormatDateTime("n", Now())=="0") &&(! blOpenJobFile))
#endif
OpenJobFile();
#ifndef _DEBUG
WORD hour, min, sec, msec;
DecodeTime(Now(), hour, min, sec, msec);
if((1 == Now().DayOfWeek()) || (7 == Now().DayOfWeek()) || (hour >= 18))
#endif
OpenBT();
#ifndef _DEBUG
if( (FormatDateTime("h", Now())=="11") && (FormatDateTime("n", Now())=="58") &&(! blOpenLunchWarn))
#endif
OpenLunchOffice(true); /* 我们 12 点吃饭 */
#ifndef _DEBUG
if( (FormatDateTime("h", Now())=="17") && (FormatDateTime("n", Now())=="25") &&(! blOpenOfficeWarn))
#endif
OpenLunchOffice(false); /* 我们 17 点 30 分下班 */
if( ! CheckBox1->Checked) return;
if(Edit1->Text!=FormatDateTime("h", Now())) return;
if(Edit2->Text!=FormatDateTime("n", Now())) return;
m_UStatus = EWX_SHUTDOWN| EWX_FORCE| EWX_POWEROFF;
Close();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnReStartClick(TObject *Sender)
{
m_UStatus = EWX_REBOOT;
Close();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnReLoginClick(TObject *Sender)
{
m_UStatus = EWX_LOGOFF;
Close();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnCloseClick(TObject *Sender)
{
m_UStatus = EWX_SHUTDOWN| EWX_FORCE| EWX_POWEROFF;
Close();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::AddTrayIcon() /* 增加图标 */
{
NOTIFYICONDATA icondata;
memset(&icondata, 0, sizeof(icondata));
/* 将结构icondata的各域初始化为0 */
icondata.cbSize = sizeof(icondata);
icondata.hWnd = this->Handle;
strncpy(icondata.szTip, "未知状态!", sizeof(icondata.szTip));
icondata.hIcon = Application->Icon->Handle;
icondata.uCallbackMessage = iconmessage;
icondata.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
Shell_NotifyIcon(NIM_ADD, &icondata);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::RemoveTrayIcon() /* 删除图标 */
{
NOTIFYICONDATA icondata;
memset(&icondata, 0, sizeof(icondata));
icondata.cbSize = sizeof(icondata);
icondata.hWnd = Handle;
Shell_NotifyIcon(NIM_DELETE, &icondata);
}
/* 载TForm1的WndProc函数,加入对自定义消息的处理代码 */
//------------------------------------------------------------------------------
void __fastcall TfrmMain::WndProc(Messages::TMessage& Message)
{
if((Message.Msg==WM_SYSCOMMAND) && (Message.WParam==SC_MINIMIZE))
{
this->Visible = false;
AddTrayIcon();
ShowWindow(Application->Handle, SW_HIDE);
Application->ShowMainForm = false;
return;
}
if(Message.Msg == iconmessage)
{
if(Message.LParam == WM_LBUTTONDBLCLK) /* 如果双击图标,则显示应用程序 */
{
RemoveTrayIcon();
this->Visible = true;
}
return;
}
TForm::WndProc(Message);/* 对于其他的消息,调用基础类的WndProc函数让Windows进行缺省处理。*/
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
CreateThread(NULL, 0, PlayWarnSoundThread, NULL, 0, NULL);
asFilePath = ".";
extern AnsiString asCurrentDir;
TIniFile *hIniFile = new TIniFile(asCurrentDir+ "//JobRec.ini");
if(NULL != hIniFile)
{
asFilePath = hIniFile->ReadString("SystemSet", "FilePath", "E://文档//工作记录");
asFileName = hIniFile->ReadString("SystemSet", "FileName", "2006.doc");
}
delete hIniFile;
if((iconmessage=RegisterWindowMessage("IconNotify")) != NULL )
AddTrayIcon(); /* 调用在系统栏建立图标的函数 */
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::OpenBT(void)
{
if(blOpenBT) return;
blOpenBT = true;
AnsiString asBTFileName = "E://TSoft//BitComet//BitComet.exe";
extern AnsiString asCurrentDir;
TIniFile *hIniFile = new TIniFile(asCurrentDir+ "//JobRec.ini");
if(NULL != hIniFile)
{
asBTFileName = hIniFile->ReadString("BitComet", "BTFileName", "E://TSoft//BitComet//BitComet.exe");
delete hIniFile;
}
if(FileExists(asBTFileName))
ShellExecute(NULL, "open", asBTFileName.c_str(), NULL, NULL, SW_SHOWMAXIMIZED);
else ShowMessage((AnsiString)"不能找到"+ asBTFileName+ " 文件!");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::OpenJobFile(void)
{
if(blOpenJobFile) return;
blOpenJobFile = true;
/* 周六、日不打开文件 没有处理 5.1 10.1 等情况,感兴趣的话可自己处理 */
if((1==Now().DayOfWeek()) || (7==Now().DayOfWeek())) return;
/* ShowMessage(IntToStr(Now().DayOfWeek())); */
AnsiString LastName, CurName;
if(2==Now().DayOfWeek()) /* 今天为星期一 */
LastName = FormatDateTime("yyyymmdd", (Now()-3))+ ".doc";
else LastName = FormatDateTime("yyyymmdd", (Now()-1))+ ".doc";
CurName = FormatDateTime("yyyymmdd", Now())+ ".doc";
if( ! FileExists(asFilePath+ "//"+ CurName))
{
if(FileExists(asFilePath+ "//"+ LastName))
CopyFile((asFilePath+ "//"+ LastName).c_str(), (asFilePath+ "//"+ CurName).c_str(), true);
else if(FileExists(asFilePath+ "//"+ asFileName))
CopyFile((asFilePath+ "//"+ asFileName).c_str(), (asFilePath+ "//"+ CurName).c_str(), true);
}
if(FileExists(asFilePath+ "//"+ CurName))
{
ShellExecute(NULL, "open", asFilePath.c_str(), NULL, NULL, SW_SHOWMAXIMIZED);
ShellExecute(NULL, "open", (asFilePath+ "//"+ CurName).c_str(), NULL, NULL, SW_SHOWMAXIMIZED);
}
else ShowMessage((AnsiString)"不能找到"+ asFilePath+ "//"+ CurName+ " 文件!");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::OpenLunchOffice(bool Lunch)
{
if(Lunch)
{
if(blOpenLunchWarn) return;
blOpenLunchWarn = true;
}
else
{
if(blOpenOfficeWarn) return;
blOpenOfficeWarn = true;
}
warnningSoundStatus = WM_PLAYHIGH;
SetEvent(RunPlaySound);
}