CsDn,盒子…都无法解决的问题.满分送上.关于Api窗体问题. ( 积分: 210 )

  • 主题发起人 主题发起人 DelphiVip
  • 开始时间 开始时间
D

DelphiVip

Unregistered / Unconfirmed
GUEST, unregistred user!
我用Api创建一窗体,并创建一个Edit.<br><br>但怎么写出EDIT的事件过程(Event)?<br><br>如:OnkeyDown这个事件?.<br><br>非Api的窗体事件过程是:<br><br>procedure&nbsp;&nbsp;&nbsp;TForm1.Edit1KeyDown(Sender:&nbsp;&nbsp;&nbsp;TObject;&nbsp;&nbsp;&nbsp;var&nbsp;&nbsp;&nbsp;Key:&nbsp;&nbsp;&nbsp;Word;&nbsp;&nbsp;&nbsp;Shift:&nbsp;&nbsp;&nbsp;TShiftState);&nbsp;<br><br>那Api该怎么写才能返回:键盘输入的值?.我尝试过创建消息循环.Wm_Command,但是此函数会返回鼠标按下,键盘按下的实件.Wm_Keydown并不能返回键盘按下的事件.<br><br>当然我不是想返回是否按下了键盘这个事件,而是想返回键盘按下时,并取得它的按下的值.<br><br>举个例子,按下键盘后,会自动将键盘的值sendmenssage到记事本中.?&nbsp;该怎么操作?.<br><br>非Api的代码:<br><br>procedure&nbsp;TForm1.Edit1KeyPress(Sender:&nbsp;TObject;&nbsp;var&nbsp;Key:&nbsp;Char);<br>var<br>&nbsp;&nbsp;vHandle:&nbsp;Integer;<br>begin<br>&nbsp;&nbsp;vHandle&nbsp;:=&nbsp;FindWindow('Notepad',&nbsp;nil);<br>&nbsp;&nbsp;if&nbsp;vHandle&nbsp;=&nbsp;0&nbsp;then&nbsp;Exit;<br>&nbsp;&nbsp;vHandle&nbsp;:=&nbsp;FindWindowEx(vHandle,&nbsp;0,&nbsp;'Edit',&nbsp;nil);<br>&nbsp;&nbsp;if&nbsp;vHandle&nbsp;=&nbsp;0&nbsp;then&nbsp;Exit;<br>&nbsp;&nbsp;SendMessage(vHandle,&nbsp;WM_CHAR,&nbsp;Ord(Key),&nbsp;0);<br>end;<br><br>但是我的Edit是Api创建的.就难操作了.<br><br>附API创建窗体代码:<br><br>procedure&nbsp;MainCreate;<br>begin<br>&nbsp;&nbsp;&nbsp;attstart:=true;<br>&nbsp;&nbsp;&nbsp;hMutex:=createmutex(nil,true,'Bome2001');<br>&nbsp;&nbsp;&nbsp;releasemutex(hMutex);<br>end;<br><br>function&nbsp;WindowProc(hWnd,Msg,wParam,lParam:integer):Longint;&nbsp;stdcall;<br>begin<br>&nbsp;&nbsp;Result:=DefWindowProc(hWnd,Msg,wParam,lParam);<br>&nbsp;&nbsp;case&nbsp;Msg&nbsp;of&nbsp;WM_COMMAND:<br>&nbsp;&nbsp;&nbsp;if&nbsp;lParam=&nbsp;hEditEmail&nbsp;then&nbsp;&nbsp;showmessage('ok');&nbsp;//&nbsp;这个是按下鼠标或者键盘都会弹出,而且无法拦截它输入的信息。<br>&nbsp;&nbsp;end;<br>end;<br><br>function&nbsp;&nbsp;CreateEdit(name:pchar;x1,y1,x2,y2:integer):hwnd;<br>begin<br>Result:=CreateWindowEx(WS_EX_CLIENTEDGE,'Edit',name,WS_VISIBLE&nbsp;&nbsp;or&nbsp;&nbsp;WS_CHILD&nbsp;&nbsp;or&nbsp;&nbsp;ES_LEFT&nbsp;&nbsp;or&nbsp;&nbsp;ES_AUTOHSCROLL,x1,y1,x2,y2,Handle,0,hInst,nil);<br>end;<br>function&nbsp;&nbsp;CreateMain(name:pchar;x1,y1,x2,y2:integer):hwnd;<br>begin<br>&nbsp;&nbsp;hInst:=GetModuleHandle(nil);<br>&nbsp;&nbsp;with&nbsp;&nbsp;wClass&nbsp;&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Style:=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CS_CLASSDC&nbsp;or&nbsp;CS_PARENTDC;<br>&nbsp;&nbsp;&nbsp;&nbsp;hIcon:=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LoadIcon(hInst,'MAINICON');<br>&nbsp;&nbsp;&nbsp;&nbsp;lpfnWndProc:=&nbsp;&nbsp;&nbsp;&nbsp;@WindowProc;<br>&nbsp;&nbsp;&nbsp;&nbsp;hInstance:=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hInst;<br>&nbsp;&nbsp;&nbsp;&nbsp;hbrBackground:=&nbsp;&nbsp;COLOR_BTNFACE+1;<br>&nbsp;&nbsp;&nbsp;&nbsp;lpszClassName:=&nbsp;&nbsp;'MainClass';<br>&nbsp;&nbsp;&nbsp;&nbsp;hCursor:=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LoadCursor(0,IDC_ARROW);<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;RegisterClass(wClass);<br>&nbsp;&nbsp;Result:=CreateWindow(wClass.lpszClassName,name,WS_OVERLAPPEDWINDOW&nbsp;&nbsp;or&nbsp;&nbsp;WS_VISIBLE,x1,y1,x2,y2,0,0,hInst,nil);<br>end;<br><br>begin<br>&nbsp;&nbsp;handle:=CreateMain(exename,mrect.left,mrect.top,806,625);<br>&nbsp;&nbsp;hEditEmail:=CreateEdit('',358,258,94,14);<br>&nbsp;&nbsp;while(GetMessage(Msg,Handle,0,0))do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;TranslateMessage(Msg);<br>&nbsp;&nbsp;&nbsp;&nbsp;DispatchMessage(Msg);<br>&nbsp;&nbsp;end;<br>end.
 
