大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> 程序开发 -> 怎样建立简单的任务栏应用程序

怎样建立简单的任务栏应用程序

时间: 2021-07-31 作者:daque

windows 95 和 windows nt 4.0包括一个令人激动的个性:工作栏。这个常常坐落地区工作条右面包车型的士地区能包括小的图标,那些图标能引出大的运用步调大概菜单。本篇作品重要计划怎样运用delphi创造如许的运用步调。 在发端之前,请看底下的须要的接口上面的实质: 从本领上面来说,一个工作栏运用步调特殊象普遍的运用步调,它有一个动静轮回,相映windows的动静来实行相映的功效。 procedure runtrayapplication;var msg : tmsg;begincreatewindow;addtrayicon;while getmessage(msg,0,0,0) do begintranslatemessage(msg);dispatchmessage(msg);end;deletetrayicon;end;你能看到:一切须要做的处事是创造一个窗口,备案一个图标到工作栏,树立它的动静轮回,结果封闭它。固然,必需再有减少其余代码实行相映的功效,然而,它是真的不须要担忧。 让咱们从窗口的创造发端。本质上,这个窗口是否能在工作栏上能见到的窗口。相映的,这个窗口不过处置动静轮回、其它父类的处事。工作窗口(windows 95 & nt)句柄创造动静(比方鼠标单击等)和将动静发到咱们的窗口。 procedure createwindow;varwc : twndclass;w : hwnd;beginwith wc do beginstyle := 0;lpfnwndproc := @wndproc;cbclsextra := 0;cbwndextra := 0;hicon := 0;hcursor := 0;hbrbackground := 0;lpszmenuname := nil;lpszclassname := 'mytrayiconclass';hinstance := system.hinstance;end;registerclass(wc);w := windows.createwindow('mytrayiconclass','myveryowntrayiconwindow',ws_overlappedwindow,0,0,0,0,0,0,hinstance,nil);showwindow(w,sw_hide);updatewindow(w);mainwindow := w;end;这个窗口运用普遍的窗口因变量创造。提防这个窗口的典型是“ws_overlappedwindow”,然而这个尺寸是0,而且它是湮没的,一切,它将不会表露出来。 下一步是加(备案)咱们的图标。这将须要运用shell_notifyicon这个api因变量,这个因变量本质上不妨实行三个功效,这边只须要它的减少的个性。 procedure addtrayicon;var icondata : tnotifyicondata;beginwith icondata do begincbsize := sizeof(icondata);wnd := mainwindow;uid := 0;uflags := nif_icon or nif_message or nif_tip;ucallbackmessage := wm_mycallback;hicon := loadicon(hinstance,'myicon');strcopy(sztip,pchar(trayicontip));end;shell_notifyicon(nim_add,@icondata);end;这个最要害的工作是tnotifyicondata的数据构造,它是一个树立window句柄的数据构造,是一个记载参数,对咱们来说,咱们须要树立这个图目标窗口句柄(这将设置哪个窗口处置动静轮回),回调动静号,图标,东西提醒等。一旦这个数据树立了,咱们就不妨减少一个图标到工作栏上了。为了实行这个处事,运用nim_add步调。 现行反革命咱们仍旧加了咱们的图标到工作栏,底下须要确定怎样处置动静。 constwm_mycallback = wm_user+1000;cm_exit = 100; { we worry about... }cm_about = 101; { ...these later }这个本质的窗口处置进程也是十分普遍。几个窗口动静(如wm_nccreate)必需处置。但是,对咱们来说,更要害的工作是处置wm_mycallback和wm_command动静: function wndproc(window : hwnd; msg,wparam,lparam : integer): integer; stdcall;beginresult := 0;case msg ofwm_nccreate : result := 1;wm_destroy : postquitmessage(0);wm_command : begin { a command was chosen from the popup menu }if (wparam = cm_exit) thenpostmessage(window,wm_destroy,0,0)else if (wparam = cm_about) thenmessagebox(0,'shell test copyright ?'+'jani j鋜vinen 1996.','about shell test',mb_ok)else opendesktopicon(wparam-cm_about);end;wm_mycallback : begin { our icon was clicked }if (lparam = wm_lbuttondown) thenshowiconpopupmenuelse if (lparam = wm_rbuttondown) thenshowaboutpopupmenu;end;else result := defwindowproc(window,msg,wparam,lparam);end;end;就象你看到的一律,当用户单击图标时,windows提醒咱们。提防咱们不运用常常运用的wm_lbuttondown 动静,而运用wm_mycallback message,精细的动静消息保存在lparam参数中。 当用户单击鼠标右键,咱们创造一个菜单在桌面上。 typeticondata = array[1..100] of string;varicondata : ticondata;procedure showiconpopupmenu;varshellfolder : ishellfolder;enumidlist : ienumidlist;result : hresult;dummy : ulong;itemidlist : titemidlist;pntr : pitemidlist;strret : tstrret;popupmenu : hmenu;itemid : integer;pos : tpoint;procedure addtomenu(item : string);var s : string;beginicondata[itemid-cm_about] := item;s := extractfilename(item);if (system.pos('.',s) <> 0) then setlength(s,system.pos('.',s)-1);appendmenu(popupmenu,mf_enabled or mf_string,itemid,pchar(s));inc(itemid);end;beginpopupmenu := createpopupmenu;itemid := cm_about+1;shgetdesktopfolder(shellfolder);shellfolder.enumobjects(mainwindow,shcontf_nonfolders,enumidlist);pntr := @itemidlist;result := enumidlist.next(1,pntr,dummy);while (result = noerror) do beginshellfolder.getdisplaynameof(pntr,shgdn_forparsing,@strret);with strret do addtomenu(string(cstr));result := enumidlist.next(1,pntr,dummy);end;enumidlist.release;shellfolder.release;getcursorpos(pos);appendmenu(popupmenu,mf_separator,0,'');appendmenu(popupmenu,mf_enabled or mf_string,cm_exit,'e&xit');setforegroundwindow(mainwindow);trackpopupmenu(popupmenu,tpm_leftalign or tpm_leftbutton,pos.x,pos.y,0,mainwindow,nil);destroymenu(popupmenu);end;上头的步调看上去有点搀杂,你不妨将它分红两个局部来看:创造和表露菜单。 陈列创造菜单是用windows的外壳接口实行的。开始,咱们运用shgetdesktopforlder因变量获得运用桌面包车型的士ishellfolder接口。运用这个接口,咱们能获得另一个接口的范例:ienumidlist。这个接口常常实行本质的陈列处事。咱们大略的反复挪用这个因变量直到缺点值归来(比方:一切的菜单被陈列)。当咱们获得一个菜单,咱们运用addtomenu因变量加它。 当一切的菜单被陈列和创造后,此刻咱们须要运转这个菜单。咱们将找到的菜单生存到一个全部的list变量中,每一个菜单都具有它的菜单号。这保证咱们能获得它的索引。 opendesktopicon(wparam-cm_about) 固然,wparam中积聚了用户单击鼠目标菜单的菜单号(id)。 底下咱们将处置运转用户采用的菜单。 procedure opendesktopicon(number : integer);vars : string;i : integer;begins := icondata[number];i := shellexecute(0,nil,pchar(s),nil,nil,sw_shownormal);if (i < 32) then begins := 'could not open selected item "'+s+'". '+'result was: '+inttostr(i)+'.';messagebox(0,pchar(s),'shell test',mb_ok);end;end;上头,win 32 api因变量shellexecute做了一切的处事。 此刻你该当能用delphi创造大略的工作栏的步调了。 本质上,有少许免费的元件不妨供您径直运用,然而,由于运用vcl,文献的巨细将比拟大,即使运用上头的本领,文献的巨细将只有20k。固然,此刻文献的巨细仍旧不是咱们该格外关心的题目了。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.