如何在Notepad上加入自己的菜单项,并响应Onclick(300分)

  • 主题发起人 主题发起人 歪就歪
  • 开始时间 开始时间

歪就歪

Unregistered / Unconfirmed
GUEST, unregistred user!
300分求详细解决方案:<br><br>在Notepad上加入自己的菜单项,并能响应Onclick事件<br><br>这不是吃饱了撑的么!自己编一个Notepad不就得了?!没辙,<br>我老板非要这么做:用Delphi 程序找到运行着的Notepad,并<br>给它加上一个菜单项,当这菜单项被Click时,在显示的文件<br>的指定位置上加入一个字串。<br><br>我现在能做到的是:<br><br>1、用FindWindow找到Notepad<br>2、读出Notepad的内容,<br>3、用CreateWindows在Notepad上放一个“死”Button,(MenuItem<br>不会加,只好先用Button试试)<br>4、在内容中加入字串(但不是指定位置)<br><br>剩下的就实在是做不出来了。<br><br>代码如下:<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; Memo1: TMemo;<br>&nbsp; &nbsp; btnGetText: TButton;<br>&nbsp; &nbsp; btnWriteText: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; btnCreateBtn: TButton;<br>&nbsp; &nbsp; btnCreateMenu: TButton;<br>&nbsp; &nbsp; procedure btnGetTextClick(Sender: TObject);<br>&nbsp; &nbsp; procedure btnWriteTextClick(Sender: TObject);<br>&nbsp; &nbsp; procedure btnCreateBtnClick(Sender: TObject);<br>&nbsp; &nbsp; procedure btnCreateMenuClick(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; WindowCaption : String;<br>&nbsp; AimWindow : HWND;<br>&nbsp; FoundWnd : HWND;<br><br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.btnGetTextClick(Sender: TObject);<br>var<br>&nbsp; Buf:array [0..1024] of char;<br>&nbsp; &nbsp; Wnd : HWND;<br>begin<br>&nbsp; Memo1.Clear;<br>&nbsp; WindowCaption := Edit2.Text;<br>&nbsp; FoundWnd := 0;<br>&nbsp; AimWindow := FindWindow(PChar(WindowCaption), nil);<br>&nbsp; if AimWindow &lt;&gt;0 then begin<br>&nbsp; &nbsp; Wnd := GetWindow(AimWindow, GW_CHILD);<br>&nbsp; &nbsp; while Wnd &lt;&gt; 0 do begin<br>&nbsp; &nbsp; &nbsp; FoundWnd := Wnd;<br>&nbsp; &nbsp; &nbsp; SendMessage(Wnd, WM_GETTEXT, 1024, LongInt(@Buf));<br>&nbsp; &nbsp; &nbsp; Memo1.Lines.Add(Buf);<br>&nbsp; &nbsp; &nbsp; Wnd :=GetWindow(Wnd, GW_HWNDNEXT);<br>&nbsp; &nbsp; end;<br>&nbsp; end<br>&nbsp; else ShowMessage('Can not found Window: ' + WindowCaption)<br><br>end;<br><br>procedure TForm1.btnWriteTextClick(Sender: TObject);<br>var Wnd : HWND;<br>&nbsp; &nbsp; Buf : array [0..256] of char;<br>&nbsp; &nbsp; St : String;<br>begin<br>&nbsp; WindowCaption := Edit2.Text;<br>&nbsp; St := Edit1.Text;<br>&nbsp; AimWindow := FindWindow(PChar(WindowCaption), nil);<br>&nbsp; if AimWindow &lt;&gt; 0 then begin<br>&nbsp; &nbsp; Wnd := GetWindow(AimWindow, GW_CHILD);<br>&nbsp; &nbsp; if Wnd &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; SendMessage(Wnd, WM_SETTEXT, 256, LongInt(PChar(st)));<br>&nbsp; end;<br>end;<br><br>procedure TForm1.btnCreateBtnClick(Sender: TObject);<br>begin<br>&nbsp; if AimWindow &lt;&gt; 0 then begin<br>&nbsp; &nbsp; if (CreateWindowEx(WS_EX_TOPMOST, 'Button', nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ws_child or ws_visible or ws_border or bs_PushButton,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10,10, 70, 30, AimWindow, 100, 0, nil)=0) then<br>&nbsp; &nbsp; &nbsp; showmessage('not work');<br>&nbsp; end<br>&nbsp; else showmessage('window not exist');<br>end;<br><br>procedure TForm1.btnCreateMenuClick(Sender: TObject);<br>begin<br>&nbsp; if AimWindow &lt;&gt; 0 then begin<br>&nbsp; &nbsp; if (CreateWindowEx(WS_EX_TOPMOST, 'SubMenu', nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ws_child or ws_visible or ws_border or bs_PushButton,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10,10, 70, 30, AimWindow, 100, 0, nil)=0) then<br>&nbsp; &nbsp; &nbsp; showmessage('not work');<br>&nbsp; end<br>&nbsp; else showmessage('window not exist');<br><br>end;<br><br>end.<br><br>各位帮帮忙吧,别三言两语的,给我点CODE,我出300分。<br><br>这叫什么事儿,杀了老板的心都有!<br>
 
呵呵, 你还是杀了老板先:-)
 