我没怎么玩过这类开发,不过,以在下的愚见,要对Edit的消息进行处理,就应该像在<br>CreateMain&nbsp;中一样,给Edit自定义消息处理过程,在其中响应你希望响应的消息。<br><br>&nbsp;&nbsp;这个帖子或许对楼主会有所帮助:<br>&nbsp;&nbsp;&nbsp;&nbsp;http://www.delphibbs.com/delphibbs/dispq.asp?lid=2239240
 
帮你顶<br>---------------------------------------------------------------------------------------http://www.waibaoinfo.com&nbsp;外包信息网&nbsp;-&nbsp;IT界专业的外包项目信息发布和承接平台
 
我已经定义了一个消息循环啊.&gt;&nbsp;Wm_Command&nbsp;<br>但是定义什么函数可以将键盘按值取出来?.WM_keydown&nbsp;这个为什么不行?&nbsp;而且无法取出它的值..除了键盘钩子
 
function&nbsp;WindowProc(hWnd,Msg,wParam,lParam:integer):Longint;&nbsp;stdcall;<br>begin<br>&nbsp;&nbsp;Result:=DefWindowProc(hWnd,Msg,wParam,lParam);<br>&nbsp;&nbsp;case&nbsp;Msg&nbsp;of&nbsp;WM_COMMAND:<br>&nbsp;&nbsp;&nbsp;if&nbsp;lParam=&nbsp;hEditEmail&nbsp;then&nbsp;&nbsp;showmessage('ok');&nbsp;//&nbsp;这个是按下鼠标或者键盘都会弹出,而且无法拦截它输入的信息。<br>&nbsp;&nbsp;end;<br>end;<br><br>问题出在上面这个函数里,因为hWnd是主窗体的句柄,不是edit的句柄。<br>&nbsp;case&nbsp;Msg&nbsp;of&nbsp;wm_keydown:<br>&nbsp;&nbsp;&nbsp;if&nbsp;lParam=&nbsp;hEditEmail&nbsp;then&nbsp;&nbsp;showmessage('formKeyDown');//form1.keyPrivew:=&nbsp;&nbsp;&nbsp;true<br>&nbsp;&nbsp;&nbsp;if&nbsp;lParam&lt;&gt;hEditEmail&nbsp;then&nbsp;showmessage('editKeyDown');&nbsp;<br>&nbsp;(处于容器的from,其keyPreview属性必须为true,才能出发其内部控件的onKeyDown事件)
 
谢谢,但是还有一点是怎么能取得敲下的值?.谢谢啊!大大啊!
 
