各位前辈,学习API我该如何下手?(50分)

  • 主题发起人 主题发起人 ymf
  • 开始时间 开始时间
Y

ymf

Unregistered / Unconfirmed
GUEST, unregistred user!
各位前辈,我一看到API函数就感觉头痛……,学习API我该如何下手?
 
学API 先用sendmessage就可以了,
 
只要会用findwindow() showwindow(),sendmessage(),postmessage()就够了
 
多看看书,做做例子
 
API不需要学,<br>要用的时候,再去查!
 
&nbsp; &nbsp;我推荐一本书&lt;&lt;Windows API 超级宝典&gt;&gt;,有说明,有例子!
 
windows 编程 第五版,有一定的 c 语言基础就可以了。让你从更深的层次了解 windows <br>程序。从 winmain() 入口到 RegisterClass() 消息循环到 message procedure。<br>对鼠标,键盘,gdi,dib 等等编程更能深入。
 
to 御键飞天 <br>什么地方有下载的
 
最好看C++windows程序设计的书,绝非MFC,全API的<br><br>现会findwondow,shellAbout,Winexec逐渐学会消息<br><br>处理(messages.pas),再知道回调函数,多线程就勉强可以了.<br>var i,count: integer;<br>&nbsp; &nbsp; xy: HWND; //某另外程序窗口中listBox的句柄<br>&nbsp; &nbsp;s: Pchar;<br>begin<br>&nbsp; count:=Sendmessage(xy,LB_GETCOUNT,0,0);<br>&nbsp; xy:.......; //此处省略句柄得到的代码1500行(最多15行)<br>&nbsp; GetMem(s,256);<br>&nbsp; for i:=0 to Count-1 do<br>&nbsp; begin<br>&nbsp; &nbsp; Sendmessage(xy,LB,_GetTEXT,i,longint(s));<br>&nbsp; &nbsp; ListBox1.Items.Add(Strpas(s)); &nbsp; <br>&nbsp; end;<br>freemem(s);<br>end; &nbsp;//程序能获取其他程序中listbox中内容为我所用,<br>&nbsp; &nbsp; &nbsp; sendmessage(xy,LB_RESETCONTENT,0,0); 清空xy的所有内容<br>&nbsp; &nbsp; &nbsp; 你就知道怎么用消息了!!!恭喜你!
 
大家继续多举几个例子呀
 
我觉得首先要理解什么叫“句柄”,什么叫“消息”。<br>然后弄清楚在Delphi与windows api中的数据类型的对应关系:<br>据我所知,好像是这样的:<br>UINT=integer<br>MSG=integer<br>wParam=integer<br>lParam=integer<br>HWND=Thandle=integer<br>LRESULT=integer<br>BOOL=Boolean<br>LPCTSTR=pchar<br>INT=integer<br>CALLBACK=stdcall<br>最后,弄清楚几个常用API的用法: &nbsp;<br>我觉得下面这几个比较常用:<br>messagebox<br>sendmessage<br>findwindow<br>windowfrompoint<br>setwindowpos<br>getwindowtext<br>setwindowtext<br>getforegroundwindow<br>getdc<br>好了,我知道的就这些了,毫无保留地写出来了。<br>如果有错,请大家指点。<br>
 
API不用学,<br>要用的时候去查!
 
我推荐《Windows程序设计(第五版)》,Charles Petzold写的,有时间就拿出来看看,<br>需要的时候就当资料查查,慢慢的就会了。这本书非常棒!!!
 
呵呵<br>那就先学vc吧
 
我说是不刻意去学它,用时查帮助
 
看看这个例子吧,对学API很有帮助的:<br>program myapiapplication;<br><br>uses<br>&nbsp; Windows,<br>&nbsp; Messages;<br><br>var<br>&nbsp; &nbsp; wc: TWndClass;<br>&nbsp; &nbsp; hWnd: Integer;<br>&nbsp; &nbsp; lpMSG: TMsg;<br><br>function WndProc( hWnd,Msg,wParam,lParam:integer) :integer ;stdcall; <br>var<br>&nbsp; myhdc : hdc;<br>&nbsp; ps : TPaintstruct;<br>&nbsp; myrect : TRect;<br>&nbsp; po : TPoint;<br>&nbsp; ClientHdc : hdc;<br>begin<br>&nbsp; &nbsp; Result := 0;<br>&nbsp; &nbsp; case Msg of<br>&nbsp; &nbsp; &nbsp; WM_LBUTTONDOWN: begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if wParam=vk_lbutton then messagebox(hWnd,'请不要在窗口上乱点鼠标左键!','提示',o+mb_iconinformation);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if wParam=vk_rbutton then messagebox(hWnd,'请不要在窗口上乱点鼠标右键!','提示',o+mb_iconinformation);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; WM_MOUSEMOVE: begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCursorPos(po);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ClientHdc :=getDc(hWnd);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ellipse(hWnd,po.x-100,po.y-100,po.x,po.y);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; releaseDc(hWnd,hdc); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; WM_DESTROY:begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PostQuitMessage(0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; WM_PAINT : begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myhdc :=BeginPaint(hwnd,ps);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetClientRect(hwnd,myrect);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DrawText(myhdc,'你好,欢迎学习API函数!',-1,myrect,DT_SINGLELINE OR DT_CENTER OR DT_VCENTER);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;endPaint(hwnd,ps);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Result := DefWindowProc(hWnd, Msg, wParam, lParam);<br>end;<br><br>begin &nbsp;<br>&nbsp; &nbsp; wc.style := CS_VREDRAW or CS_HREDRAW;<br>&nbsp; &nbsp; wc.lpfnWndProc := @wndProc;<br>&nbsp; &nbsp; wc.cbClsExtra := 0;<br>&nbsp; &nbsp; wc.cbWndExtra := 0;<br>&nbsp; &nbsp; wc.hInstance := HInstance;<br>&nbsp; &nbsp; wc.hIcon := LoadIcon(0, IDI_APPLICATION);<br>&nbsp; &nbsp; wc.hCursor := LoadCursor(0, IDC_ARROW);<br>&nbsp; &nbsp; wc.hbrBackground := (COLOR_BTNFACE+1);<br>&nbsp; &nbsp; wc.lpszMenuName := nil;<br>&nbsp; &nbsp; wc.lpszClassName := 'My App';<br>&nbsp; &nbsp; if RegisterClass(wc)=0 then Exit;<br>&nbsp; &nbsp; hWnd := CreateWindow( wc.lpszClassName,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'用Pascal语言加api函数编制的Windows程序',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WS_OVERLAPPEDWINDOW,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;100,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;50,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;400,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;300,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HInstance,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil);<br>&nbsp; &nbsp; ShowWindow(hWnd, SW_SHOWNORMAL);<br>&nbsp; &nbsp; UpdateWindow(hWnd);<br>&nbsp; &nbsp; while GetMessage(lpMSG, 0, 0, 0) do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TranslateMessage(lpMSG);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DispatchMessage(lpMSG);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; Halt(lpMSG.wParam);<br><br>end.<br>
 
感谢小唐兄、tan_jian及各位的热情帮助,希望大家不要嫌分少
 
后退
顶部