怎么样在一个小程序中加上 TTimer 这个功能呢?我现在就只有35分了,全SHOW了!!!(0分)

Z

zlwnet

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么样在一个小程序中加上 TTimer 这个功能呢?我现在就只有35分了,全SHOW了!!!
这个小程序是指一个没有窗口框架下的程序,能实现定时工作!
 
试试:
uses ExtCtrls;

timer1 := TTimer.Create(nil);
timer1.OnTimer := ....
 
动态创建,并且要自定义事件和相关属性。
 
在timer的 ontimer事件里写你的代码
 
timer1.OnTimer := FARPROG
这个怎么定义 FARPROG ?
 
private
procedure TimerProc(Sender: TObject);
public
end;

procedure TForm1.TimerProc(Sender: TObject);
begin
////你的代码
end;
timer1.OnTimer=TimerProc;
 
lb_icesea79, 请把你的timer1.OnTimer=TimerProc;在以下程序中定义并使用!
我试过你上面给出的方法,在这里我做不,还请赐教!
谢谢!

program prog1;
{prog1.dpr}
uses Windows, Messages, ShellAPI, sysutils;
{$R *.RES}
{可以看出本程序比普通的 Delphi 程序用到的 Unit 少的多。 下面声明了全局常量和变量,暂时可以不管他们。}
const
AppName = 'DeskTop Hide';
var
x: integer;
tid: TNotifyIconData;
WndClass: array[0..50] of char;

procedure HandleCommand (Wnd: hWnd; Cmd: Word);
begin
case Cmd of
Ord ('A'): MessageBox (0, 'Freeware brian.slack@strath.ac.uk 1997',AppName, mb_ok);
Ord ('E'): PostMessage (Wnd, wm_Close, 0, 0);
end;
end;


function DummyWindowProc (Wnd: hWnd; Msg, wParam: Word; lParam: LongInt):LongInt; stdcall;
{注意这里有一个 stdcall;定义了回调函数}
var
TrayHandle: Thandle;
dc: hDC;
pm: Hmenu;
pt: Tpoint;
begin
DummyWindowProc := 0;
{下面两句是找到 Win95 任务栏的句柄}
StrPCopy(@WndClass[0], 'Progman');
TrayHandle := FindWindow(@WndClass[0], nil);
{下面开始处理消息}
case Msg of
{收到窗口创建消息 - 在任务栏上显示一个图标}
wm_Create: // Program initialisation - just set up a tray icon
begin
tid.cbSize := sizeof (tid);
tid.Wnd := Wnd;
tid.uID := 1;
tid.uFlags := nif_Message or nif_Icon or nif_Tip;
tid.uCallBackMessage := wm_User;
tid.hIcon := LoadIcon (hInstance, 'mainICON');
lstrcpy (tid.szTip,'Desktop is on');
Shell_NotifyIcon (nim_Add, @tid);
end;
wm_Destroy: {收到关闭窗口消息时的处理}
begin
Shell_NotifyIcon (nim_Delete, @tid);
PostQuitMessage (0);
ShowWindow(TrayHandle, SW_RESTORE);
end;

{收到菜单消息时调用 HandleCommand 过程,并退出函数}
wm_Command: // Command notification
begin
HandleCommand (Wnd, LoWord (wParam));
Exit;
end;

{收到其他用户消息时的处理}
wm_User: // Had a tray notification - see what to do
{如果单击了鼠标左键, 则打开或关闭桌面}
if (lParam = wm_LButtonDown) then
begin
if x = 0 then
begin
ShowWindow(TrayHandle, SW_HIDE);
tid.hIcon := LoadIcon (hInstance, 'OFFICON');
lstrcpy (tid.szTip,'Desktop is off');
Shell_NotifyIcon (NIM_MODIFY, @tid);
x:=1
end else
begin
ShowWindow(TrayHandle, SW_RESTORE);
tid.hIcon := LoadIcon (hInstance, 'ONICON');
lstrcpy (tid.szTip,'Desktop is on');
Shell_NotifyIcon (NIM_MODIFY, @tid);
x:= 0;
end; {end of if}
end else
{如果是鼠标右键,则动态生成一个弹出式菜单}
if (lParam = wm_RButtonDown) then
begin
GetCursorPos (pt);
pm := CreatePopupMenu;
AppendMenu (pm, 0, Ord ('A'), 'About DeskTop Hide...');
AppendMenu (pm, mf_Separator, 0, Nil);
AppendMenu (pm, 0, Ord ('E'), 'Exit DeskTop Hide');
SetForegroundWindow (Wnd);
dc := GetDC (0);
if TrackPopupMenu (pm, tpm_BottomAlign or tpm_RightAlign,
pt.x,pt.y, 0, Wnd, Nil)
then SetForegroundWindow (Wnd);
DestroyMenu (pm)
end; {end of if}
end; {end of case}
{在处理过消息之后,还要调用默认函数,
以完成标准的Windows程序应该执行的任务,
所以这一句非常重要}
DummyWindowProc := DefWindowProc (Wnd, Msg, wParam, lParam);
end;
{这个就是处理菜单消息的过程}
{现在看来,程序的主框架很明了,但是它还不能完成任何任务。
过程 Panic将显示一个对话框后退出程序,
它在 Winmain 过程的开始部分被调用,
其实 Panic的功能很简单,之所以要写成一个函数的原因
恐怕一方面是结构化编程的需要,
另一方面借此避开了 String 和 Pchar 的转换。}
procedure Panic (szMessage: Pchar);
begin
if szMessage <> Nil then
MessageBox (0, szMessage, AppName, mb_ok);
Halt (0);
end;


{现在进入程序的主要部分,首先是定义了一批过程,
为了能让读者更好地理解,我们先把这些过程跳过去,
先说主程序。主程序位于程序的最后,
这样做的好处是可以直接使用程序中定义的过程。
主程序十分简单:}
procedure WinMain;
var
Wnd: hWnd; {声明窗口句柄(Handle)变量}
Msg: TMsg; {声明消息变量}
cls: TWndClass; {窗口类变量}
begin
{ Previous instance running ? If so, exit }
{ 检查是否程序已经运行,如果已经运行则调用Panic过程退出 }
if FindWindow (AppName, Nil) <> 0 then
Panic (AppName + ' is already running.');
{ Register the window class }
{ 这里的注册窗口类程序是例行公事,照抄即可}
FillChar (cls, sizeof (cls), 0); {用这一句将窗口类变量cls清零}
cls.lpfnWndProc := @DummyWindowProc; {取回调函数DummyWindowProc的地址}
cls.hInstance := hInstance; {实例句柄}
cls.lpszClassName := AppName; {窗口类名}
RegisterClass (cls); {注册窗口类cls}
{ 现在可以创建程序的主窗口了-在本程序中是个虚拟窗口}
{ Now create the dummy window }
Wnd := CreateWindow (AppName, AppName, ws_OverlappedWindow,0, 0, 100, 100,
0, 0, hInstance, Nil);
x:= 0; {变量X其实是个开关变量,记录现在是否已经隐藏了桌面}
{ 如果窗口创建成功,则显示窗口,并进入消息循环 }
if Wnd <> 0 then
begin
ShowWindow (Wnd, sw_Hide);{本例中窗口是隐藏的}
{ 下面进入消息循环,该循环将不断运行直到 GetMessage返回0 }
while GetMessage (Msg, 0, 0, 0) do
begin
TranslateMessage (Msg);
DispatchMessage (Msg);
end;
end;
end;
{主程序}
begin
WinMain;
end.



 
顶部