问题描述
我需要获得Firemonkey组件 TCanvas
的DC。我需要使用无法通过Firemonkey访问的Win API绘图功能,主要是100%控制字体渲染。
I need to get the DC of a Firemonkey component's TCanvas
. I need this to use Win API drawing functions not accessible through Firemonkey, mainly 100% control over font rendering.
显然,这是一个纯Windows应用程序,因此与OSX不是问题。
Obviously, this is a pure Windows Application, so any compatibilities with OSX isn't an issue.
我设法掌握了TForm的句柄,将其转换为HWND,然后使用 GetDC(FmxHandleToHWND(Handle));
I managed to get hold of the TForm's handle and convert it into a HWND, then getting the DC with GetDC(FmxHandleToHWND(Handle));
Character_PaintBox
控件的 OnPaint
处理程序。
HWND hWND = FmxHandleToHWND(Handle);
HDC hDC = GetDC(hWND);
int x = PreviewBack_Rectangle->Position->X + Character_PaintBox->Position->X;
int y = PreviewBack_Rectangle->Position->Y + Character_PaintBox->Position->Y;
TextOut(hDC,x,y,L"Test",4);
ReleaseDC(hWND,hDC);
这是表格的DC,我写的任何内容在下次更新时都会被覆盖。
How ever this is the Form's DC and anything I write is overwritten at next update.
这在VCL中是一项简单的任务,在Firemonkey中不会那么复杂,或者?
This was an easy task in VCL and it can't be that complicated in Firemonkey, or?
推荐答案
问题在于,使用Firemonkey,您只有一个用于表单的设备上下文,而没有每个组件的上下文。当需要重绘组件时,它会通过表单画布,但是裁剪和坐标映射到组件位置。正如您已经发现的那样,在Windows中,您可以随时获取该上下文并在其上进行绘制,但是随后您将与正常的firemonkey绘画(绘画方法中发生的绘画)竞争。
The problem is that with Firemonkey, you only have a single device context for the form and not one for each component. When a component needs to be redrawn, it gets passed the forms canvas but with clipping and co-ordinates mapped to the components location. As you already found, in Windows, you can get that context and draw on it at any time but you are then competing with the normal firemonkey painting which happens in the paint methods.
您可以在表单上放置一个TImage并对此进行自定义绘制。当表单需要绘制时,Firemonkey只会继续重绘图像。
You can put a TImage on the form and do your custom drawing to that. Firemonkey will just keep redrawing the image when the form needs drawing.
我知道您说过您不想要MAC,但对于其他人来说,您却无法在OSX上绘制图形上下文并对其进行绘制,因为该上下文在paint方法之外无效。因此,图像方法将是唯一的方法。大概可以解释为什么Firemonkey可以在单个上下文中使用。
I know you said you don't want MAC but for anyone else reading this, you can't get a graphics context on OSX and draw to it because the context isn't valid outside the paint method. So the image method would be the only way. This presumably explains why Firemonkey works with the single context.
这篇关于如何在Firemonkey中获取TCanvas DC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!