DELPHI中如何使用API。(100分)

  • 主题发起人 主题发起人 snookerfan
  • 开始时间 开始时间
S

snookerfan

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何在DELPHI中使用API,我在使用LOADICON()函数时,好象图标显示不出来,<br>各位高手救救兄弟吧?
 
看看你的代码?
 
我用的时候很好啊,源代码那出来看看嘛
 
Delphi 调用API 很好用的.这个函数用过,<br>很好的,源代码如果可以,别忘了也给我一份
 
我要答案
 
可以用ExtractIcon试试. 例如:<br>在uses中加入ShellAPI,在form中加入TImage, TButton,<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; h:HIcon;<br>begin<br>&nbsp; &nbsp; &nbsp;h:=ExtractIcon(hinstance,Pchar('c:/windows/progman.exe'),0);<br>&nbsp; &nbsp; &nbsp;if h&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp;drawicon(image1.canvas.handle,0,0,h);<br>end;<br>当然你的情况最好能看看源程序
 
注意定义图标句柄!API函数需要在单元中 uses ShellApi;Delphi调用API函数决无问题,<br>通常不正常的主要原因是调用不规范(符合微软的规范)
 
同意only you<br>把源码贴出来啊!!!!
 
在调用API函数的私有部分的Implementation后的加:uses shellapi;
 
把源码贴出来
 
没有源代码,谁知道你到底要干什么
 
在use 单元中加入api函数所在的单元文件,如uses windows;shellapi<br>然后就和delphi的函数一样使用.
 
g:twt2000 and only you
 
