WPF transparent text with opaque background

后端 未结 2 814
南笙
南笙 2021-01-26 16:44

I need transparent text with opaque background with WPF/XAML. Is that possible?

Here is an example, but I don\'t use css: css, transparent text with opaque background

2条回答
  •  礼貌的吻别
    2021-01-26 17:27

    U can make this by converting your text to Path and then Make Clipping On your white Rectangle with that Path.

    Try this:

    
                        
                            
                            
                        
    
                        
                        
    
    

    c# code

    private void textToMask_TextChanged(object sender, TextChangedEventArgs e)  
    {  
        Typeface face = new Typeface("Candara");  
        FormattedText tx = new FormattedText(textToMask.Text, Thread.CurrentThread.CurrentUICulture, FlowDirection.LeftToRight, face, 70, Brushes.Black);  
        Geometry textGeom = tx.BuildGeometry(new Point(0, 0));  
        Rect boundingRect = new Rect(new Point(-100000, -100000), new Point(100000, 100000));  
        RectangleGeometry boundingGeom = new RectangleGeometry(boundingRect);  
        GeometryGroup group = new GeometryGroup();  
        group.Children.Add(boundingGeom);  
        group.Children.Add(textGeom);  
        target.Clip = group;  
    } 
    

提交回复
热议问题