var aa:THandle;<br>&nbsp; &nbsp; bb:Hmenu;<br>begin<br>&nbsp; aa:=findwindow(......)<br>&nbsp; bb:=windows.GetMenu(aa);<br>&nbsp; windows.AppendMenu(....<br><br>剩下的我不会了。<br>
 
To: cyTown:别呀,我还是先自杀吧!<br>To: www:谢谢,我会试试:GetMenu和AppendMenu,<br><br>不过,问题还远远没有解决……
 
加菜单我调成功了,但不能响应事件,别急,慢满来。<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, Menus;<br><br>type<br>&nbsp; TForm1aa = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; MainMenu1: TMainMenu;<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; aaa: TMenuItem;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure AppendTo(Item: TMenuItem; Menu: HMENU; nPos: integer);<br>&nbsp; &nbsp; procedure aaaClick(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1aa: TForm1aa;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1aa.Button1Click(Sender: TObject);<br>var aa:thandle;<br>&nbsp; &nbsp; bb:hmenu;<br>begin<br>&nbsp; aa:=findwindow('TForm1','form1'); &nbsp;//我的一个程序名<br>&nbsp; bb:=windows.GetMenu(aa);<br>&nbsp; appendto(popupmenu1.Items[0],bb,0); &nbsp; //加入采单<br>end;<br><br>procedure Tform1aa.AppendTo(Item: TMenuItem; Menu: HMENU; nPos: integer);<br>const<br>&nbsp; IBreaks: array[TMenuBreak] of Longint = (MFT_STRING, MFT_MENUBREAK, MFT_MENUBARBREAK);<br>&nbsp; IChecks: array[Boolean] of Longint = (MFS_UNCHECKED, MFS_CHECKED);<br>&nbsp; IDefaults: array[Boolean] of Longint = (0, MFS_DEFAULT);<br>&nbsp; IEnables: array[Boolean] of Longint = (MFS_DISABLED or MFS_GRAYED, MFS_ENABLED);<br>&nbsp; IRadios: array[Boolean] of Longint = (MFT_STRING, MFT_RADIOCHECK);<br>&nbsp; ISeparators: array[Boolean] of Longint = (MFT_STRING, MFT_SEPARATOR);<br>var<br>&nbsp; MenuItemInfo: TMenuItemInfo;<br>&nbsp; aCaption: string;<br>&nbsp; NewFlags: Integer;<br>begin<br>&nbsp; if Item.Visible then with Item do begin<br>&nbsp; &nbsp; aCaption := Caption;<br>&nbsp; &nbsp; if Count &gt; 0 then MenuItemInfo.hSubMenu := Handle<br>&nbsp; &nbsp; else if (ShortCut &lt;&gt; scNone) and ((Parent = nil) or<br>&nbsp; &nbsp; &nbsp; (Parent.Parent &lt;&gt; nil) or not (Parent.Owner is TMainMenu)) then<br>&nbsp; &nbsp; &nbsp; aCaption := aCaption + #9 + ShortCutToText(ShortCut);<br>&nbsp; &nbsp; if Lo(GetVersion) &gt;= 4 then begin<br>&nbsp; &nbsp; &nbsp; MenuItemInfo.cbSize := SizeOf(TMenuItemInfo);<br>&nbsp; &nbsp; &nbsp; MenuItemInfo.fMask := MIIM_CHECKMARKS or MIIM_DATA or MIIM_ID or<br>&nbsp; &nbsp; &nbsp; &nbsp; MIIM_STATE or MIIM_SUBMENU or MIIM_TYPE;<br>&nbsp; &nbsp; &nbsp; MenuItemInfo.fType := IRadios[RadioItem] or IBreaks[Break] or<br>&nbsp; &nbsp; &nbsp; &nbsp; ISeparators[Caption = '-'];<br>&nbsp; &nbsp; &nbsp; MenuItemInfo.fState := IChecks[Checked] or IEnables[Enabled]<br>&nbsp; &nbsp; &nbsp; &nbsp; or IDefaults[Default];<br>&nbsp; &nbsp; &nbsp; MenuItemInfo.wID := Command;<br>&nbsp; &nbsp; &nbsp; MenuItemInfo.hSubMenu := 0;<br>&nbsp; &nbsp; &nbsp; MenuItemInfo.hbmpChecked := 0;<br>&nbsp; &nbsp; &nbsp; MenuItemInfo.hbmpUnchecked := 0;<br>&nbsp; &nbsp; &nbsp; MenuItemInfo.dwTypeData := PChar(aCaption);<br>&nbsp; &nbsp; &nbsp; if Count &gt; 0 then MenuITemInfo.hSubMenu := Handle;<br>&nbsp; &nbsp; &nbsp; InsertMenuItem(Menu, nPos, True, MenuItemInfo);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else begin<br>&nbsp; &nbsp; &nbsp; NewFlags := IBreaks[Break] or IChecks[Checked] or IEnables[Enabled] or<br>&nbsp; &nbsp; &nbsp; &nbsp; ISeparators[Caption = '-'] or MF_BYPOSITION;<br>&nbsp; &nbsp; &nbsp; if Count &gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; InsertMenu(Menu, nPos, MF_POPUP or NewFlags, Handle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PChar(aCaption))<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; InsertMenu(Menu, nPos, NewFlags, Command, PChar(aCaption));<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br><br>procedure TForm1aa.aaaClick(Sender: TObject);<br>begin<br>&nbsp; showmessage('saf');<br>end;<br><br>end.<br>
 
加个菜单倒是很简单<br><br>InsertMenu(GetMenu(FindWindow('notepad',nil)), $FFFFFFFF, MF_STRING or MF_BYPOSITION, 4,'test');<br><br>要响应ONCLICK没那么容易。
 
就算你把菜单加上去了,也能响应事件,<br>那么事件响应中的代码怎么执行呢?<br>Notepad没法执行你的程序中的代码吧??<br><br>考虑一下用钩子,先解决这个问题<br>否则白搭
 
加菜单可以用www的,但处理click事件可能必须得写一个全局的消息钩子<br>处理wh_msgfilter(响应菜单消息)或WH_GETMESSAGE(可能不如<br>wh_msgfilter好)
 
有这些时间,自己做一个notepad都出来了,反正notepad的功能这么简单,<br>你干脆做一个和他界面一模一样的,万一下次你们老板再提出什么其他的<br>非礼要求时,不就简单多了.
 
谢谢,请各位再帮忙给解决一下。
 
实在对不起,消息钩子我不会做。<br>不过你的老板也真够可以的,你把他炒了吧!
 
这种老板最可怕,<br>狗屁不懂,想象力还特别丰富,<br>对付他的办法:<br>1、阳奉阴违<br>2、炒<br>否则迟早被他逼疯,你不想吧?
 
给WWW,你已经给我很多Source Code的了,我的水平,如果<br>没Source Code,我自己总是扣不出来。谢谢你。<br><br>你们说的钩子,是hook么?或是Callback?哪位能帮忙给做<br>一个?知道应该不容易,所以才咬牙出300分呢。<br><br>HELP???
 
没错, 没错, 在你没自杀前.....
 
你在论坛里搜索"hook",一大把,自己看吧。
 
钩子作出来了,sent out to YjiuY.
 
给&lt;a href=mailto:tommm@371.net&gt;我&lt;/a&gt;一份
 
to www: demo.zip sent out.<br><br>完全符合YJY的要求。<br><br>
 
后退
顶部