各位,我将源码贴于此,请多多指教,谢谢!<br><br>unit clock;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls,<br>&nbsp; Forms, Dialogs,shellapi, ExtCtrls, ComCtrls, StdCtrls, Menus;<br>const<br>&nbsp; WM_TRAYNOTIFY=WM_USER+1;//定义通知消息<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; StatusBar1: TStatusBar;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; Label3: TLabel;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; Edit3: TEdit;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; CheckBox1: TCheckBox;<br>&nbsp; &nbsp; CheckBox2: TCheckBox;<br>&nbsp; &nbsp; Bevel1: TBevel;<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; N1: TMenuItem;<br>&nbsp; &nbsp; N3: TMenuItem;<br>&nbsp; &nbsp; N4: TMenuItem;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormDestroy(Sender: TObject);<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure N4Click(Sender: TObject);<br>&nbsp; &nbsp; procedure N1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; procedure WndProc(var Msg: TMessage); override;<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; nd:notifyicondata;<br>&nbsp; hs:longword;<br>&nbsp; formhide:boolean;<br>&nbsp; information:string;<br>&nbsp; c_already_run_time:string;<br>&nbsp; c_current_time:string;<br><br>&nbsp; implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp;formhide:=true;<br>&nbsp;hs:=loadicon(from1.handle,'mainicon');<br>&nbsp;//填充NotifyIconData记录型变量nd1<br>&nbsp;nd.cbSize := sizeof(NotifyIconData);<br>&nbsp;nd.Wnd := handle;<br>&nbsp;nd.uID := 0;<br>&nbsp;nd.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;<br>&nbsp;nd.uCallbackMessage := WM_TRAYNOTIFY;<br>&nbsp;nd.hIcon := hs;<br>&nbsp;StrPLCopy(nd.szTip, '成功!', 63);<br>&nbsp;//在任务栏状态区添加图标<br>&nbsp;Shell_NotifyIcon(NIM_ADD, @nd);<br>&nbsp;edit1.text:=timetostr(time);<br>&nbsp;edit2.text:=edit1.text;<br>&nbsp;edit3.text:='0:00:00';<br>&nbsp;timer1.enabled:=true;<br>&nbsp;end;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br>&nbsp;//将图标从任务栏状态区删除<br>&nbsp;Shell_NotifyIcon(NIM_DELETE, @nd);<br>end;<br><br>//处理 通知消息<br>procedure TForm1.WndProc(var Msg: TMessage);<br>var<br>&nbsp; IconID:integer;<br>&nbsp; pt:TPOINT;<br>begin<br>&nbsp; c_already_run_time:=' ';<br>&nbsp; if msg.Msg = WM_TRAYNOTIFY then<br>&nbsp; begin<br>&nbsp; {<br>&nbsp; 在通知消息中,wParam参数为图标的uID,<br>&nbsp; lParam参数为鼠标事件的类型。<br>&nbsp; }<br>&nbsp; &nbsp; iconID := msg.WParam;<br>&nbsp; &nbsp; //获取鼠标的在屏幕上的位置<br>&nbsp; &nbsp; GetCursorPos(pt);<br><br>&nbsp; //通知消息的处理的基本框架结构如下:<br>&nbsp; &nbsp; case msg.lParam of<br>&nbsp; &nbsp; &nbsp; WM_LBUTTONDOWN:<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //鼠标右键被按下<br>&nbsp; &nbsp; &nbsp; &nbsp;beep;<br>&nbsp; &nbsp; &nbsp; &nbsp;formhide:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp;form1.show;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; WM_RBUTTONDOWN:<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //鼠标左键被按下<br>&nbsp; &nbsp; &nbsp; &nbsp;popupmenu1.popup(pt.x,pt.y);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; WM_LBUTTONUP:<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //释放鼠标左键<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; WM_RBUTTONUP:<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //释放鼠标右键<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; WM_MOUSEMOVE:<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //鼠标在图标上移动<br>&nbsp; &nbsp; &nbsp; &nbsp; //生成已运行时间的字符串<br>&nbsp; &nbsp; &nbsp; &nbsp; if length(trim(edit3.text))&lt;&gt;8 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if strtoint(copy(edit3.text,1,1))&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c_already_run_time:=inttostr(strtoint(copy(edit3.text,1,1)))+'小时';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if strtoint(copy(edit3.text,3,2))&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c_already_run_time:=c_already_run_time+inttostr(strtoint(copy(edit3.text,3,2)))+'分';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if strtoint(copy(edit3.text,6,2))&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c_already_run_time:=c_already_run_time+inttostr(strtoint(copy(edit3.text,6,2)))+'秒';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if strtoint(copy(edit3.text,1,2))&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c_already_run_time:=copy(edit3.text,1,2);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if strtoint(copy(edit3.text,4,2))&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c_already_run_time:=c_already_run_time+copy(edit3.text,4,2);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if strtoint(copy(edit3.text,7,2))&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c_already_run_time:=c_already_run_time+copy(edit3.text,7,2);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; //修改提示信息<br>&nbsp; &nbsp; &nbsp; &nbsp; information:='成功啦!您的电脑是 '+edit1.text+' 打开的,已经运行了:'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+c_already_run_time;<br>&nbsp; &nbsp; &nbsp; &nbsp; StrPLCopy(nd.szTip, information, 100);<br>&nbsp; &nbsp; &nbsp; &nbsp; Shell_NotifyIcon(NIM_MODIFY, @nd);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; WM_LBUTTONDBLCLK:<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //鼠标左键双击<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; WM_RBUTTONDBLCLK:<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //鼠标右键双击<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end; //end case<br>&nbsp; end<br>&nbsp; else//调用父类的WndProc方法处理其它消息<br>&nbsp; &nbsp; inherited;<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br>&nbsp;if formhide=true then form1.hide;<br>&nbsp;timer1.interval:=1000;<br>&nbsp;edit2.text:=timetostr(time);<br>&nbsp;edit3.text:=timetostr(strtotime(edit2.text)-strtotime(edit1.text));<br>&nbsp;//整点报时<br>&nbsp;if (length(edit2.text)=8) and (checkbox1.checked=true) and (strtoint(copy(edit2.text,4,2))=0) and (strtoint(copy(edit2.text,7,2))=0) then<br>&nbsp; showmessage('现在时间:'+edit2.text);<br>&nbsp;if (length(edit2.text)=7) and (checkbox1.checked=true) and (strtoint(copy(edit2.text,3,2))=0) and (strtoint(copy(edit2.text,6,2))=0)then<br>&nbsp; showmessage('现在时间:'+edit2.text);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp;form1.Hide;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp;application.Terminate;<br>end;<br><br>procedure TForm1.N4Click(Sender: TObject);<br>begin<br>&nbsp;application.terminate;<br>end;<br><br>procedure TForm1.N1Click(Sender: TObject);<br>begin<br>&nbsp;//messagebox('希望您多多指教。(作者:彭滔)','关于闹钟',mb_OK,1);<br>&nbsp;beep;<br>&nbsp;Application.MessageBox(<br>&nbsp; &nbsp; &nbsp; &nbsp; '希望您多多指教,不胜感激。(snookerfan)',<br>&nbsp; &nbsp; &nbsp; &nbsp; '关于闹钟',<br>&nbsp; &nbsp; &nbsp; &nbsp; mb_ok);<br>end;<br><br>end.
 
用rx控件吧,方便死了
 
可能是FORM1的ICON的问题!<br>用hs:=form1.icon.handle同样不行的!!!!
 
如果用hs:=Application.icon.handle就可以了!!!!<br>但是loadicon(application.handle,'mainicon')仍然不行!
 
啊,对不起!刚才form1的icon没设置!:)<br>如果设置了,用hs:=form1.icon.handle就可以了!
 
NOD,其实调用API关键是各种句柄的传递和控制<br>我以前自定义光标就因为自己定义了一个HINST句柄却没有自己控制它而导致失败。<br>所以我觉得你可能问题在<br>hs:=loadicon(from1.handle,'mainicon');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;~~~~~~~~~~~~<br>可以直接loadicon(hinstance,'mainicon');<br>并且hinstance不用定义,当然自己定义可以,但是需要赋值//既然操作系统自动给了,就偷个懒,呵呵<br><br>&nbsp;<br>
 
有谁能介绍基本关于DELPHI编程方面的好书呢,最好是对能对WIN32 API的使用<br>能多介绍一些就好,谢谢!
 
后退
顶部