我可以创建一个只是另一个设备上下文的一部分的设备上下文吗?

发布于 2024-10-09 05:24:44 字数 646 浏览 5 评论 0 原文

我对一个图形控件进行了子类化,该控件采用设备上下文句柄 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
}

I have subclassed a graphics control that takes a device context handle, HDC, as an input and uses that for drawing. My new control is just the original control centered on top of a larger image. I would like to be able to call the original control's Draw() method for code re-use, but I'm unsure how to proceed.

Here's the idea:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昨迟人 2024-10-16 05:24:45

我想您可能对设备上下文是什么感到困惑。设备上下文是内存中可以绘制的位置,可以是屏幕缓冲区、位图或其他内容。因为我想你只想在屏幕上绘图,所以你只需要一个 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!

意中人 2024-10-16 05:24:45

虽然并非万无一失,但您可以通过使用 SetViewportOrgExSelectObject,其中一个区域被剪切到相关子区域。

这种方法的问题是,如果绘图代码已经使用了这些 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文