線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1880
推到 Plurk!
推到 Facebook!

使用多執行緒處理TShape元件改變Top及left的值

答題得分者是:Coffee
esp_pzj
初階會員


發表:32
回覆:70
積分:40
註冊:2007-02-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-01-09 09:51:42 IP:59.120.xxx.xxx 訂閱
請教各位前輩:
開發環境:Delphi 6

程式的目的是讓使用者,使用滑鼠按住 TShape元件 移動 TShape元件會跟著移動,同時要處理25個TShape元件移動,25個TShape元件不用執行緒處理時,畫面會有延遲現像。

我改用多執行緒來處理 TShape元件
呼叫多執行緒時(moveSnXY1.Create(s1);)出現錯誤。
錯誤資訊:「 There is no overloaded version of'Create' that can be called with these arguments」。

請教前輩這個錯誤資訊要如何解決,或者我的處理關念不正確,
還是有更好的處理方式,請各位前輩指導
感謝

呼叫執行緒程式:
[code delphi]

public //宣告全域變數
mousedownX:Integer; //取得滑鼠點一下的X值
mousedownY:Integer; //取得滑鼠點一下的Y值
MoveTop:Integer; //取得滑鼠Top移動的距離
MoveLeft:Integer; //取得滑鼠Left移動的距離

procedure TForm1.S1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
mousedownX:=X;//取得滑鼠點一下的X值
mousedownY:=Y;//取得滑鼠點一下的Y值
end


procedure TForm1.S1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin

MoveTop:=Y-mousedownY;//取得滑鼠Top移動的距離
MoveLeft:=X-mousedownX;//取得滑鼠Left移動的距離

moveSnXY1.Create(s1);//呼到
end;

[/code]




以下是我執行緒的程式:

[code delphi]
unit moveSnXY;
interface
uses
Classes,QExtCtrls,QForms;
type
moveSnXY1 = class(TThread)
private
Sn:TShape;
{ Private declarations }
protected
procedure Execute;override;
public
constructor Create(varSn:TShape);overload; '建構式
end;
implementation
uses Cut, Controls, QControls;

{ moveSnXY }
constructor moveSnXY1.Create(varSn:TShape);
var MoveTop,MoveLeft:Integer;
begin
inherited Create(False);
Sn:=varSn;
FreeOnTerminate:=True;
end;

procedure moveSnXY1.Execute;
var MoveTop,MoveLeft:Integer;
begin
MoveTop:=CutForm.MoveTop; //取移動的top
MoveLeft:=CutForm.MoveLeft; //取移動的left
sn.Top:=sn.Top MoveTop;
sn.Top:=sn.Top MoveLeft;
end;

end.
[/code]
------
學藝不精 謝多多指教
Coffee
版主


發表:31
回覆:878
積分:561
註冊:2006-11-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-01-16 14:56:02 IP:220.130.xxx.xxx 訂閱
以下是我的sample code,純粹是繼承一個thread去控制單一的TShape,沒有你提到的問題。
不過如果你要用25個thread去控制25個shape,請考慮VCL component是non thread-safe。



[code delphi]
unit Unit1;

interface

uses
Forms,
ExtCtrls, StdCtrls, Controls, Classes;

type TCntlThread= class(TThread)
private
FShape : TShape;
public
constructor Create(varShape : TShape);overload;
procedure Execute;override;
end;

type
TForm1 = class(TForm)
Shape1: TShape;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation


constructor TCntlThread.Create(varShape : TShape);
begin
inherited Create(false);
FShape:=varShape;
FreeOnTerminate:=true;
end;

procedure TCntlThread.Execute;
begin
with FShape do
begin
Top:=Top 20;
Left:=Left 20;
end;
end;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
AThread : TCntlThread;
begin
AThread:=TCntlThread.Create(Shape1);
end;

end.
[/code]
------
不論是否我發的文,在能力範圍皆很樂意為大家回答問題。
為了補我的能力不足之處,以及讓答案可以被重複的使用,希望大家能儘量以公開的方式問問題。
在引述到我的文時自然會儘量替各位想辦法,謝謝大家!
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-01-16 18:09:25 IP:61.64.xxx.xxx 訂閱
what is s1 ? Is it's type correct ?

I don't think it is a good ideal to solve your problem in this way. It will slow in any way, may be only accetable fast at your computer, and under some circumstances the result may not be what you want. You should try DirectX or OpenGL when you have so many items to draw. If your do not want to go by DirectX, then do by canva and draw by yourself, not much work but faster a lot then do it by TShape s.

===================引 用 esp_pzj 文 章===================
請教各位前輩:
開發環境:Delphi 6

程式的目的是讓使用者,使用滑鼠按住 TShape元件 移動 TShape元件會跟著移動,同時要處理25個TShape元件移動,25個TShape元件不用執行緒處理時,畫面會有延遲現像。

我改用多執行緒來處理 TShape元件
呼叫多執行緒時(moveSnXY1.Create(s1);)出現錯誤。
錯誤資訊:「 There is no overloaded version of'Create' that can be called with these arguments」。

請教前輩這個錯誤資訊要如何解決,或者我的處理關念不正確,
還是有更好的處理方式,請各位前輩指導
感謝

呼叫執行緒程式:
[code delphi]

public //宣告全域變數
mousedownX:Integer; //取得滑鼠點一下的X值
mousedownY:Integer; //取得滑鼠點一下的Y值
MoveTop:Integer; //取得滑鼠Top移動的距離
MoveLeft:Integer; //取得滑鼠Left移動的距離

procedure TForm1.S1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
mousedownX:=X;//取得滑鼠點一下的X值
mousedownY:=Y;//取得滑鼠點一下的Y值
end


procedure TForm1.S1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin

MoveTop:=Y-mousedownY;//取得滑鼠Top移動的距離
MoveLeft:=X-mousedownX;//取得滑鼠Left移動的距離

moveSnXY1.Create(s1);//呼到
end;

[/code]




以下是我執行緒的程式:

[code delphi]
unit moveSnXY;
interface
uses
Classes,QExtCtrls,QForms;
type
moveSnXY1 = class(TThread)
private
Sn:TShape;
{ Private declarations }
protected
procedure Execute;override;
public
constructor Create(varSn:TShape);overload; '建構式
end;
implementation
uses Cut, Controls, QControls;

{ moveSnXY }
constructor moveSnXY1.Create(varSn:TShape);
var MoveTop,MoveLeft:Integer;
begin
inherited Create(False);
Sn:=varSn;
FreeOnTerminate:=True;
end;

procedure moveSnXY1.Execute;
var MoveTop,MoveLeft:Integer;
begin
MoveTop:=CutForm.MoveTop; //取移動的top
MoveLeft:=CutForm.MoveLeft; //取移動的left
sn.Top:=sn.Top MoveTop;
sn.Top:=sn.Top MoveLeft;
end;

end.
[/code]
esp_pzj
初階會員


發表:32
回覆:70
積分:40
註冊:2007-02-09

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-01-17 14:12:16 IP:59.120.xxx.xxx 訂閱
感謝 Coffee 前輩:

我參考你寫的方式完成了我的需求。
再度感謝
Coffee 前輩您熱心的答覆,
讓我們這些新手更有努力學習 delphi 的動力


也感謝
syntax 前輩的建議
------
學藝不精 謝多多指教
系統時間:2024-04-30 15:21:48
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!