全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:867
推到 Plurk!
推到 Facebook!

InputBox 無法在 Thread 中使用?

答題得分者是:Zard
hornacek
一般會員


發表:29
回覆:76
積分:21
註冊:2004-02-02

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-11-10 18:40:49 IP:220.135.xxx.xxx 未訂閱
在thread中加入InputBox,會出現發生問題,必須關閉的訊息? 請問為什麼呢?有什麼解決的辦法?感激不盡! ><
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-11-10 22:58:58 IP:61.64.xxx.xxx 未訂閱
我試的結果是當InputBox放在Thread內時, 不知道為什麼, 他的Canvas卻尚未ready, 所以只好等Canvas ready後, 再繼續下去, 我把InputBox的碼挖出來, 修改一下就可以用了. 為了不和原版InputBox衝到, 我把它改名為ZInputBox.    把下一篇的InputBox.pas加入你的BCB Project內, 記得include InputBox.hpp    我在Win2000下用BCB5試沒問題, 如果在遇到問題, 建議你直接自已做一個InputBox, 其實實作一個InputBox是相當簡單的.    測試程式如下:
//---------------------------------------------------------------------------    #include 
#include 
#pragma hdrstop    #include "Unit1.h"    #include "InputBox.hpp"    //---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
DWORD WINAPI ThreadProc(void* pParam)
{
  while(TRUE)
  {
    AnsiString szTest;
    szTest = ZInputBox("Test", "Enter a string", "");
    // 若輸入Stop, 則Thread結束
    if (szTest == "Stop")
      break;
    else
      ShowMessage(szTest);
  }      return 0;
}    void __fastcall TForm1::Button1Click(TObject *Sender)
{
  DWORD dwThreadID;
  CreateThread(NULL, 0, ThreadProc, NULL, 0, &dwThreadID);
}
//---------------------------------------------------------------------------    
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-11-10 23:04:47 IP:61.64.xxx.xxx 未訂閱
InputBox.pas上半部
unit InputBox;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;    Const
  BTN_OK      = '確定';
  BTN_CANCEL  = '取消';    function ZInputBox(const ACaption, APrompt, ADefault: string): string;    implementation    function ZGetAveCharSize(Canvas: TCanvas): TPoint;
var
  I: Integer;
  Buffer: array[0..51] of Char;
  bOK: Boolean;
  
begin
  for I := 0 to 25 do Buffer[I] := Chr(I   Ord('A'));
  for I := 0 to 25 do Buffer[I   26] := Chr(I   Ord('a'));      bOK := FALSE;
  while(Not bOK) do
  begin
    try
      GetTextExtentPoint(Canvas.Handle, Buffer, 52, TSize(Result));
      bOK := TRUE;
    except
    end;
  end;      Result.X := Result.X div 52;
end;
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-11-10 23:06:20 IP:61.64.xxx.xxx 未訂閱
InputBox.pas下半部
function ZInputQuery(const ACaption, APrompt: string;
  var Value: string): Boolean;
var
  Form: TForm;
  Prompt: TLabel;
  Edit: TEdit;
  DialogUnits: TPoint;
  ButtonTop, ButtonWidth, ButtonHeight: Integer;
  
begin
  Result := False;
  Form := TForm.Create(Application);
  with Form do
    try
      Canvas.Font := Font;
      BorderStyle := bsDialog;
      DialogUnits := ZGetAveCharSize(Canvas);
      Caption := ACaption;
      ClientWidth := MulDiv(180, DialogUnits.X, 4);
      ClientHeight := MulDiv(63, DialogUnits.Y, 8);
      Position := poScreenCenter;
      Prompt := TLabel.Create(Form);
      with Prompt do
      begin
        Parent := Form;
        AutoSize := True;
        Left := MulDiv(8, DialogUnits.X, 4);
        Top := MulDiv(8, DialogUnits.Y, 8);
        Caption := APrompt;
      end;
      Edit := TEdit.Create(Form);
      with Edit do
      begin
        Parent := Form;
        Left := Prompt.Left;
        Top := MulDiv(19, DialogUnits.Y, 8);
        Width := MulDiv(164, DialogUnits.X, 4);
        MaxLength := 255;
        Text := Value;
        SelectAll;
      end;
      ButtonTop := MulDiv(41, DialogUnits.Y, 8);
      ButtonWidth := MulDiv(50, DialogUnits.X, 4);
      ButtonHeight := MulDiv(14, DialogUnits.Y, 8);
      with TButton.Create(Form) do
      begin
        Parent := Form;
        Caption := BTN_OK;
        ModalResult := mrOk;
        Default := True;
        SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth,
          ButtonHeight);
      end;
      with TButton.Create(Form) do
      begin
        Parent := Form;
        Caption := BTN_CANCEL;
        ModalResult := mrCancel;
        Cancel := True;
        SetBounds(MulDiv(92, DialogUnits.X, 4), ButtonTop, ButtonWidth,
          ButtonHeight);
      end;
      if ShowModal = mrOk then
      begin
        Value := Edit.Text;
        Result := True;
      end;
    finally
      Form.Free;
    end;
end;    function ZInputBox(const ACaption, APrompt, ADefault: string): string;
begin
  Result := ADefault;
  ZInputQuery(ACaption, APrompt, Result);
end;    end.
hornacek
一般會員


發表:29
回覆:76
積分:21
註冊:2004-02-02

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-11-10 23:18:02 IP:220.139.xxx.xxx 未訂閱
原來是InputBox的問題~~~ 謝啦!來試用你的方法囉! ^^
系統時間:2024-06-01 22:51:05
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!