分层图形透明

时间:2017-03-22 17:10:56

标签: c# .net transparency gdi+ drawstring

只是制作一个小游戏,并希望将暂停菜单叠加到顶部。

计时器负责游戏循环。当它暂停时,它会调用一个单独的功能来绘制暂停菜单。现在我正在绘制一个小的居中矩形。然而,矩形并不透明,即使我将其清除为透明。

我有什么方法可以做到吗?

这是我的游戏循环的代码,用于更新状态并根据它们进行绘制。正如您所看到的,当游戏暂停时,它会调用一个单独的方法。

    private void tmrGameLoop_Tick(object sender, EventArgs e)
    {

        if (Paused)
        {
            Pause();
            return;
        }

        using (Graphics gr = CreateGraphics())
        {
            using (BufferedGraphicsContext bgc = new BufferedGraphicsContext())
            {
                using (BufferedGraphics bg = bgc.Allocate(gr, DisplayRectangle))
                {
                    bg.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    bg.Graphics.Clear(Color.Transparent);
                    // draw the stuff
                    bg.Render();
                }
            }
        }
    }

这是暂停()。它按照预期绘制了顶部,但由于它是一个单独的图形对象,它按照我的预期绘制并且不透明。

    private void Pause()
    {
        using (Graphics gr = CreateGraphics())
        {
            using (BufferedGraphicsContext bgc = new BufferedGraphicsContext())
            {
                using (BufferedGraphics bg = bgc.Allocate(gr, m_menu))
                {
                    bg.Graphics.Clear(Color.Transparent);
                    // draw pause menu
                    bg.Render();
                }
            }
        }
    }

有没有办法让它透明?

1 个答案:

答案 0 :(得分:1)

您似乎相信Graphics.Clear(Color.Transparent)会创建透明画布。

嗯,只有将它应用于Bitmap时才会出现这种情况。对于controlsBackColor允许Parent FormsParent Form Graphics Form using (SolidBrush br = new SolidBrush(Color.FromArgb(192, Color.Silver))) g.FillRectangle(br, ClientRectangle); StringFormat fmt = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; using (SolidBrush br = new SolidBrush(Color.FromArgb(128, Color.Tomato))) using (Font f = new Font("Consolas", 40f)) g.DrawString("PAUSED",f, br, ClientRectangle, fmt); Graphics e.Graphics Form黑色

它还重置 Paint对象,这可能也不是你想要的......

以下是如何在Timer.Tick上绘制半透明叠加层:

CreateGraphics

结果:

enter image description here

使用数字作为叠加层的alpha值和(可能)文本!

通常,要使用的cell3.innerHTML = " <input type='button' value='X' class='some_class'>" 对象应为$('body').on('click', '.some_class', function(){ $(this).closest('tr').remove(); }); $json = '{ "network":"BTC", "event_type":"address-transactions", "addresses":{ "3PvGLecQQMnm8oB2EqLbV94LFh7FLZpgJv":0 }, "data":{ }, "retry_count":0 }'; $test = json_decode($json, true); 事件中的foreach ($test["addresses"] as $key => $value) { echo sprintf("key: %s, value %s", $key, $value); }

但是,由于您似乎从public function rules() { return [ [['added_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['added_by' => 'id']], ]; } 完成了所有绘图,因此您可能会使用public function getAddedUser() { return $this->hasOne(User::className(), ['id' => 'added_by']); } 创建它,这通常不推荐,因为结果是非持久性< /强> ..