实例介绍
按照winform的编写方式写的代码,基本不写xaml文件,很容易看清楚。
包括:窗体无边框(窗体)、 FontAwesome字体图标、rtf转图像、动画移动控件、控件翻转、淡入淡出、控件位置获取、圆角图像、滚动条等等,
在cs文件中对照查看。使用.net 4.61及以下,不支持4.7。
基本按照一个留言板模式写的Demo,登陆、留言、用户信息等基本都有,因为是演示,没有搞得那么精致了。
用户信息在UserData.mdb,
登陆名:1001至1029,密码123456
图像及设计模式是照搬来winform的,开始仔细学习wpf,有错误或繁琐,自行修正,仅供参考
以下内容自行修改
bug修改:RichTextBox输入内容转换图像,搞成嵌入winform组件了,不科学。
修改如下:
Wpf 的RichTextBox(rich1) 插入图像:
private void Button_Click(object sender, RoutedEventArgs e)
{
string filepath = "";
OpenFileDialog openfilejpg = new OpenFileDialog();
openfilejpg.Filter = "png图片(*.png)|*.png|gif图片(*.gif)|*.gif";
openfilejpg.FilterIndex = 0;
openfilejpg.RestoreDirectory = true;
openfilejpg.Multiselect = false;
if (openfilejpg.ShowDialog() == true)
{
filepath = openfilejpg.FileName;
System.Windows.Controls.Image img = new System.Windows.Controls.Image();
BitmapImage bImg = new BitmapImage();
img.IsEnabled = true;
bImg.BeginInit();
bImg.UriSource = new Uri(filepath, UriKind.Relative);
bImg.EndInit();
img.Width = 28;
img.Height = 28;
img.Source = bImg;
img.UseLayoutRounding = true;
new InlineUIContainer(img, rich1.Selection.Start);
}
}
然后顺序取得里面的全部图像放入到List里面
public class imglist
{
public Image image;
public imglist(Image img)
{
image = img;
}
}
public static List<imglist> ImageList = new List<imglist>();
public static void ResizeRtbImages(RichTextBox rtb)
{
foreach (Block block in rtb.Document.Blocks)
{
if (block is Paragraph)
{
Paragraph paragraph = (Paragraph)block;
foreach (Inline inline in paragraph.Inlines)
{
if (inline is InlineUIContainer)
{
InlineUIContainer uiContainer = (InlineUIContainer)inline;
if (uiContainer.Child is Image)
{
Image image = (Image)uiContainer.Child;
ImageList.Add(new imglist(image));
}
}
}
}
}
}
转换为xaml
public static string ToXaml(RichTextBox richTextBox)
{
string xaml = string.Empty;
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
using (MemoryStream ms = new MemoryStream())
{
textRange.Save(ms, System.Windows.DataFormats.Xaml);
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
xaml = sr.ReadToEnd();
}
return xaml;
}
再替换字符串
string tmp = ToXaml(rich1);
int b = tmp.IndexOf("<Paragraph>") 11;
int t = tmp.LastIndexOf("</Paragraph>");
string tmp1 = tmp.Substring(b, t-b);
rich2.AppendText("\r\r\r" tmp1);
string tmp2 = tmp1.Replace("<Run> </Run>", "▣"). // 特殊符号
Replace("</Paragraph><Paragraph>", "\r").
Replace("<Run xml:lang=\"zh-cn\">", "").
Replace("</Run>", "");
最后画文字到一幅图像,遇到“特殊符号“顺序画ImageList[n].image就可以了
生成对话框需计算文字宽度等,参考原文件
自行修改,修改后可以用.net 4.7
相关软件
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)