void __fastcall TForm1::Button1Click(TObject *Sender)
{
ListBox2->Items->>Add(ListBox1->Items->Strings[ListBox1->ItemIndex]);
}
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox2.Items.Add(ListBox1.Items[ListBox1.ItemIndex]);
end;
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "cgauges.h"
#include "Thread.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE 管理のコンポーネント
TButton *Button1;
TButton *Button2;
TCGauge *CGauge1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // ユーザー宣言
Th *ThObj; // スレッド変数
bool bThRun; // スレッド起動中フラグ true:起動中
void __fastcall ThEnd(TObject *Sender); // OnTerminateイベントハンドラ
public: // ユーザー宣言
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cgauges"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ThObj = new Th(false);
ThObj->FreeOnTerminate = true;
ThObj->OnTerminate = ThEnd; // OnTerminateイベントハンドラの設定
bThRun = true;
}
//---------------------------------------------------------------------------
// OnTerminateのイベントハンドラ
void __fastcall TForm1::ThEnd(TObject *Sender)
{
if (Sender == ThObj) bThRun = false; // スレッドが終了
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
ThObj->Terminate(); // スレッドの停止
}
//---------------------------------------------------------------------------
// [一時停止]ボタン
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ThObj->Suspend(); // スレッドの一時停止
}
//---------------------------------------------------------------------------
// [再スタート]ボタン
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ThObj->Resume(); // スレッドの再開始
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef ThreadH
#define ThreadH
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class Th : public TThread
{
private:
int iProgress;
void __fastcall _fnUpdateGauge(void); // Synchronizeで呼ばれる
protected:
void __fastcall Execute();
public:
__fastcall Th(bool CreateSuspended);
};
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Thread.h"
#include "Unit1.h" // Formに張り付けたコンポーネントを呼ぶ為
#pragma package(smart_init)
//---------------------------------------------------------------------------
// 注意: VCL オブジェクトのメソッドとプロパティを使用するには, Synchronize
// を使ったメソッド呼び出しでなければなりません。次に例を示します。
//
// Synchronize(UpdateCaption);
//
// ここで, UpdateCaption は次のように記述できます。
//
// void __fastcall Th::UpdateCaption()
// {
// Form1->Caption = "スレッドから書き換えました";
// }
//---------------------------------------------------------------------------
__fastcall Th::Th(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
// フォームに張り付けたGaugeにアクセスする
void __fastcall Th::_fnUpdateGauge(void)
{
Form1->CGauge1->Progress = iProgress;
}
//---------------------------------------------------------------------------
void __fastcall Th::Execute()
{
//---- スレッドのコードをここに記述 ----
iProgress = 0;
while (true) {
if (Terminated) break; // スレッドの終了判断
Synchronize(_fnUpdateGauge);
if (iProgress >= 100) iProgress = 0;
else iProgress++;
// Sleep(1);
}
}
//---------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Thread, StdCtrls, ComCtrls, Gauges;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Gauge1: TGauge;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private 宣言 }
ThObj: Th; // スレッド変数
bThRun: boolean; // スレッド起動中フラグ true:起動中
procedure ThEnd(Sender: TObject); // OnTerminateイベントハンドラ
public
{ Public 宣言 }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
ThObj := Th.Create(false);
ThObj.OnTerminate := ThEnd; // OnTerminateイベントハンドラの設定
bThRun := true;
end;
//OnTerminateのイベントハンドラ
procedure TForm1.ThEnd(Sender: TObject);
begin
if Sender = ThObj then bThRun := false; // スレッドが終了
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ThObj.Terminate; // スレッドの停止
end;
// [一時停止]ボタン
procedure TForm1.Button1Click(Sender: TObject);
begin
ThObj.Suspend; // スレッドの一時停止
end;
// [再スタート]ボタン
procedure TForm1.Button2Click(Sender: TObject);
begin
ThObj.Resume; // スレッドの再開始
end;
end.
unit Thread;
interface
uses
Classes,
Windows; // sleepを使用する為
type
Th = class(TThread)
private
{ Private 宣言 }
iProgress: integer;
procedure _fnUpdateGauge; // Synchronizeで呼ばれる
protected
procedure Execute; override;
public
constructor Create(mode: boolean);
end;
implementation
uses Unit1; // Formに張り付けたコンポーネントを呼ぶ為
{注意:
メインスレッドが所有する VCL のメソッド/関数/プロパティを
扱うには、このクラスに他のオブジェクトを参照するための
メソッドを追加し,Synchronize メソッドの引数として渡す必要が
あります。
たとえば,UpdateCaption メソッドを以下のように定義し,
procedure Th.UpdateCaption;
begin
Form1.Caption := 'スレッドから書き換えました';
end;
Execute メソッドの中で Synchronize メソッドに渡します。
Synchronize(UpdateCaption);
}
{ Th }
//
constructor Th.Create(mode: boolean);
begin
inherited Create(mode);
end;
// フォームに張り付けたGaugeにアクセスする
procedure Th._fnUpdateGauge;
begin
Form1.Gauge1.Progress := iProgress;
end;
//
procedure Th.Execute;
begin
{ スレッドとして実行したいコードをここに記述 }
iProgress := 0;
while true do begin
if Terminated then exit; // スレッドの終了判断
Synchronize(_fnUpdateGauge);
if iProgress >= 100 then iProgress := 0
else inc(iProgress);
// sleep(1);
end;
end;
end.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TDateTime tDate = Now();
unsigned short year, month, day;
unsigned short hour, min, sec, msec;
tDate.DecodeDate(&year, &month, &day);
Label1->text =
IntToStr(year)+"-"+
IntToStr(month)+":"+
IntToStr(day);
tDate.DecodeTime(&hour, &min, &sec, &msec);
Label2->Text =
IntToStr(hour)+":"+
IntToStr(min)+":"+
IntToStr(sec)+":"+
IntToStr(msec);
}
procedure TForm1.Button1Click(Sender: TObject);
var
Year, Month, Day, Hour, Min, Sec, MSec: Word;
tDate: TDateTime;
begin
tDate := Now;
DecodeDate(tDate,Year,Month,Day);
Label1.Text :=
IntToStr(Year)+'-'+
IntToStr(Month)+'-'+
IntToStr(Day);
DecodeTime(tDate,Hour,Min,Sec,MSec);
Label2.Text :=
IntToStr(Hour)+':'+
IntToStr(Min)+':'+
IntToStr(Sec)+':'+
IntToStr(MSec);
end;
void __fastcall TForm1::StringGrid1KeyPress(TObject *Sender, char &Key)
{
if ( Key == VK_RETURN && StringGrid1->EditorMode) {
if (StringGrid1->Col < StringGrid1->ColCount - 1)
StringGrid1->Col = StringGrid1->Col + 1;
else {
if (StringGrid1->Row < StringGrid1->RowCount - 1)
StringGrid1->Row = StringGrid1->Row + 1;
else
StringGrid1->Row = StringGrid1->FixedRows;
StringGrid1->Col = StringGrid1->FixedCols;
}
}
}
void __fastcall TForm1::_fnDrawCell(TObject *Sender, long Col, long Row,TRect &Rect, TGridDrawState State)
{
RECT r = RECT(Rect);
// 固定セルの背景色をセット
if (State.Contains(gdFixed))
sg->Canvas->Brush->Color= sg->FixedColor;
//フォーカスのあるセルの背景色をセット
else if (State.Contains(gdFocused))
sg->Canvas->Brush->Color= sg->Options.Contains(goDrawFocusSelected)? clHighlight: sg->Color;
//選択されているセルの背景色をセット
else if (State.Contains(gdSelected))
sg->Canvas->Brush->Color= clHighlight;
//普通のセルの背景色をセット
else
sg->Canvas->Brush->Color= sg->Color;
//背景色で消去
sg->Canvas->FillRect(Rect);
//固定セルの立体枠を描く
if (sg->Ctl3D&&State.Contains(gdFixed))
DrawEdge(sg->Canvas->Handle,&r,BDR_RAISEDINNER,BF_RECT);
//フォーカスのあるセルの文字色をセット
if (State.Contains(gdFocused)) {
sg->Canvas->Font->Color = sg->Options.Contains(goDrawFocusSelected)? clHighlightText: sg->Font->Color;
//フォーカス枠を描く
DrawFocusRect(sg->Canvas->Handle,&r);
}
//選択されているセルの文字色をセット
else if (State.Contains(gdSelected))
sg->Canvas->Font->Color = clHighlightText;
//普通のセルの文字色をセット
else
sg->Canvas->Font->Color= sg->Font->Color;
//テキスト表示領域の設定
InflateRect(&r,-2,-2);
//テキストの表示
DrawText(sg->Canvas->Handle,sg->Cells[Col][Row].c_str(),-1,&r,DT_RIGHT); //右寄せ
// DrawText(sg->Canvas->Handle,sg->Cells[Col][Row].c_str(),-1,&r,DT_CENTER); //中央寄せ(水平)
// DrawText(sg->Canvas->Handle,sg->Cells[Col][Row].c_str(),-1,&r,DT_VCENTER|DT_SINGLELINE); //中央寄せ(垂直)
// DrawText(sg->Canvas->Handle,sg->Cells[Col][Row].c_str(),-1,&r,DT_BOTTOM|DT_SINGLELINE); //下寄せ
// DrawText(sg->Canvas->Handle,sg->Cells[Col][Row].c_str(),-1,&r,DT_CENTER|DT_VCENTER|DT_SINGLELINE); //水平 ・垂直とも中央寄せ
// DrawText(sg->Canvas->Handle,sg->Cells[Col][Row].c_str(),-1,&r,DT_WORDBREAK); //複数行表示
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
_fnGridSort(StringGrid1, 1); //1列目をキーにソートする
}
// StringGridのソート
void __fastcall TForm1::_fnGridSort(TStringGrid *p, //StringGrid
long col, //ソートのキーとなる列
bool desc = false) //false:昇順, true:降順
{
if ((0 <= col) && (p->ColCount <= col)) ; //範囲外の指定
else return;
for (int i=p->FixedRows ; i<p->RowCount-1 ; i++) {
for (int j=i+1 ; j<p->RowCount ; j++) {
bool b;
// 数値として比べてみてだめだったら文字で比べる(8と10などを文字で比べたときに逆転するのを防ぐ)
try {
b = p->Cells[col][i].ToDouble() > p->Cells[col][j].ToDouble();
} catch(...) {
b = p->Cells[col][i] > p->Cells[col][j];
}
// 行の入れ替え
if (desc? !b: b) {
TStringList *sl=new TStringList;
sl->Assign(sg->Rows[i]);
p->Rows[i] = p->Rows[j];
p->Rows[j]= sl;
sl->Free();
}
}
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for (int i=0 ; i<10000 ; i++) {
Label1->Caption = IntToStr(i); // カウンタ表示の更新
Application->ProcessMessages(); // 溜まっているイベントの処理
}
}
Application->Initialize(); SplashWindow=new TSplashWindow(Application); SplashWindow->Position=poScreenCenter; SplashWindow->Show(); Application->ProcessMessages(); Application->CreateForm(__classid(TForm1), &Form1); SplashWindow->Hide(); SplashWindow->Free(); Application->Run();
#include <IniFiles.hpp>
AnsiString FileName;
FileName = ExtractFilePath(Application->ExeName)+"MyApp.ini";
TIniFile *ini_file = new TIniFile(szFileName);
ini_file->WriteInteger("Position", "xpos_pas", fPassMaker->Top);
ini_file->WriteInteger("Position", "ypos_pas", fPassMaker->Left);
ini_file->WriteBool("Check", "check_status", Form1->CheckBox1->Checked);
delete ini_file;
#include <IniFiles.hpp>
AnsiString FileName;
FileName = ExtractFilePath(Application->ExeName)+"MyApp.ini";
TIniFile *ini_file = new TIniFile(szFileName);
Form1->Top = ini_file->ReadInteger("Position", "xpos_pas", 0);
Form1->Left = ini_file->ReadInteger("Position", "ypos_pas", 0);
Form1->CheckBox1->Checked = ini_file->ReadBool("Check", "check_status", true);
delete ini_file;
#include <Registry.hpp> //TRegistoryを使うときに必要
AnsiString GetReg(AnsiString Key, AnsiString Name, HKEY RootKey)
{
AnsiString s;
TRegistry *Reg = new TRegistry();
Reg->RootKey = RootKey;
if (Reg->OpenKey(Key,false)) {
if (Reg->ValueExists(Name))
s = Reg->ReadString(Name);
Reg->CloseKey();
}
Reg->Free();
return Value;
}
//---------------------------------------------------------------------------
// Unit1.h
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE 管理のコンポーネント
TMainMenu *MainMenu1;
TButton *ButtonAdd;
TButton *ButtonDelete;
TButton *ButtonItemAdd;
TButton *ButtonItemDelete;
void __fastcall ButtonAddClick(TObject *Sender);
void __fastcall ButtonDeleteClick(TObject *Sender);
void __fastcall ButtonItemAddClick(TObject *Sender);
void __fastcall ButtonItemDeleteClick(TObject *Sender);
private:
void __fastcall UserMenuClick(TObject * Sender); // ユーザー宣言
public: // ユーザー宣言
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
// Unit1.cpp
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::UserMenuClick(TObject * Sender)
{
Application->MessageBox(((TMenuItem*)Sender)->Caption.c_str(),"AddMenu",MB_OK);
}
//---------------------------------------------------------------------------
//メニューの追加
void __fastcall TForm1::ButtonAddClick(TObject *Sender)
{
TMenuItem *new_item = new TMenuItem(MainMenu1);
new_item->Caption = "新メニュー"+IntToStr(MainMenu1->Items->Count);
new_item->OnClick = UserMenuClick;
MainMenu1->Items->Insert(MainMenu1->Items->Count, new_item);//最後のメニューに追加
}
//---------------------------------------------------------------------------
//メニューの削除
void __fastcall TForm1::ButtonDeleteClick(TObject *Sender)
{
if (MainMenu1->Items->Count > 0) {
MainMenu1->Items->Delete(MainMenu1->Items->Count-1); //最後のメニューを削除
}
}
//---------------------------------------------------------------------------
//メニューの項目の追加
void __fastcall TForm1::ButtonItemAddClick(TObject *Sender)
{
TMenuItem *to_item;
TMenuItem *new_item;
if (MainMenu1->Items->Count > 0) {
to_item = MainMenu1->Items->Items[MainMenu1->Items->Count-1];
new_item = new TMenuItem(to_item);
new_item->Caption = "新メニュー項目"+IntToStr(to_item->Count);
new_item->OnClick = UserMenuClick;
to_item->OnClick = NULL;
to_item->Insert(to_item->Count, new_item); //最後のメニュー項目に追加
}
}
//---------------------------------------------------------------------------
//メニュー項目の削除
void __fastcall TForm1::ButtonItemDeleteClick(TObject *Sender)
{
TMenuItem *to_item;
if (MainMenu1->Items->Count > 0) {
to_item = MainMenu1->Items->Items[MainMenu1->Items->Count-1];
if (to_item->Count > 0) {
to_item->Delete(to_item->Count-1); //最後のメニュー項目を削除
}
}
}
// フォントのピクセル単位を格納する構造体
struct FontSize
{
long x;
long y;
} FontPixel;
// タブ幅のサイズを計算
FontPixel.x = (GetDialogBaseUnits()&0xffff)/2*4;
// メッセージを送る
SendMessage(Memo1->Handle, EM_SETTABSTOPS, 1, long(&FontPixel.x));
// メッセージを送る SendMessage(Memo1->Handle, EM_UNDO, 0, 0); その他に、Performメソッドを利用することもできます。 Memo1->Perform(EM_UNDO, 0, 0)
// 最大値の設定 Memo1->MaxLength = 80;
このページ直接たどり着いた方は、Topページに戻ってください。
by kenji-b
Copyright (C) 1998-2001 kenji-b homepage All Rights Reserved.