如何为我的SWT WPF程序添加主题?

4
我想在我的Java应用程序的Windows版本中使用WPF。我发现使用SWT很容易实现,它还支持SWT元素的WPF ResourceDictionary XAML(请参见这里)。SWT WPF 实现对我来说很好用,但我不知道如何将WPF主题应用于它。像org.eclipse.swt.widgets.Display这样的一些SWT小部件具有setData方法,但有些没有,例如org.eclipse.swt.widgets.Button。此外,在shell上设置setDate方法将无法为整个窗口设置主题。

那么我该如何为我的SWT WPF程序的整个窗口设置主题呢?

以下是示例代码:

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;

public class MainForm {

    protected Shell shell;

    public static void main(String[] args) {
        try {
            MainForm window = new MainForm();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    protected void createContents() {
        shell = new Shell();
        shell.setData(
                "ResourceDictionary",
                "ExpressionDark.xaml");
        // theme not applied on entire window, just buttons

        shell.setSize(450, 300);
        shell.setText("Window Title");

        Button button = new Button(shell, SWT.FLAT);
        button.setText("Button");
        button.setSize(250, 50);
        button.setBounds(0, 50, 250, 50);

        Menu menu = new Menu(shell, SWT.BAR);
        shell.setMenuBar(menu);

        MenuItem mntmNewSubmenu = new MenuItem(menu, SWT.CASCADE);
        mntmNewSubmenu.setText("New SubMenu");

        Menu menu_1 = new Menu(mntmNewSubmenu);
        mntmNewSubmenu.setMenu(menu_1);

    }
}
2个回答

1

0

我必须承认我没有使用过SWT WPF,所以这可能根本不起作用,但如果您正在使用标准的WPF,那么这通常是您要做的。

<Window 
    ....
    ....
    ....
    ResizeMode="CanResizeWithGrip"
    Template="{StaticResource WindowTemplateKey}">
</Window>

在编程中,你可能会有一个类似这样的模板

<!-- Custom Window : to allow repositioning of ResizeGrip-->
<ControlTemplate x:Key="WindowTemplateKey" TargetType="{x:Type Window}">
    <Border Background="{TemplateBinding Background}" 
            BorderBrush="{TemplateBinding BorderBrush}" 
            BorderThickness="{TemplateBinding BorderThickness}">
        <Grid>
            <AdornerDecorator>
                <ContentPresenter/>
            </AdornerDecorator>
            <ResizeGrip Visibility="Collapsed" 
                        HorizontalAlignment="Right" x:Name="WindowResizeGrip" 
                        Style="{DynamicResource ResizeGripStyle1}" 
                        VerticalAlignment="Bottom" IsTabStop="false"/>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="ResizeMode" Value="CanResizeWithGrip"/>
                <Condition Property="WindowState" Value="Normal"/>
            </MultiTrigger.Conditions>
            <Setter Property="Visibility" 
                 TargetName="WindowResizeGrip" Value="Visible"/>
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

所以我猜想,如果你有一个窗口(在你的情况下,它被指定为一个名为“CustomShellTemplateFile.xaml”的单独资源字典(XAML文件)),你可以这样做:

shell.setData( "ResourceDictionary", "CustomShellTemplateFile.xaml");


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接