我可以创建一个只是另一个设备上下文的一部分的设备上下文吗?
我对一个图形控件进行了子类化,该控件采用设备上下文句柄 HDC 作为输入并使用它进行绘图。我的新控件只是位于较大图像顶部的原始控件。我希望能够调用原始控件的 Draw() 方法来重用代码,但我不确定如何继续。
想法是这样的:
void CCheckBox::DrawCtrl( HDC hdc, HDC hdcTmp, LPSIZE pCtlSize, BYTE alpha ) {
// original method draws a checkbox
}
void CBorderedCheckBox::DrawCtrl( HDC hdc, HDC hdcTmp, LPSIZE pCtlSize, BYTE alpha ) {
// Draw my image here
// Create new hdc2 and hdcTemp2 which are just some portion of hdc and hdcTemp
// For example, hdc2 may just be a rectangle inside of hdc that is 20 pixels
// indented on all sides.
// Call CCheckBox::DrawCtrl() with hdc2 and hdcTemp2
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您可能对设备上下文是什么感到困惑。设备上下文是内存中可以绘制的位置,可以是屏幕缓冲区、位图或其他内容。因为我想你只想在屏幕上绘图,所以你只需要一个 DC。为了完成你想要的,我建议将一个矩形传递给函数,告诉它在哪里绘制。或者,如果性能较差,您可以为较小的区域创建一个新的位图,并为该函数提供位图的 DC 以供绘制。现在我想起来,这可能就是你最初的意思:P 祝你好运!
I think you may be confused of what a device context is. A device context is a place in memory that you can draw to, be it the screen buffer or a bitmap or something else. Since I imagine you only want to draw on the screen, you only need one DC. To accomplish what you want, I would recommend passing a rectangle to the function that tells it where to draw. Optionally, and with poorer performance, you could create a new Bitmap for the smaller area, and give the function the Bitmap's DC to draw on. Now that I think about it, that might have been what you meant in the first place :P Good luck!
虽然并非万无一失,但您可以通过使用 SetViewportOrgEx 和 SelectObject,其中一个区域被剪切到相关子区域。
这种方法的问题是,如果绘图代码已经使用了这些 API,则需要重写它,以了解它需要将其遮罩和偏移与现有的偏移和剪切区域结合起来。
While not foolproof, you can fake a DC as a subsection of a DC by using a combination of SetViewportOrgEx and SelectObject with a region clipped to the sub area in question.
The problem with this approach is if drawing code already uses these APIs it needs to rewritten to be aware that it needs to combine its masking and offsetting with the existing offsets and clipping regions.