【问题标题】:C++ MFC How to Draw Alpha transparent RectangleC ++ MFC如何绘制Alpha透明矩形
【发布时间】:2010-11-03 13:47:09
【问题描述】:

在 C++ MFC 应用程序中。使用 (CPaintDC dc(this);) 的 dc

如何绘制一个具有我可以调整的 alpha 透明度的矩形 (LPRECT)?

以下是我需要转换为 C++ 的示例 c# 代码

private void pictureBox1_Paint(object sender, PaintEventArgs e)  
{
    Graphics g = e.Graphics;
    Color color = Color.FromArgb(75,Color.Red); //sets color Red with 75% alpha transparency

    Rectangle rectangle = new Rectangle(100,100,400,400);
    g.FillRectangle(new SolidBrush(color), rectangle); //draws the rectangle with the color set.
} 

【问题讨论】:

    标签: c++ mfc paint


    【解决方案1】:

    您需要研究 GDI+。它有点麻烦,但您可以按如下方式创建“图形”对象:

    Gdiplus::Graphics g( dc.GetSafeHdc() );
    Gdiplus::Color color( 192, 255, 0, 0 );
    
    Gdiplus::Rect rectangle( 100, 100, 400, 400 );
    Gdiplus::SolidBrush solidBrush( color );
    g.FillRectangle( &solidBrush, rectangle );
    

    别忘了做

    #include <gdiplus.h>
    

    并调用

     GdiplusStartup(...);
    

    某处:)

    您会发现它与您的 C# 代码非常相似;)

    值得注意的是,您在 FromArgb 代码中输入的 75 并未设置 75% 的 alpha,它实际上设置了 75/255 或 ~29% 的 alpha。

    【讨论】:

    • 如果我调用 GdiplusStartup();我必须调用 GdiplusShutdown 吗?我想我必须。如果我必须,它应该在 Paint Event 中吗?
    • GdiplusStartup 通常应该从 MFC 应用程序的 InitInstance 函数调用一次。然后,应该在您的应用程序退出时调用一次 GdiplusShutdown。
    【解决方案2】:

    GDI(以及 MFC)对使用 alpha 进行绘图没有很好的支持。但是 GDI+ 也可以在 C++ 代码中使用。使用#include &lt;gdiplus.h&gt; 并使用 GdiplusStartup() 对其进行初始化。您可以使用 Graphics 类,使用您的 CPaintDC 中的 Graphics(HDC) 构造函数创建一个。并使用它的 FillRectangle() 方法。 SDK 文档are here

    【讨论】:

      【解决方案3】:
      int StartHoriz,StartVert,BarWidth,BarHeight; // rect start, width and height
      StartHoriz=0;
      StartVert=100;
      width = 100;
      height=120;
      CDC* pCDC = GetDC();      // Get CDC pointer
      CRect Rect(StartHoriz,StartVert,BarWidth,BarHeight);  //create rectangle dimensions
      pCDC->Rectangle(Rect);   //draw rectangle
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-05
        • 1970-01-01
        • 2016-03-12
        • 1970-01-01
        • 2016-05-25
        • 2013-08-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多