只绘制指定形式的位图一定不难,但我无法理解为什么代码不起作用(我在一些delphi示例中看到过):
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。请帮我找出问题所在。
答案 0 :(得分: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后:
提示:您可以放大或缩小剪裁区域,更改区域2的宽度和高度:)
来源:http://www.borlandtalk.com/how-to-use-selectcliprgn-vt11696.html