窗口设置背景色
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);
}
}
}
HOperatorSet.DispText报错解决
Halcon C#联合开发HOperatorSet.DispText报错: HALCON error #5123: Wrong window type in operator disp_text
解决方法:
将hcanvas.dll拷贝到运行目录
但是,会导致其他问题,比如显示字体变细,鼠标滚轮缩放图像报错等
HALCON error #5190: Invalid window parameter的解决办法
解决方法:
SetWindowParam函数需要依赖动态库文件hcanvas.dll
HOperatorSet.InfoFramegrabber报错解决
HOperatorSet.InfoFramegrabber ,HOperatorSet.OpenFramegrabber报错
解决方法:
将pthreadVC2.dll拷贝到运行目录