delphi中动态创建了Form1,Label1,chart1,可是不知道在chart1的mousemove中如何调用label1啊?恳求高手赐教。

解决方案 »

  1.   

    Label1定义成Form1内的全局变量
      

  2.   

    声明Tlabel变量,如果是多个可以考虑数组
      

  3.   


    這在乎於FORM1這個變量是不是全局的。form1 := TForm.create;
    // 只要 form1  是全局的且有引用實例就可以直接var 
     vStr : String; vStr :=  form1.label1.caption ;  //  這樣來試。
      

  4.   

    冒昧打扰了啊
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls, TeeProcs, TeEngine, Chart, Series;type
      TForm1 = class(TForm)
        Button1: TButton;
        Chart1: TChart;
        Series1: TLineSeries;
        Label1: TLabel;
        Panel1: TPanel;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        //Procedure chartMouseDown(Sender:Tobject;Button:TMousebutton;shift:TshiftState;X,Y:integer);
        procedure ChartMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
        procedure FormShow(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure Button2Click(Sender: TObject);
        //procedure Button3Click(Sender: TObject);
        //procedure   CMMouseEnter(var Message:   TMessage);   message   CM_MOUSEENTER;
      private
        { Private declarations }  public
        { Public declarations }
         OldX,OldY:Longint;
        CrossHairColor:TColor;
        CrossHairStyle:TPenStyle;
      end; TMypanel=class(Tpanel)
          public
            procedure Setcolor;
          end;var
      Form1: TForm1;
      mypanel1:TMyPanel;
      Fchart:Tform;implementation{$R *.dfm}(*procedure TMyPanel.setcolor;
    begin
        self.color:=clRed;
    end;*)procedure TForm1.Button1Click(Sender: TObject);//主要使这里面
    var
      //Fchart:Tform;
      mychart:Tchart;
      myseries:Tlineseries;
      njseries:Tlineseries;
      XY_Label:Tlabel;
      i:integer;
      j:integer;
      const scale:single =0.85 ;
    begin
      Fchart:=Tform.Create(self);//******关键一:动态生成了窗体Fchart
      Fchart.Width:=800;
      Fchart.Height:=600;
      Fchart.Position:=poScreenCenter;
      Fchart.Show;  XY_Label:=TLabel.Create(self);
      XY_Label.Parent:=Fchart;
      XY_Label.Width:=100;
      XY_label.Left:=0;
      XY_label.Top:=0;  mychart:=Tchart.Create(self);//******关键二:动态生成了mychart并定义mychart.parent:=Fchart
      mychart.Parent:=Fchart;
      //mychart.OnMouseDown:=chartMouseDown;
      mychart.OnMouseMove:=chartMouseMove;  //*********定义了mychart的mousemove事件
      //mychart.on
      with mychart do
      begin  width:=trunc(Fchart.Width * scale);
      Height:=trunc(Fchart.Height * scale);
      left:=trunc((Fchart.Width-width)/2);
      top:=trunc((Fchart.Height-Height)/2);
      Color:=clwhite;
      View3D:=false; //整个面板的颜色设置为白色
      Title.Text.Text:='轴力-扭矩-转角曲线';
      Title.Font.Size:=13;
      BottomAxis.Title.Caption:='转角(°)';
      BottomAxis.Title.Font.Size:=13;
      LeftAxis.Title.Caption:='夹紧力(kN)';
      LeftAxis.Title.Font.Color:=clred;
      leftAxis.Title.Font.Size:=13;
      RightAxis.Title.Caption:='扭矩(N·m)';
      RightAxis.Title.Font.Size:=13;
      RightAxis.Title.Font.Color:=clBlue;
      marginleft:=1;
      marginright:=1;
      marginBottom:=1;  end;
      myseries:=Tlineseries.Create(self);//*************实例化了两个数据系列myseries,njseries
      myseries.ParentChart:=mychart;
      njseries:=Tlineseries.Create(self);
      njseries.ParentChart:=mychart;
      mychart.series[1].VertAxis:=aRightAxis;  with myseries do
      begin
         for i:=0 to 10 do
         begin
             addxy(300*i/5.4+i,2.5*i*i,inttostr(i));
         end;
      end;
      with njseries do
      begin
          LinePen.Style:=psDash;
          linepen.Width:=2;
          for j:=5 to 15 do
          begin
              addxy(200*j/4.78,j*j*j+200*j,inttostr(j),clblue);
          end;
      end;
      //固定Tchart中的坐标,不使Tchart中的坐标随Series的变化而变化
      with  myChart.LeftAxis do
      begin
          Automatic:=False;      minimum:=0;
          maximum:=1500;
          increment:=300;
          MinorTickCount:=4;  end;
      with  myChart.RightAxis do
      begin
          Automatic:=False;
          minimum:=0;
          maximum:=3500;
          increment:=700;
          MinorTickCount:=4;
      end;
      (*with  myChart.bottomAxis do
      begin
          //Automatic:=False;
         //setminmax(0,500);
          increment:=50;
          MinorTickCount:=4;
      end;*)
      //mychart.Legend.Alignment:=laLeft;
      mychart.Legend.LegendStyle:=lsSeries;  myseries.Title:='夹紧力-转角';
      njseries.Title:='扭矩-转角';
    end;
    {procedure TForm1.ChartMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
     Var
       chart:Tchart;
    begin
      chart:=Sender as Tchart;
      //showmessage('('+floattostr(chart.Series[0].YScreenToValue(X))+','+floattostr(chart.Series[1].YScreenToValue(Y))+')');
    end; }procedure Tform1.ChartMouseMove(Sender:TObject;Shift:TShiftState;X,Y:integer);//***********关键三:mychart的mousemove事件
    { This procedure draws the crosshair lines }Procedure DrawCross(AX,AY:Integer;Sender:TObject);//定义了一个画十字的过程
    var
      chart:Tchart;
      begin
        chart:=Sender as Tchart;//%%%%%%%%%最重要的地方:我想在自定义的DrawCross中将X、Y值fchart(窗体)中动态创建的空间label1中,我会动态创建label1,
                                     //%%%%%%%%可是不知道在DrawCross中如何引用啊?恳求大侠救助啊。
        With Chart.Canvas,chart do
        begin
          Pen.Color:=clYellow;
          Pen.Style:=psSolid;
          Pen.Mode:=pmXor;
          Pen.Width:=1;
          MoveTo(ax,ChartRect.Top-Height3D);
          LineTo(ax,ChartRect.Bottom-Height3D);
          MoveTo(ChartRect.Left+Width3D,ay);
          LineTo(ChartRect.Right+Width3D,ay);
          chart.Title.Text.Text:='转角='+formatfloat('0.##',chart.Series[0].XScreenToValue(X))+',夹紧力=' +formatfloat('0.##',chart.Series[0].YScreenToValue(Y))+',扭矩='+formatfloat('0.##',chart.Series[1].YscreenToValue(Y));    end;
      end;
    Var
      chart:Tchart;
    begin
      chart:=Sender as Tchart;
      if (OldX<>-1) then
      begin
        DrawCross(OldX,OldY,chart);  { draw old crosshair }
        OldX:=-1;
      end;
      if PtInRect( Chart.ChartRect, Point(X-Chart.Width3D,Y+Chart.Height3D)  ) then
      begin
        DrawCross(x,y,chart);  { draw crosshair at current position }
        { store old position }
        OldX:=x;
        OldY:=y;
      end;
    end;
    procedure TForm1.FormShow(Sender: TObject);
    begin
    //chart1.Cursor:=crCross;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Series1.FillSampleValues(30);  { <-- some random values }
      OldX:=-1;                          { initialize variables }
      CrossHairColor:=clYellow;
      CrossHairStyle:=psSolid;
    end;procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);{ This procedure draws the crosshair lines }
      Procedure DrawCross(AX,AY:Integer);
      begin
        With Chart1,Canvas do
        begin
          Pen.Color:=CrossHairColor;
          Pen.Style:=CrossHairStyle;
          Pen.Mode:=pmXor;
          Pen.Width:=1;
          MoveTo(ax,ChartRect.Top-Height3D);
          LineTo(ax,ChartRect.Bottom-Height3D);
          MoveTo(ChartRect.Left+Width3D,ay);
          LineTo(ChartRect.Right+Width3D,ay);
        end;
      end;Var tmpX,tmpY:Double;
    begin
      if (OldX<>-1) then
      begin
        DrawCross(OldX,OldY);  { draw old crosshair }
        OldX:=-1;
      end;  { check if mouse is inside Chart rectangle }
      if PtInRect( Chart1.ChartRect, Point(X-Chart1.Width3D,Y+Chart1.Height3D)  ) then
      begin
        DrawCross(x,y);  { draw crosshair at current position }
        { store old position }
        OldX:=x;
        OldY:=y;
        { set label text }
        With Series1 do
        begin
          GetCursorValues(tmpX,tmpY);  { <-- get values under mouse cursor }
          Label1.Caption:=GetVertAxis.LabelValue(tmpY)+
                          ' '+
                          GetHorizAxis.LabelValue(tmpX);
        end;
      end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    try
      //edit1.Text:=inttostr(strtoint('$'+trim(Edit1.text)));
    except
      halt;
    end;
    end;{ TMypanel }procedure TMypanel.Setcolor;
    begin
    self.Color:=clRed;
    end;{procedure TForm1.Button3Click(Sender: TObject);
    var
      mypanel:Tmypanel;
    begin
      mypanel:=Tmypanel.Create(self);
      mypanel.Parent:=form1;
      mypanel.Setcolor;
    end;    }end.
      

  5.   

    冒昧打扰了啊
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls, TeeProcs, TeEngine, Chart, Series;type
      TForm1 = class(TForm)
        Button1: TButton;
        Chart1: TChart;
        Series1: TLineSeries;
        Label1: TLabel;
        Panel1: TPanel;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        //Procedure chartMouseDown(Sender:Tobject;Button:TMousebutton;shift:TshiftState;X,Y:integer);
        procedure ChartMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
        procedure FormShow(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure Button2Click(Sender: TObject);
        //procedure Button3Click(Sender: TObject);
        //procedure   CMMouseEnter(var Message:   TMessage);   message   CM_MOUSEENTER;
      private
        { Private declarations }  public
        { Public declarations }
         OldX,OldY:Longint;
        CrossHairColor:TColor;
        CrossHairStyle:TPenStyle;
      end; TMypanel=class(Tpanel)
          public
            procedure Setcolor;
          end;var
      Form1: TForm1;
      mypanel1:TMyPanel;
      Fchart:Tform;implementation{$R *.dfm}(*procedure TMyPanel.setcolor;
    begin
        self.color:=clRed;
    end;*)procedure TForm1.Button1Click(Sender: TObject);//主要使这里面var
      //Fchart:Tform;
      mychart:Tchart;
      myseries:Tlineseries;
      njseries:Tlineseries;
      XY_Label:Tlabel;
      i:integer;
      j:integer;
      const scale:single =0.85 ;
    begin
      Fchart:=Tform.Create(self);//******关键一:动态生成了窗体Fchart
      Fchart.Width:=800;
      Fchart.Height:=600;
      Fchart.Position:=poScreenCenter;
      Fchart.Show;  XY_Label:=TLabel.Create(self);
      XY_Label.Parent:=Fchart;
      XY_Label.Width:=100;
      XY_label.Left:=0;
      XY_label.Top:=0;  mychart:=Tchart.Create(self);//******关键二:动态生成了mychart并定义mychart.parent:=Fchart
      mychart.Parent:=Fchart;
      //mychart.OnMouseDown:=chartMouseDown;
      mychart.OnMouseMove:=chartMouseMove;  //*********定义了mychart的mousemove事件  //mychart.on
      with mychart do
      begin  width:=trunc(Fchart.Width * scale);
      Height:=trunc(Fchart.Height * scale);
      left:=trunc((Fchart.Width-width)/2);
      top:=trunc((Fchart.Height-Height)/2);
      Color:=clwhite;
      View3D:=false; //整个面板的颜色设置为白色
      Title.Text.Text:='线';
      Title.Font.Size:=13;
      BottomAxis.Title.Caption:='转角(°)';
      BottomAxis.Title.Font.Size:=13;
      LeftAxis.Title.Caption:='';
      LeftAxis.Title.Font.Color:=clred;
      leftAxis.Title.Font.Size:=13;
      RightAxis.Title.Caption:='';
      RightAxis.Title.Font.Size:=13;
      RightAxis.Title.Font.Color:=clBlue;
      marginleft:=1;
      marginright:=1;
      marginBottom:=1;  end;
      myseries:=Tlineseries.Create(self);//*************实例化了两个数据系列myseries,njseries
      myseries.ParentChart:=mychart;
      njseries:=Tlineseries.Create(self);
      njseries.ParentChart:=mychart;
      mychart.series[1].VertAxis:=aRightAxis;  with myseries do
      begin
         for i:=0 to 10 do
         begin
             addxy(300*i/5.4+i,2.5*i*i,inttostr(i));
         end;
      end;
      with njseries do
      begin
          LinePen.Style:=psDash;
          linepen.Width:=2;
          for j:=5 to 15 do
          begin
              addxy(200*j/4.78,j*j*j+200*j,inttostr(j),clblue);
          end;
      end;
      //固定Tchart中的坐标,不使Tchart中的坐标随Series的变化而变化
      with  myChart.LeftAxis do
      begin
          Automatic:=False;      minimum:=0;
          maximum:=1500;
          increment:=300;
          MinorTickCount:=4;  end;
      with  myChart.RightAxis do
      begin
          Automatic:=False;
          minimum:=0;
          maximum:=3500;
          increment:=700;
          MinorTickCount:=4;
      end;
      (*with  myChart.bottomAxis do
      begin
          //Automatic:=False;
         //setminmax(0,500);
          increment:=50;
          MinorTickCount:=4;
      end;*)
      //mychart.Legend.Alignment:=laLeft;
      mychart.Legend.LegendStyle:=lsSeries;  myseries.Title:='';
      njseries.Title:='';
    end;
    {procedure TForm1.ChartMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
     Var
       chart:Tchart;
    begin
      chart:=Sender as Tchart;
      //showmessage('('+floattostr(chart.Series[0].YScreenToValue(X))+','+floattostr(chart.Series[1].YScreenToValue(Y))+')');
    end; }procedure Tform1.ChartMouseMove(Sender:TObject;Shift:TShiftState;X,Y:integer);//***********关键三:mychart的mousemove事件
    { This procedure draws the crosshair lines }Procedure DrawCross(AX,AY:Integer;Sender:TObject);//定义了一个画十字的过程
    var
      chart:Tchart;
      begin
        chart:=Sender as Tchart;//%%%%%%%%%最重要的地方:我想在自定义的DrawCross中将X、Y值fchart(窗体)中动态创建的空间label1中,我会动态创建label1,
                                     //%%%%%%%%可是不知道在DrawCross中如何引用啊?恳求大侠救助啊
        With Chart.Canvas,chart do
        begin
          Pen.Color:=clYellow;
          Pen.Style:=psSolid;
          Pen.Mode:=pmXor;
          Pen.Width:=1;
          MoveTo(ax,ChartRect.Top-Height3D);
          LineTo(ax,ChartRect.Bottom-Height3D);
          MoveTo(ChartRect.Left+Width3D,ay);
          LineTo(ChartRect.Right+Width3D,ay);
          chart.Title.Text.Text:='='+formatfloat('0.##',chart.Series[0].XScreenToValue(X))+',=' +formatfloat('0.##',chart.Series[0].YScreenToValue(Y))+',='+formatfloat('0.##',chart.Series[1].YscreenToValue(Y));    end;
      end;
    Var
      chart:Tchart;
    begin
      chart:=Sender as Tchart;
      if (OldX<>-1) then
      begin
        DrawCross(OldX,OldY,chart);  { draw old crosshair }
        OldX:=-1;
      end;
      if PtInRect( Chart.ChartRect, Point(X-Chart.Width3D,Y+Chart.Height3D)  ) then
      begin
        DrawCross(x,y,chart);  { draw crosshair at current position }
        { store old position }
        OldX:=x;
        OldY:=y;
      end;
    end;
    procedure TForm1.FormShow(Sender: TObject);
    begin
    //chart1.Cursor:=crCross;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Series1.FillSampleValues(30);  { <-- some random values }
      OldX:=-1;                          { initialize variables }
      CrossHairColor:=clYellow;
      CrossHairStyle:=psSolid;
    end;procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);{ This procedure draws the crosshair lines }
      Procedure DrawCross(AX,AY:Integer);
      begin
        With Chart1,Canvas do
        begin
          Pen.Color:=CrossHairColor;
          Pen.Style:=CrossHairStyle;
          Pen.Mode:=pmXor;
          Pen.Width:=1;
          MoveTo(ax,ChartRect.Top-Height3D);
          LineTo(ax,ChartRect.Bottom-Height3D);
          MoveTo(ChartRect.Left+Width3D,ay);
          LineTo(ChartRect.Right+Width3D,ay);
        end;
      end;Var tmpX,tmpY:Double;
    begin
      if (OldX<>-1) then
      begin
        DrawCross(OldX,OldY);  { draw old crosshair }
        OldX:=-1;
      end;  { check if mouse is inside Chart rectangle }
      if PtInRect( Chart1.ChartRect, Point(X-Chart1.Width3D,Y+Chart1.Height3D)  ) then
      begin
        DrawCross(x,y);  { draw crosshair at current position }
        { store old position }
        OldX:=x;
        OldY:=y;
        { set label text }
        With Series1 do
        begin
          GetCursorValues(tmpX,tmpY);  { <-- get values under mouse cursor }
          Label1.Caption:=GetVertAxis.LabelValue(tmpY)+
                          ' '+
                          GetHorizAxis.LabelValue(tmpX);
        end;
      end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    try
      //edit1.Text:=inttostr(strtoint('$'+trim(Edit1.text)));
    except
      halt;
    end;
    end;{ TMypanel }procedure TMypanel.Setcolor;
    begin
    self.Color:=clRed;
    end;{procedure TForm1.Button3Click(Sender: TObject);
    var
      mypanel:Tmypanel;
    begin
      mypanel:=Tmypanel.Create(self);
      mypanel.Parent:=form1;
      mypanel.Setcolor;
    end;    }end.
      

  6.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, TeeProcs, TeEngine, Chart, StdCtrls;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        lab:TLabel;
        chart:TChart;
        procedure ChartMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ChartMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      lab.Caption := IntToStr(X)+':'+IntToStr(Y);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      lab := TLabel.Create(Self);
      chart := TChart.Create(Self);
      lab.Parent := Self;
      chart.Parent := Self;
      lab.Left := 0;
      lab.Top := 0;
      lab.Caption := 'aaaaaa';
      chart.Left := 100;
      chart.Top := 200;
      chart.OnMouseMove := Self.ChartMouseMove;
    end;
    end.
      

  7.   

    希望对你有帮助!unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        GroupBox1: TGroupBox;
        RadioButton1: TRadioButton;
        RadioButton2: TRadioButton;
        procedure RadioButton1Click(Sender: TObject);
        procedure RadioButton2Click(Sender: TObject);
        procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure FormCreate(Sender: TObject);
      private
      classkind: tcontrolclass; 
        cpnum: integer;
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.RadioButton1Click(Sender: TObject);
    begin
      classkind:=Tedit;
    end;procedure TForm1.RadioButton2Click(Sender: TObject);
    begin
      classkind:= tbutton;
    end;procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var 
    newcp:tcontrol; 
    cpname:string; 
    begin
      newcp:=classkind.Create(self);
      newcp.visible:=false; 
      newcp.Parent:=self;
      newcp.left:=x;
      newcp.top:=y;
      inc(cpnum);
      cpname:=classkind.classname+inttostr(cpnum);
      delete(cpname,1,1);
      newcp.name:=cpname;
      newcp.visible:=true;end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      classkind:=tbutton;
      cpnum:=900;
    end;end.