开发教程

一、新建工程

  1. 新建项目工程,框架选择“window应用程序”,.net6及以上版本。
  2. 打开项目所在文件夹,复制.csproj文件,任意重命令,留用(从这个.csproj打开工程就是dotnet原生工程,可以用来管理设计窗体)
  3. 打开项目工程属性,框架选择“window应用程序”,改配置<UseWindowsForms>为false,.net6及以上版本
  4. NuGet安装GtkSharp(3.24.24.95)、GTKSystem.Windows.Forms
  5. 检查form表单是否有使用图像资源,如果使用,需新建System.Resources.ResourceManager和System.ComponentModel.ComponentResourceManager,具体请看下面内容。
  6. 安装本下载包里的【VisualStudio开发插件】,用于添加窗体创建模板(可以不安装此插件,可以使用其它方案设计窗体,具体请看窗体设计器教程)。

二、开发工程配置:

以下配置在你的项目工程里操作,如果项目里没有使用资源图像文件,可以不用配置【1】【2】项:

1、GTKWinFormsApp.csproj
配置<UseWindowsForms>为false,(或者使用控制台应用程序,可用于调试)

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <UseWindowsForms>false</UseWindowsForms>

2、引用GTKSystem.Windows.Forms

3、新建添加配置文件Directory.Build.props,配置如下:

<Project>
    <PropertyGroup>
        <BaseIntermediateOutputPath>obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
        <IntermediateOutputPath>$(BaseIntermediateOutputPath)\$(TargetFramework)</IntermediateOutputPath>
    </PropertyGroup>
</Project>

4、【可选项】从NuGet安装GTKSystem.Windows.FormsDesigner,用于开启窗体设计器,可选装。
使用窗体设计器有几种方法,详细的使用教程可以访问https://www.gtkapp.com/formsdesigner

5、【可选项】新建System.Resources.ResourceManager类
在项目下新建System.Resources.ResourceManager类,继承GTKSystem.Resources.ResourceManager,用于覆盖原生System.Resources.ResourceManager类。 GTKSystem.Resources.ResourceManager实现了项目资源文件和图像文件读取。 如果项目里没有使用资源图像文件,可以不用新建此文件

6、【可选项】新建System.ComponentModel.ComponentResourceManager类
在项目下新建System.ComponentModel.ComponentResourceManager类,继承GTKSystem.ComponentModel.ComponentResourceManager,用于覆盖原生System.ComponentModel.ComponentResourceManager类。
GTKSystem.ComponentModel.ComponentResourceManager实现了项目资源文件和图像文件读取(调用GTKSystem.Resources.ResourceManager)。 如果项目里没有使用资源图像文件,可以不用新建此文件


三、Resources资源的使用

1、增加存放资源文件夹Resources

具体请看下面的【全局共享资源Properties/Resources.resx】和【窗体独占资源Form.resx】的使用方法。 一般情况下,单个图片资源可以直接兼容原生使用,可以无需额外配置,图片组资源则必须把图片文件存放到Resources文件夹下。

创建方法如下:

在项目下和编译输出目录下创建Resources文件夹,把Resources资源存放的图片复制到Resources文件夹,此文件夹和文件全部生成到工程项目编译输出目录下。

2、使用全局共享资源Properties/Resources.resx

新建System.Resources.ResourceManager类

在项目下新建System.Resources.ResourceManager类,继承GTKSystem.Resources.ResourceManager,用于覆盖原生System.Resources.ResourceManager类。 GTKSystem.Resources.ResourceManager实现了项目资源文件和图像文件读取。 如果项目里没有使用资源图像文件,可以不用新建此文件

新建System.ComponentModel.ComponentResourceManager类

在项目下新建System.ComponentModel.ComponentResourceManager类,继承GTKSystem.ComponentModel.ComponentResourceManager,用于覆盖原生System.ComponentModel.ComponentResourceManager类。
GTKSystem.ComponentModel.ComponentResourceManager实现了项目资源文件和图像文件读取(调用GTKSystem.Resources.ResourceManager)。 如果项目里没有使用资源图像文件,可以不用新建此文件

3、使用窗体独占资源Form.resx

新建System.ComponentModel.ComponentResourceManager类

在项目下新建System.ComponentModel.ComponentResourceManager类,继承GTKSystem.ComponentModel.ComponentResourceManager,用于覆盖原生System.ComponentModel.ComponentResourceManager类。
GTKSystem.ComponentModel.ComponentResourceManager实现了项目资源文件和图像文件读取(调用GTKSystem.Resources.ResourceManager)。 如果项目里没有使用资源图像文件,可以不用新建此文件

图片组资源的使用

由于GTKSystem无法读取图片组(ImageList),需要把图片组的图片存入到项目的Resources文件夹下(或者通过自写程序添加图片,则不需要在Resources目录放置图片),如:

Form2.Designer.cs的配置程序如下:
 imageList1.ImageStream = (ImageListStreamer)resources.GetObject("imageList1.ImageStream");
 imageList1.TransparentColor = System.Drawing.Color.Transparent;
 imageList1.Images.SetKeyName(0, "010.jpg");
 imageList1.Images.SetKeyName(1, "timg2.jpg");

那么需要把图片010.jpg和timg2.jpg复制到文件夹Resources或Resources/Form2(所属窗体名)。

或者通过程序添加:
 imageList1.Images.Add("010.jpg",image1);
 imageList1.Images.Add("timg2.jpg",image2);
 imageList1.Images.SetKeyName(0, "010.jpg");
 imageList1.Images.SetKeyName(1, "timg2.jpg");

四、如何运行软件

  1. windows下直接编译发布运行,Debug目录的demo_app.exe文件或demo_app.dll文件都可以直接运行。
  2. linux和macos下执行命令运行: > dotnet <app path>/demo_app.dll。

1、制作友好的启动方式

  1. 制作desktop快捷方式,通过鼠标双击即可启动软件。
  2. 制作sh文件,可以在sh文件里添加命令,通过调用sh文件启动,快捷方式也可以调用sh文件执行(有可能需要授权,如授权demo.sh:chmod +x demo.sh)。启动目录是容易引发问题的地方,先cd到程序根目录再启动程序是兼容性最好的方式,sh文件方便增加命令。

2、快捷方式制作方法

建一个文本文件,修改后缀名为.desktop(权限勾选可执行程序),用记事本打开文件,写入以下代码:
[Desktop Entry]
Type=Application
Version=1.0.0   # 软件版本
Name=english title
Name[zh_CN]=中文软件名字
Exec=<dotnet path>/dotnet <app path>  #如:/home/.dotnet/dotnet /home/test/你的App.dll 或$HOME/.dotnet/dotnet $HOME/test/你的App.dll。
#方法二:Exec=<软件所在目录>/xxapp.sh  #如:调用sh文件启动
Terminal=false  # 如果需要同时打开终端,则设为true,否则设为 false
Icon=<app icon path>  #如:/home/my/test/appicon.png
Categories=GTK;System
Type=Application
StartupNotify=true

注意:直接创建修改app.desktop可能会有执行权限问题,需要添加可执行权限
sudo chmod +x ~/Desktop/app.desktop

滚动至顶部