一个初学者的问题.(50分)

  • 主题发起人 主题发起人 阿汀
  • 开始时间 开始时间

阿汀

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp;我在我的主窗体中用CreateWindow函数创建了一个ClassName为'EDIT'<br>的Child window,现在我想为该Child window 写一个按键事件处理程<br>序(WM_KeyDown),例如:我按F1,使其字体颜色变为红色等等,我该如<br>何写程序,最好把语句写出来,Thank you very much!
 
ask to cakk
 
初学者真厉害,今后一定前程无限.
 
&nbsp; 已经有的组件最好不要用CreateWindow建立,这样建立的控件只能用Api函数<br>去更改它的属性太麻繁。除非有些Delphi没有可视化的控件如Ip地址编辑框等我<br>建议最好不要用CreateWindow建立。
 
问我?? 我说过我会吗?
 
procedure TForm1.MyOnMessage(var Msg: TMsg; var Handled: Boolean) ;<br>begin<br>&nbsp; if msg.message=256 then //有键按下<br>&nbsp; begin<br>&nbsp; //.....<br>&nbsp; &nbsp;if msg.wParam=112 then //F1<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;//...<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;if msg.wParam=113 then //F2<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;//...<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;//...<br>&nbsp; end;<br><br>&nbsp; if msg.message=256 then ////有键放开<br>&nbsp; begin<br>&nbsp; //.....<br>&nbsp; &nbsp;if msg.wParam=112 then //F1<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;//...<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;if msg.wParam=113 then //F2<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;//...<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;//...<br>&nbsp; end;<br>end;<br><br>在form的OnCreate事件里:<br>&nbsp; application.OnMessage:=MyOnMessage;
 
向你推荐西蒙与殊斯特公司出版的DELPHI自学书籍,你的问题在上边都有。
 
同意Fencer.<br>这样一来,所有的操作都要用API来完成,烦死了,比如你设置个字体颜色都很麻烦!<br><br>既然是你自己的程序,为什么不用现成的东西?
 
这个问题已经有头绪,请等待...<br><br>提示一下: 用createwindow创建的window,你必须先子类化一个新的子类出来,<br>然后将窗口定义成该子类,然后用setwiondowlong来设置它的wndproc.最后在<br>wndproc里面处理按键消息.
 
不用这么复杂,这样也可以,不过怎么设置字体颜色不太清楚;)<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; &nbsp;procedure mymessage(var msg:TMsg;var handled:boolean);<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; hb: hwnd;<br>implementation<br><br>{$R *.DFM}<br>var<br>&nbsp; oldmessage:TMessageEvent;<br>procedure tform1.mymessage(var msg:TMsg;var handled:boolean);<br>begin<br>&nbsp; if @oldmessage&lt;&gt;nil then<br>&nbsp; &nbsp; oldmessage(msg,handled);<br>&nbsp; if msg.hwnd=hb then<br>&nbsp; begin<br>&nbsp; &nbsp; if msg.message=WM_KEYDOWN then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if msg.wParam=VK_F1 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; msg.wParam:=integer('2');<br>// &nbsp; &nbsp; &nbsp; &nbsp;handled:=true;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br><br>&nbsp; hb:=Createwindow('edit',nil,ws_child or ws_visible or ws_border,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10,10,200,20,self.handle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hmenu(-1),hinstance,nil);<br>&nbsp; oldmessage:=application.OnMessage;<br>&nbsp; application.OnMessage:=mymessage;<br>end;<br><br>end.
 
唉,瞧瞧人家初学者,再看看你自己。唉!
 
下面程序可捕获键盘如入,至于快捷键课做出相应的处理<br>unit Unit4;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm4 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure FormShow(Sender: TObject);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; OldWndMeth, NewWndMeth: Pointer;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form4: TForm4;<br>&nbsp; he:Thandle;<br><br>implementation<br><br>{$R *.DFM}<br><br>var<br>&nbsp; OldWndProc: Pointer = nil;<br><br>function NewWinProc (Handle: THandle;<br>&nbsp; Msg, wParam, lParam: LongInt): LongInt; stdcall;<br>begin<br>&nbsp; if Msg = wm_keydown then<br>&nbsp; begin<br>&nbsp; &nbsp; Beep;<br>&nbsp; &nbsp; SetWindowText (Handle,<br>&nbsp; &nbsp; &nbsp; PChar (Format (' %s', [char(lParam)])));<br>&nbsp; end;<br>&nbsp; // pass call to old window proc<br>&nbsp; Result := CallWindowProc (OldWndProc, Handle,<br>&nbsp; &nbsp; Msg, wParam, lParam);<br>end;<br><br>procedure TForm4.FormShow(Sender: TObject);<br>begin<br>&nbsp; he:=createwindow('edit','edit',ws_border or ws_visible or ws_child,10,10,<br>&nbsp; 100,40,handle,0,hinstance,0);<br>end;<br><br>procedure TForm4.Button1Click(Sender: TObject);<br>begin<br>&nbsp; OldWndProc := Pointer (SetWindowLong<br>&nbsp; &nbsp; (he, gwl_WndProc, LongInt (@NewWinProc)));<br>end;<br><br>end.<br>
 
既然大家写的差不多了,我就不写了,就是这个意思.<br><br>(我的代码总有点问题. :-P)
 
to amo:<br>但是,我再加一个button,点击后却出现了一个堆栈溢出的错误!是不是处理函数没有<br>正常返回?<br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; if IsWindow(hb) then<br>&nbsp; begin<br>&nbsp; &nbsp; DestroyWindow(hb);<br>&nbsp; end;<br>&nbsp; hb1:=Createwindow('edit',nil,ws_child or ws_visible or ws_border,<br>&nbsp; &nbsp; &nbsp; &nbsp;300,300,200,20,self.handle,<br>&nbsp; &nbsp; &nbsp; &nbsp; hmenu(-1),hinstance,nil);<br>&nbsp; oldmessage:=application.OnMessage;<br>&nbsp; application.OnMessage:=mymessage;<br>end;<br>
 
你已不是初学者了!
 
阿汀:<br>我的代码如何<br>
 
procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; if IsWindow(hb) then<br>&nbsp; begin<br>&nbsp; &nbsp; DestroyWindow(hb);<br>//*****************************************<br>&nbsp; &nbsp; application.onmessage:=oldmessage; <br>//*****************************************<br>&nbsp; end;<br>&nbsp; hb1:=Createwindow('edit',nil,ws_child or ws_visible or ws_border,<br>&nbsp; &nbsp; &nbsp; &nbsp;300,300,200,20,self.handle,<br>&nbsp; &nbsp; &nbsp; &nbsp; hmenu(-1),hinstance,nil);<br>&nbsp; oldmessage:=application.OnMessage;<br>&nbsp; application.OnMessage:=mymessage;<br>end;<br>注意,<br>如果这样用,<br>必须加上<br>&nbsp; &nbsp; application.onmessage:=oldmessage; <br>因为在你 &nbsp; &nbsp;DestroyWindow(hb);<br>后,onmessage仍然在用新的onmessage,而hb已经被destroy了。<br><br>
 
to all:<br>&nbsp; Thank you for your help, and the answers of amo and Liu JZX were<br>accpted.
 
后退
顶部