首先用 BCB 開一個 vcl 專案,詳細請參考前面文章
BCB6 視窗程式入門
http://kowala21.blogspot.tw/2013/01/bcb6.html
接著新增兩個按鈕,及一個 TImage 物件
把 TImage 物件屬性改成
Height 480
Width 640
這就是我們的工作區,但它現在是空的,我們還要新增TBitmap 物件,並指給它。
我們先新增個 mylib.h ,這樣會使程式看起來較簡潔。
在專案右鍵,然後新增個 h檔,再改檔名為 mylib.h
把它引入, #include "mylib.h"
我們可以新增個圖紙物件 TBitmap,並指給TImage 物件,並為它設定屬性,指定底色。
看起來就像這樣。
這樣看起來很單調,為它加一些其他東西, 我希望能在 mylib.h 中完成,所以必須寫個函式,把 TBitmap 物件傳進去。
然後我們可以在 mylib.h 中為所欲為,我為它加了框,畫了X,順便寫幾個字。
mylib.h
code如下
void initSCR(Graphics::TBitmap *buf){mainfrm.cpp
int w,h,cx,cy,left, top, right, bottom;
//先在中間畫個框 160*120 TRect(left, top, right, bottom)
buf->Canvas->Pen->Color = clGreen;//綠色
buf->Canvas->Brush->Style = bsClear;//框中間透明 Rectangle
h=buf->Height;//480 工作區寬度
w=buf->Width;//640 工作區高度
cx=w/2; cy=h/2; //320*240 中心點
left=cx-(160/2);//240
top=cy-(120/2);//180
right=cx+(160/2);//400
bottom=cy+(120/2);//300
buf->Canvas->Rectangle(TRect(left,top,right,bottom));//開始畫
//中間畫 X,用描點的畫
buf->Canvas->Pixels[cx-3][cy-3] = clWhite;
buf->Canvas->Pixels[cx-2][cy-2] = clWhite;
buf->Canvas->Pixels[cx-1][cy-1] = clWhite;
buf->Canvas->Pixels[cx][cy] = clWhite;//center point (320,240)
buf->Canvas->Pixels[cx+1][cy+1] = clWhite;
buf->Canvas->Pixels[cx+2][cy+2] = clWhite;
buf->Canvas->Pixels[cx+3][cy+3] = clWhite;
buf->Canvas->Pixels[cx-3][cy+3] = clWhite;
buf->Canvas->Pixels[cx-2][cy+2] = clWhite;
buf->Canvas->Pixels[cx-1][cy+1] = clWhite;
buf->Canvas->Pixels[cx][cy] = clWhite;
buf->Canvas->Pixels[cx+1][cy-1] = clWhite;
buf->Canvas->Pixels[cx+2][cy-2] = clWhite;
buf->Canvas->Pixels[cx+3][cy-3] = clWhite;
//再加個字
buf->Canvas->Brush->Color = clGreen;//background color
buf->Canvas->Font->Color = clWhite;//font color
buf->Canvas->Font->Size=10;
buf->Canvas->TextOutA(left+30,top+20,"再加個字");
buf->Canvas->Brush->Color = clBlack;//background color
buf->Canvas->Font->Color = clYellow;//font color
buf->Canvas->TextOutA(left+50,top+80,"不要底色");
}
code 如下
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "mainfrm.h"
#include "mylib.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
Graphics::TBitmap *myLab;//宣告圖紙,當工作區
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
myLab = new Graphics::TBitmap();//開一塊圖紙,當工作區
myLab->PixelFormat = pf24bit; //bmp
myLab->Height = 480;//圖紙 h
myLab->Width = 640;//圖紙 w
//設定底色
myLab->Canvas->Brush->Color = clBlack;//background = clBlack
myLab->Canvas->FloodFill(0,0,clBlack,fsBorder);//fsSurface,fsBorder
//把物件傳進去
initSCR(myLab);
//指給Image1物件
Form1->Image1->Picture->Bitmap=myLab;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
this->Close();
}
//---------------------------------------------------------------------------
這樣就是一個簡單實驗室了
沒有留言:
張貼留言
請提供您的寶貴意見 ;-)