那年黄梅花开,确实是高手!<br><br>function&nbsp;WindowProc(hWnd,Msg,wParam,lParam:integer):Longint;&nbsp;stdcall;<br>var<br>&nbsp;&nbsp;i&nbsp;:&nbsp;Integer&nbsp;;<br>&nbsp;&nbsp;dc&nbsp;:HDC;<br>&nbsp;&nbsp;str&nbsp;:&nbsp;String;<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DC&nbsp;:=&nbsp;GetDC(handle)&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str&nbsp;:=&nbsp;Format('hwnd:%d,msg:%d,wpa%d&nbsp;&nbsp;,&nbsp;&nbsp;lpa%d',[hWnd,Msg,wParam,lParam]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TextOut(dc,0,300,pansichar(str),strlen(pansichar(str)));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result:=DefWindowProc(hWnd,Msg,wParam,lParam);<br>end;<br><br>WM_KEYDOWN<br>The&nbsp;WM_KEYDOWN&nbsp;message&nbsp;is&nbsp;posted&nbsp;to&nbsp;the&nbsp;window&nbsp;with&nbsp;the&nbsp;keyboard&nbsp;focus&nbsp;when&nbsp;a&nbsp;nonsystem&nbsp;key&nbsp;is&nbsp;pressed.&nbsp;A&nbsp;nonsystem&nbsp;key&nbsp;is&nbsp;a&nbsp;key&nbsp;that&nbsp;is&nbsp;pressed&nbsp;when&nbsp;the&nbsp;ALT&nbsp;key&nbsp;is&nbsp;not&nbsp;pressed.&nbsp;<br><br>A&nbsp;window&nbsp;receives&nbsp;this&nbsp;message&nbsp;through&nbsp;its&nbsp;WindowProc&nbsp;function.&nbsp;<br><br>LRESULT&nbsp;CALLBACK&nbsp;WindowProc(<br>&nbsp;&nbsp;HWND&nbsp;hwnd,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;handle&nbsp;to&nbsp;window<br>&nbsp;&nbsp;UINT&nbsp;uMsg,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;WM_KEYDOWN<br>&nbsp;&nbsp;WPARAM&nbsp;wParam,&nbsp;&nbsp;&nbsp;//&nbsp;virtual-key&nbsp;code<br>&nbsp;&nbsp;LPARAM&nbsp;lParam&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;key&nbsp;data<br>);<br>Parameters<br>wParam&nbsp;<br>Specifies&nbsp;the&nbsp;virtual-key&nbsp;code&nbsp;of&nbsp;the&nbsp;nonsystem&nbsp;key.&nbsp;<br>lParam&nbsp;<br>Specifies&nbsp;the&nbsp;repeat&nbsp;count,&nbsp;scan&nbsp;code,&nbsp;extended-key&nbsp;flag,&nbsp;context&nbsp;code,&nbsp;previous&nbsp;key-state&nbsp;flag,&nbsp;and&nbsp;transition-state&nbsp;flag,&nbsp;as&nbsp;shown&nbsp;in&nbsp;the&nbsp;following&nbsp;table.&nbsp;Value&nbsp;Description&nbsp;<br>0–15&nbsp;Specifies&nbsp;the&nbsp;repeat&nbsp;count&nbsp;for&nbsp;the&nbsp;current&nbsp;message.&nbsp;The&nbsp;value&nbsp;is&nbsp;the&nbsp;number&nbsp;of&nbsp;times&nbsp;the&nbsp;keystroke&nbsp;is&nbsp;autorepeated&nbsp;as&nbsp;a&nbsp;result&nbsp;of&nbsp;the&nbsp;user&nbsp;holding&nbsp;down&nbsp;the&nbsp;key.&nbsp;If&nbsp;the&nbsp;keystroke&nbsp;is&nbsp;held&nbsp;long&nbsp;enough,&nbsp;multiple&nbsp;messages&nbsp;are&nbsp;sent.&nbsp;However,&nbsp;the&nbsp;repeat&nbsp;count&nbsp;is&nbsp;not&nbsp;cumulative.&nbsp;<br>16–23&nbsp;Specifies&nbsp;the&nbsp;scan&nbsp;code.&nbsp;The&nbsp;value&nbsp;depends&nbsp;on&nbsp;the&nbsp;original&nbsp;equipment&nbsp;manufacturer&nbsp;(OEM).&nbsp;<br>24&nbsp;Specifies&nbsp;whether&nbsp;the&nbsp;key&nbsp;is&nbsp;an&nbsp;extended&nbsp;key,&nbsp;such&nbsp;as&nbsp;the&nbsp;right-hand&nbsp;ALT&nbsp;and&nbsp;CTRL&nbsp;keys&nbsp;that&nbsp;appear&nbsp;on&nbsp;an&nbsp;enhanced&nbsp;101-&nbsp;or&nbsp;102-key&nbsp;keyboard.&nbsp;The&nbsp;value&nbsp;is&nbsp;1&nbsp;if&nbsp;it&nbsp;is&nbsp;an&nbsp;extended&nbsp;key;&nbsp;otherwise,&nbsp;it&nbsp;is&nbsp;0.&nbsp;<br>25–28&nbsp;Reserved;&nbsp;do&nbsp;not&nbsp;use.&nbsp;<br>29&nbsp;Specifies&nbsp;the&nbsp;context&nbsp;code.&nbsp;The&nbsp;value&nbsp;is&nbsp;always&nbsp;0&nbsp;for&nbsp;a&nbsp;WM_KEYDOWN&nbsp;message.&nbsp;<br>30&nbsp;Specifies&nbsp;the&nbsp;previous&nbsp;key&nbsp;state.&nbsp;The&nbsp;value&nbsp;is&nbsp;1&nbsp;if&nbsp;the&nbsp;key&nbsp;is&nbsp;down&nbsp;before&nbsp;the&nbsp;message&nbsp;is&nbsp;sent,&nbsp;or&nbsp;it&nbsp;is&nbsp;zero&nbsp;if&nbsp;the&nbsp;key&nbsp;is&nbsp;up.&nbsp;<br>31&nbsp;Specifies&nbsp;the&nbsp;transition&nbsp;state.&nbsp;The&nbsp;value&nbsp;is&nbsp;always&nbsp;zero&nbsp;for&nbsp;a&nbsp;WM_KEYDOWN&nbsp;message&nbsp;<br><br><br>Return&nbsp;Values<br>An&nbsp;application&nbsp;should&nbsp;return&nbsp;zero&nbsp;if&nbsp;it&nbsp;processes&nbsp;this&nbsp;message.&nbsp;<br><br>Remarks<br>If&nbsp;the&nbsp;F10&nbsp;key&nbsp;is&nbsp;pressed,&nbsp;the&nbsp;DefWindowProc&nbsp;function&nbsp;sets&nbsp;an&nbsp;internal&nbsp;flag.&nbsp;When&nbsp;DefWindowProc&nbsp;receives&nbsp;the&nbsp;WM_KEYUP&nbsp;message,&nbsp;the&nbsp;function&nbsp;checks&nbsp;whether&nbsp;the&nbsp;internal&nbsp;flag&nbsp;is&nbsp;set&nbsp;and,&nbsp;if&nbsp;so,&nbsp;sends&nbsp;a&nbsp;WM_SYSCOMMAND&nbsp;message&nbsp;to&nbsp;the&nbsp;top-level&nbsp;window.&nbsp;The&nbsp;wParam&nbsp;parameter&nbsp;of&nbsp;the&nbsp;message&nbsp;is&nbsp;set&nbsp;to&nbsp;SC_KEYMENU.&nbsp;<br><br>Because&nbsp;of&nbsp;the&nbsp;autorepeat&nbsp;feature,&nbsp;more&nbsp;than&nbsp;one&nbsp;WM_KEYDOWN&nbsp;message&nbsp;may&nbsp;be&nbsp;posted&nbsp;before&nbsp;a&nbsp;WM_KEYUP&nbsp;message&nbsp;is&nbsp;posted.&nbsp;The&nbsp;previous&nbsp;key&nbsp;state&nbsp;(bit&nbsp;30)&nbsp;can&nbsp;be&nbsp;used&nbsp;to&nbsp;determine&nbsp;whether&nbsp;the&nbsp;WM_KEYDOWN&nbsp;message&nbsp;indicates&nbsp;the&nbsp;first&nbsp;down&nbsp;transition&nbsp;or&nbsp;a&nbsp;repeated&nbsp;down&nbsp;transition.&nbsp;<br><br>For&nbsp;enhanced&nbsp;101-&nbsp;and&nbsp;102-key&nbsp;keyboards,&nbsp;extended&nbsp;keys&nbsp;are&nbsp;the&nbsp;right&nbsp;ALT&nbsp;and&nbsp;CTRL&nbsp;keys&nbsp;on&nbsp;the&nbsp;main&nbsp;section&nbsp;of&nbsp;the&nbsp;keyboard;&nbsp;the&nbsp;INS,&nbsp;DEL,&nbsp;HOME,&nbsp;END,&nbsp;PAGE&nbsp;UP,&nbsp;PAGE&nbsp;DOWN,&nbsp;and&nbsp;arrow&nbsp;keys&nbsp;in&nbsp;the&nbsp;clusters&nbsp;to&nbsp;the&nbsp;left&nbsp;of&nbsp;the&nbsp;numeric&nbsp;keypad;&nbsp;and&nbsp;the&nbsp;divide&nbsp;(/)&nbsp;and&nbsp;ENTER&nbsp;keys&nbsp;in&nbsp;the&nbsp;numeric&nbsp;keypad.&nbsp;Other&nbsp;keyboards&nbsp;may&nbsp;support&nbsp;the&nbsp;extended-key&nbsp;bit&nbsp;in&nbsp;the&nbsp;lParam&nbsp;parameter.&nbsp;<br><br>Windows&nbsp;2000/XP:&nbsp;Applications&nbsp;must&nbsp;pass&nbsp;wParam&nbsp;to&nbsp;TranslateMessage&nbsp;without&nbsp;altering&nbsp;it&nbsp;at&nbsp;all.
 
很好,很强大.![:D]
 
后退
顶部