2013-03-07 61 views
2

的一部分,它不能是很難得出唯一指定的位圖的形式,但我不明白爲什麼代碼無法正常工作(我看到了它在一些Delphi例子):繪製位圖C++ Builder的

Graphics::TBitmap* bmp; 

void __fastcall TForm1::FormCreate(TObject* Sender) 
{ 
    bmp = new Graphics::TBitmap(); 
    bmp->Width = 300; 
    bmp->Height = 300; 
    bmp->Canvas->Ellipse(0, 0, 300, 300); 
} 

void __fastcall TForm1::Button1Click(TObject* Sender) 
{ 
    HRGN rgn = CreateRectRgn(10, 10, 30, 30); 
    if(SelectClipRgn(bmp->Handle, rgn) == ERROR) ShowMessage("Error"); 
    Canvas->Draw(0, 0, bmp); 
} 

所以位圖以通常的方式繪製。在MSDN中,ERROR標誌被解釋爲「以前的剪輯區域不受影響」。設備應該先配置還是先前的區域被刪除?完成這項任務是否正確?我將在包含此位圖的TImage上使用SetWindowRgn,但TImage不是窗口,因此不具有Handle。請幫助我找出什麼是錯的。

+2

你確定用'bmp-> Handle'屬性嗎?這不應該是'bmp-> Canvas-> Handle'傳遞給'SelectClipRgn'函數嗎? – TLama 2013-03-07 17:49:00

+0

未顯示錯誤,但沒有位圖未被「裁剪」 – kokbira 2013-03-22 12:38:38

回答

1

只需使用CopyRect,方法Canvas

例如,創建一個名爲Button2的按鈕,插入下面的代碼:

void __fastcall TForm1::Button2Click(TObject *Sender) 
{ 
    //dimensions of real image: 300x300 

    //clipping a region 
    //dimension: 200x200 
    //offset (x,y): (10,10) 
    int xOff = 10; 
    int yOff = 10; 
    int widthOfRegion = 200; 
    int heightOfRegion = 200; 

    //printing the clipped region 
    //dimension: 200x200 
    //offset (x,y): (305,0) -> to do not overwrite image drawn by button1 
    int xOff2 = 305; 
    int yOff2 = 0; 
    int widthOfRegion2 = 200; 
    int heightOfRegion2 = 200; 

    Canvas->CopyRect(
     //first rect is destination 
     TRect(xOff2, yOff2, xOff2 + widthOfRegion2, yOff2 + heightOfRegion2) 
     //second is canvas of source 
     ,bmp->Canvas 
     //third is rect of source that you want to copy 
     ,TRect(xOff, yOff, xOff + widthOfRegion, yOff + heightOfRegion) 
    ); 
} 

所以結果如下,運行,按Button1的和Button2的經過: XD

提示:您可以放大進出區域2的修剪區域變化寬度和高度:)

來源:http://www.borlandtalk.com/how-to-use-selectcliprgn-vt11696.html