标题:求助 Graphics 不能绘制透明效果。
取消只看楼主
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:399
专家分:190
注 册:2021-4-9
结帖率:97.22%
已结贴  问题点数:20 回复次数:3 
求助 Graphics 不能绘制透明效果。





求助 Graphics 不能绘制透明效果。

100x100 左上部1/4正文形透明色 为什么不能像右下部一样漏出背景色。
第104 行代码,不是脱了裤子放屁吗???

镂空一个矩形的效果,用嵌套for循环来实现。
如果,需要镂空一个圆,或者不规则图形,或者镂空文字 的效果应该如何实现。。。
程序代码:
#pragma once

namespace ClearRectangle {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Form1 摘要
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO:  在此处添加构造函数代码
            //
        }

    protected:
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::PictureBox^ pictureBox1;
    protected:
    private: System::Windows::Forms::Button^ button1;

    private:
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        void InitializeComponent(void)
        {
            this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
            this->button1 = (gcnew System::Windows::Forms::Button());
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
            this->SuspendLayout();
            // 
            // pictureBox1
            // 
            this->pictureBox1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)),
                static_cast<System::Int32>(static_cast<System::Byte>(192)));
            this->pictureBox1->Location = System::Drawing::Point(0, 0);
            this->pictureBox1->Name = L"pictureBox1";
            this->pictureBox1->Size = System::Drawing::Size(200, 200);
            this->pictureBox1->TabIndex = 0;
            this->pictureBox1->TabStop = false;
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(51, 213);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(97, 39);
            this->button1->TabIndex = 1;
            this->button1->Text = L"button1";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(200, 266);
            this->Controls->Add(this->button1);
            this->Controls->Add(this->pictureBox1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
            this->ResumeLayout(false);

        }
#pragma endregion
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
    {
        SolidBrush^ Brush1 = gcnew SolidBrush(Color::FromArgb(192, 255, 255)); //淡青色
        SolidBrush^ Brush2 = gcnew SolidBrush(Color::FromArgb(0x00000000));    //透明色
        Bitmap^ MyImage = gcnew Bitmap(200, 200);
        Graphics^ g = Graphics::FromImage(MyImage);

        //200x200全画面淡青色
        g->FillRectangle(Brush1, 0, 0, 200, 200);

        //100x100 左上部1/4正文形透明色
        g->FillRectangle(Brush2, 0, 0, 100, 100);

        //100x100 右下部1/4正方形 每一个像素设置为透明色
        for (int i = 100; i < 200; i++)
            for (int j = 100; j < 200; j++)
                MyImage->SetPixel(i, j, Color::FromArgb(0x00000000));

        this->pictureBox1->Image =(Image^) MyImage->Clone();

        delete MyImage;
    }
    };
}
搜索更多相关主题的帖子: System Windows Forms namespace 透明 
2022-09-02 23:10
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:399
专家分:190
注 册:2021-4-9
得分:0 




终于想到了,绘制镂空效果的方法,只是太麻烦了!
大家,知不知道,在C++ CLR中有没有现成的库函数,可以直接绘制镂空效果。。。。

程序代码:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
    {
        SolidBrush^ Brush1 = gcnew SolidBrush(Color::FromArgb(0, 255, 255)); //青色
        Bitmap^ MyImage = gcnew Bitmap(200, 200);
        Bitmap^ TempImage = gcnew Bitmap(200, 200);
        Graphics^ g = Graphics::FromImage(MyImage);
        //200x200全画面青色
        g->FillRectangle(Brush1, 0, 0, 200, 200);
        //100x100 右下部1/4正方形 每一个像素设置为透明色
        for (int i = 100; i < 200; i++)
            for (int j = 100; j < 200; j++)
                MyImage->SetPixel(i, j, Color::FromArgb(0x00000000));
        //绘制镂空文字。。。。
        g = Graphics::FromImage(TempImage);
        String^ drawString = " 二货\r\n晚上好";
        System::Drawing::Font^ drawFont = gcnew System::Drawing::Font("宋体", 20);
        PointF drawPoint = PointF(5.0F, 5.0F);
        g->DrawString(drawString, drawFont, Brush1, drawPoint);
        for (int i = 0; i < 200; i++)
            for (int j = 0; j < 200; j++)
                if(TempImage->GetPixel(i,j) != Color::FromArgb(0x00000000))
                    MyImage->SetPixel(i, j, Color::FromArgb(0x00000000));
        this->pictureBox1->Image =(Image^) MyImage->Clone();
        delete Brush1;
        delete drawString;
        delete drawFont;
        delete TempImage;
        delete MyImage;
    }
2022-09-03 20:07
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:399
专家分:190
注 册:2021-4-9
得分:0 
大佬们,都去哪了。
为什么,都不回复我的问题???

绘制镂空,图案,有没有简单直接的方法???
2022-09-04 01:28
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:399
专家分:190
注 册:2021-4-9
得分:0 
回复 4楼 apull
我需要的是,删除区域的效果。我的数字幻方中,如何去掉上面的数字,之前每次都重新输出每一个单元格,3X3,4X4,5X5的时候感觉不出来什么。
到后面,8X8,9X9, 10X10,如果每次还重新输出每一个单元格。程序太卡,没法玩。
现在暂时,使用for循环,清除数字单元格。程序速度好多了。。。




2022-09-04 16:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-509940-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022802 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved