delphi2007 教程

delphi2007 教程

首页 新随笔 联系 聚合 管理
  1013 Posts :: 0 Stories :: 28 Comments :: 0 Trackbacks
窗体创建和函数关联相关的两个小问题 VCL组件开发及应用
http://www.delphi2007.net/DelphiVCL/html/delphi_20061222112800193.html
1,用窗体名称作为传入参数,创建窗体,怎么避免重复创建?  
  如下面代码中注释调的部分;使用not   assigned()判断返回的结果每次都是false  
  也就是说,assigned判断结果一直为true。  
  function   JaxCreateForm(const   AFormName:string):boolean;  
  var  
      AForm:   TForm;  
      AFormClass:   TFormClass;  
  begin  
      try  
          if   FindClass(AFormName)   <>   nil   then  
          begin  
              try  
                  AFormClass   :=   TFormClass(FindClass(AFormName));  
  //             if   not   assigned(TForm(copy(AFormName,2,Length(AFormName)-1)))   then  
                  AForm   :=   AFormClass.Create(nil);  
                  AForm.Show;             //mdi类型form不能使用showmodal释放窗体;  
              except  
                  AForm.Free;  
                  AForm   :=   nil;  
              end;  
          end  
          else   begin  
              showmessage(format('创建窗体"%s"出错!',[AFormName]));  
          end;  
      except  
      end;  
  end;

2,动态创建的菜单项,怎么将其onclick事件与全局tnotifyevent类型的函数关联起来。  
  with   tbutton.create(nil)   do  
  begin  
      parent   :=   self;  
      caption   :=   '';  
      onclick   :=   ?.  
  end;

这个   窗口是不是自动创建的?

Q:   1,用窗体名称作为传入参数,创建窗体,怎么避免重复创建?  
   
  A:   看实例:  
   
  procedure   TActionsModule.OpenForm(FormClass:   TFormClass;   var   fm;  
      AOwner:   TComponent);  
  var  
      Idx:   Integer;  
      Child:   TForm;  
  begin  
      for   Idx   :=   0   to   Pred(Screen.FormCount)   do  
          if   Screen.Forms[Idx].ClassType   =   FormClass   then  
          begin  
                  Child   :=   Screen.Forms[Idx];  
                  if   Child.WindowState   =   wsMinimized   then  
                        ShowWindow(Child.handle,SW_SHOWNORMAL)  
                  else  
                        ShowWindow(Child.handle,SW_SHOWNA);  
                  if   (not   Child.Visible)   then   Child.Visible:=True;  
                  Child.BringToFront;  
                  Child.Setfocus;  
                  TForm(fm):=Child;  
                  exit;  
          end;  
      Child   :=   TForm(FormClass.NewInstance);  
      TForm(fm):=   Child;  
      Child.Create(AOwner);  
  end;  
   
  OpenForm(TLogInformationForm,   LogInformationForm,   Application.MainForm);  
   
  Q:,动态创建的菜单项,怎么将其onclick事件与全局tnotifyevent类型的函数关联起来。  
   
  A:   看实例  
   
  procedure   TMainForm.ButtonOnClick(Sender:   TObject);  
  begin  
      if   Sender   is   TButton   then  
          Showmessage((Sender   as   TButton).Caption);  
  end;  
   
  with   tbutton.create(nil)   do  
  begin  
      parent   :=   self;  
      caption   :=   '';  
      onclick   :=   ButtonOnClick  
  end  
   
 

Q:,动态创建的菜单项,怎么将其onclick事件与全局tnotifyevent类型的函数关联起来。  
   
  with   tbutton.create(nil)   do  
   
  ----------------------------  
   
  你的问题是菜单项,   而你的代码是TBUTTON。呵呵。   没有关系,菜单项也是一样的

1,看到了老冯的实例,不过我的创建方式和你有点不同;现在已经自己解决了;  
  将代码AForm:=   AForm.create(nil);改成..create(Application);  
  然后用Application.findcomponent('AFormName');就可以了。  
   
  2,onclick   :=   ButtonOnClick;  
  中如果buttononclick是在同一个类下是可以;但,相反,如果buttononclick是一个全局定义函数,不属于任何类下的话,好像不行。  
  再次请教。

对不起,   刚才一直无法上来。  
   
  procedure   ButtonOnClick(Sender:   TObject);  
  begin  
      Showmessage('Pointer,   Pointer   ,   I   Hate   You!');  
  end;  
   
  procedure   TForm1.FormCreate(Sender:   TObject);  
  begin  
      with   TButton.Create(nil)   do  
      begin  
          Parent   :=   Self;  
          Visible   :=   True;  
          @OnClick   :=   @ButtonOnClick;  
      end;  
  end;  
 

posted on 2008-11-18 14:27 delphi2007 阅读(152) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。