Halcon设置窗口背景色 C# C++

窗口设置背景色

hWindowControl控件直接设置背景色属性不管用,必须先HOperatorSet.SetWindowAttr("background_color", "sky blue");,再hWindowControl1.HalconWindow.OpenWindow() 一下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        HObject Image;
        HTuple imgWidth, imgHeight, partRow1, partColumn1, partRow2, partColumn2;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {                        
            HOperatorSet.ReadImage(out Image, Application.StartupPath + @"\plants_05.png");
            HOperatorSet.GetImageSize(Image, out imgWidth, out imgHeight);
            double scaleWidth = imgWidth[0].D / hWindowControl1.Size.Width;
            double scaleHeight = imgHeight[0].D / hWindowControl1.Size.Height;
            if (scaleWidth >= scaleHeight)
            {
                partRow1 = -(1.0) * ((hWindowControl1.Size.Height * scaleWidth) - imgHeight) / 2;
                partColumn1 = 0;
                partRow2 = partRow1 + hWindowControl1.Size.Height * scaleWidth;
                partColumn2 = partColumn1 + hWindowControl1.Size.Width * scaleWidth;
            }
            else
            {
                partRow1 = 0;
                partColumn1 = -(1.0) * ((hWindowControl1.Size.Width * scaleHeight) - imgWidth) / 2;
                partRow2 = partRow1 + hWindowControl1.Size.Height * scaleHeight;
                partColumn2 = partColumn1 + hWindowControl1.Size.Width * scaleHeight;
            }

            //////////hWindowControl控件
            //hWindowControl1.HalconWindow.CloseWindow();
            //hWindowControl1.BackColor = Color.AliceBlue; /////不管用
            //"sky blue"  "light blue"  "cornflower blue"
            HOperatorSet.SetWindowAttr("background_color", "sky blue"); /////必须的先设置一下background_color属性,再OpenWindow一下
            hWindowControl1.HalconWindow.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.Handle, "visible", "");
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, partRow1, partColumn1, partRow2, partColumn2);
            HOperatorSet.DispObj(Image, hWindowControl1.HalconWindow);



            //////////pictureBox控件
            HTuple wind;
            HOperatorSet.SetWindowAttr("background_color", "blue");
            HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "visible", "", out wind);
            HOperatorSet.SetPart(wind, partRow1, partColumn1, partRow2, partColumn2);
            HOperatorSet.DispObj(Image, wind);
        }

        private void hWindowControl1_SizeChanged(object sender, EventArgs e)
        {
            double scaleWidth = imgWidth[0].D / hWindowControl1.Size.Width;
            double scaleHeight = imgHeight[0].D / hWindowControl1.Size.Height;
            if (scaleWidth >= scaleHeight)
            {
                partRow1 = -(1.0) * ((hWindowControl1.Size.Height * scaleWidth) - imgHeight) / 2;
                partColumn1 = 0;
                partRow2 = partRow1 + hWindowControl1.Size.Height * scaleWidth;
                partColumn2 = partColumn1 + hWindowControl1.Size.Width * scaleWidth;
            }
            else
            {
                partRow1 = 0;
                partColumn1 = -(1.0) * ((hWindowControl1.Size.Width * scaleHeight) - imgWidth) / 2;
                partRow2 = partRow1 + hWindowControl1.Size.Height * scaleHeight;
                partColumn2 = partColumn1 + hWindowControl1.Size.Width * scaleHeight;
            }
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, partRow1, partColumn1, partRow2, partColumn2);
            HOperatorSet.DispObj(Image, hWindowControl1.HalconWindow);
        }
    }
}

 

            

 

发布了24 篇原创文章 · 获赞 3 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览