在其他应用程序窗口上创建Button

时间:2011-06-18 06:24:56

标签: delphi cross-application

我在下面编写代码来在Calculator上创建一个额外的按钮,但按钮不会显示:

var
  Object1 : TButton ;
  Hand: THandle;
begin
   Hand:= FindWindow('CalcFrame', 'Calculator');
   Object1 := TButton.CreateParented(Hand);
   Object1.Show ;
end;

在使用EnumChildWindow API函数运行上面的代码后,我得到了计算器上的控件,并看到EnumChildWindow返回的控制列表中创建的按钮,但为什么创建的按钮没有显示?

我记得我在Windows XP上使用这个代码并且它没有问题但现在在Windows 7中没有出现创建的按钮。

3 个答案:

答案 0 :(得分:4)

Win7中的

计算器和Paint是 使用.NET和WPF重建,以及 没有办法“联系” .NET代码通过本机代码 特别是使用不同的WPF 绘制控件的方法。

编辑: 使您的代码适用于本机 你可以使用这样的代码:

hand := FindWindow('TForm1','Form1');
object1 := TButton.Create(self);
object1.ParentWindow := hand;

答案 1 :(得分:0)

@Ali:我说代码不适用于任何APP,例如我在delphi中创建一个表单并在一个按钮中写下代码:

var
  Hand: THandle;
  Object1: TButton;
begin
  Hand:= FindWindow('TForm1', 'Form1');
  if Hand <> 0 then
  begin
    Object1:= TButton.CreateParented(Hand);
    Object1.Caption:= 'Test';
    Object1.Show;
  end;
end;

代码找到APP&amp;的句柄大多数创建一个按钮,它在Windows XP中工作,但在Windows 7按钮创建,但不要显示!

答案 2 :(得分:0)

你必须public class ReportIncident extends Activity { Button bb; double mylat; double mylong; String myplace; double myalt; EditText editText; private Context context; //private Context mContext; // flag for GPS status boolean isGPSEnabled = false; // flag for GPS status boolean canGetLocation = false; private myGPSTracker gpstracker; Location location; // location double latitude; // latitude double longitude; // longitude double altitude; // The minimum distance to change Updates in meters private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute // Declaring a Location Manager protected LocationManager locationManager; private static final int ACCESS_FINE_LOCATION = 200; //private Activity activity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_incident); context=getApplicationContext(); gpstracker=new myGPSTracker(); editText=(EditText)findViewById(R.id.txtReportIncident); bb=(Button)findViewById(R.id.btnReport); bb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String mydescription=editText.getText().toString().trim(); if(TextUtils.isEmpty(mydescription)) { editText.setError("Field can not be blank"); return; } Incidents incidents =new Incidents(); incidents.setId(new Date().getTime()); incidents.setIncidentdate(new Date().getTime()); incidents.setPlaceName("Some Thing"); incidents.setAltitude(gpstracker.getAltitude()); incidents.setMyLat(gpstracker.getLatitude()); incidents.setMyLongtitude(gpstracker.getLongitude()); incidents.setIncidentDescription(mydescription); new AddIncidentAsyncTask(getApplicationContext()).execute(incidents); editText.setText(""); } }); }

Visible:= False