繁体   English   中英

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

[英]How can I put a theme for my SWT WPF program?

我想在我的java应用程序的Windows版本上使用WPF。 我发现使用SWT很容易做到这一点,它也支持SWT元素的wpf ResourceDictionary XAML( 请参阅此内容 )。 SWT WPF实现对我来说运作良好,但我无法找到如何将WPF主题放在其上。 一些与org.eclipse.swt.widgets.Button相同的SWT小部件都有setData方法,但其中一些像org.eclipse.swt.widgets.Display那样没有那个方法。 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);

    }
}

不再支持SWT WPF。 所以最好不要使用。

我必须承认我没有使用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>

所以我想你提供了一个Window(在你的情况下在一个单独的ResourceDictionary(XAML文件)中指定Shell,称为“CustomShellTemplateFile.xaml”)你可以做类似的事情

shell.setData(“ResourceDictionary”,“CustomShellTemplateFile.xaml”);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM