// _Fontを返す、開放処理を簡略化したいので、スクリプト側で直接クラス生成をさせない public _Font Font(string font_family_name, int size, int frame_size) { var font = new _Font(font_family_name, size, frame_size); fonts.Add(font); return(font); }
// 文字列の描画 // DXライブラリの都合上、透明度はmain_colorのみ参照させる public void Text(int x, int y, string text, _Font font /*, Color main_color, Color frame_color*/) { if (font.main_color.color_a == 255) { DX.DrawStringFToHandle(x, y, text, font.main_color.color_uint, font.sfont.GetHandle(), font.frame_color.color_uint); } else { DX.SetDrawBlendMode(DX.DX_BLENDMODE_ALPHA, font.main_color.color_a); DX.DrawStringFToHandle(x, y, text, font.main_color.color_uint, font.sfont.GetHandle(), font.frame_color.color_uint); DX.SetDrawBlendMode(DX.DX_BLENDMODE_NOBLEND, 0); } }
public static void GetColors() { Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); Documents docs = app.Documents; Document doc = docs.Open("C:\\temp\\ColorTest3.docx", ReadOnly: true); StringBuilder sb = new StringBuilder(); sb.AppendLine("<html><body>"); sb.AppendLine("<table border=1 style='font-family:Arial;'>"); sb.AppendLine("<tr><th>Text</th><th>RGB</th><th>Color</th></tr>"); Dictionary <Color, int> counts = new Dictionary <Color, int>(); foreach (Range rng in doc.StoryRanges) { foreach (Range rngChar in rng.Characters) // by each character { _Font f = rngChar.Font; int rgb = (int)f.Color; //System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle(rgb); // do not use - not 1-to-1 var col = RgbColorRetriever.GetRGBColor(f.Color, doc); //ColorFormat cf = f.TextColor; // error - exception on ".doc" files sb.AppendLine(String.Format("<tr><td>{0}</td><td>{1},{2},{3}</td><td bgcolor='{4}'></td></tr>", rngChar.Text, col.R, col.G, col.B, ToHex(col))); //sb.AppendLine(rngChar.Text + " " + col.R + "/" + col.G + "/" + col.B); if (f.Color != WdColor.wdColorAutomatic) { if (counts.ContainsKey(col)) { counts[col]++; } else { counts[col] = 1; } } Marshal.ReleaseComObject(f); Marshal.ReleaseComObject(rngChar); } Marshal.ReleaseComObject(rng); } sb.AppendLine(String.Format("<tr><td colspan=3> </td></tr>")); foreach (Range rng in doc.StoryRanges) { foreach (Range rngWord in rng.Words) // by each word { _Font f = rngWord.Font; int rgb = (int)f.Color; //System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle(rgb); // do not use - not 1-to-1 var col = RgbColorRetriever.GetRGBColor(f.Color, doc); sb.AppendLine(String.Format("<tr><td>{0}</td><td>{1},{2},{3}</td><td bgcolor='{4}'></td></tr>", rngWord.Text, col.R, col.G, col.B, ToHex(col))); //sb.AppendLine(rngWord.Text + " " + col.R + "/" + col.G + "/" + col.B); Marshal.ReleaseComObject(f); Marshal.ReleaseComObject(rngWord); } Marshal.ReleaseComObject(rng); } sb.AppendLine("</table>"); sb.AppendLine("</body></html>"); doc.Close(false); app.Quit(false); Marshal.ReleaseComObject(doc); Marshal.ReleaseComObject(docs); Marshal.ReleaseComObject(app); // if there is a tie, then there is the risk that the color will flip back and forth depending on how the dictionary is ordered Color max = counts.Count == 0 ? Color.Black : counts.Aggregate((i1, i2) => i1.Value > i2.Value ? i1 : i2).Key; String html = sb.ToString(); // testing only }
/// <summary> /// 描画する文字列の横幅を取得する /// </summary> /// <param name="text"></param> /// <param name="font"></param> /// <returns></returns> public int GetTextWidth(string text, _Font font) { var res = DX.GetDrawStringWidthToHandle(text, text.Count(), font.sfont.GetHandle()); return(res); }