因為在網路上XUL的教學真的很少,所以在此提供一些自已從網路教學學得的基本功能,此功能的設計主要是要仿FontDialog的功能設計出來的,主要有選擇字型、大小、樣式、顏色(colorpicker)
範例如下:
xul code=======================================================
id = "&app.id;"
title = "&app.title;"
width = "400"
height = "450"
xmlns = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
onload = "onLoad();">
"/>
style="font-family:'新細明體'; font-size: 12px; font-style: italic; text-decoration: underline; font-weight: bold; color: #CC9900;"/>
紫色是顯示改變的地方,藍色是主要改變的程式碼,各自去呼叫副程式
FontDialog.js -------------------------------------------------------------------------------
var strFontName="新細明體";
var intFontSize=10;
var strFontBold="normal";
var strFontItalic="normal";
var strFontUnderline="none";
//color:black
var strFontColor="#000000";
function onLoad()
{
enableDump( true);
println( "application initialized.");
var enumerator = Components.classes["@mozilla.org/gfx/fontenumerator;1"].createInstance(Components.interfaces.nsIFontEnumerator);
var strFonts = enumerator.EnumerateAllFonts({});
println("length:"+strFonts.length);
var mpopup = document.getElementById("font-menupopup");
for(var i=0; i
{
//println("i:"+i);
var item = document.createElement("menuitem");
item.setAttribute("label",strFonts[i]);
item.setAttribute("oncommand","FontCall('"+i+"','"+strFonts[i]+"')");
mpopup.appendChild(item);
}
}
function FontCall(index,FontName)
{
println("FontName:"+FontName);
var textarea = document.getElementById('showText');
println("textarea:"+textarea);
strFontName=FontName;
textarea.setAttribute("value",FontName);
DisplayChange();
}
function SelectFontSize(FontSize)
{
println("FontSize:"+FontSize);
intFontSize=FontSize;
DisplayChange();
}
function SelectBold()
{
var cbox=document.getElementById('CBold');
if (cbox.checked)
{
strFontBold="bold";
}
DisplayChange();
}
function SelectItalic()
{
var cbox=document.getElementById('CItalic');
if (cbox.checked)
{
strFontItalic="italic";
}
DisplayChange();
}
function SelectUnderline()
{
var cbox=document.getElementById('CUnderline');
if (cbox.checked)
{
strFontUnderline="underline";
}
DisplayChange();
}
function SelectColor()
{
println("SelectColor");
var scolor = document.getElementById('Scolorpicker');
println("color:"+scolor.color);
strFontColor=scolor.color;
DisplayChange();
}
function DisplayChange()
{
var strDisplay="";
//Font-Family ------------------------------------------
strDisplay="font-family:'"+strFontName+"'; ";
//Font-Size --------------------------------------------
strDisplay+="font-size: "+intFontSize+"px; ";
//Font-Bold --------------------------------------------
strDisplay+="font-weight: "+strFontBold+"; ";
//Font-Italic ------------------------------------------
strDisplay+="font-style: "+strFontItalic+"; ";
//Font_Underline ---------------------------------------
strDisplay+="text-decoration: "+strFontUnderline+"; ";
//Font-Color -------------------------------------------
strDisplay+="color: "+strFontColor+"; ";
println("strDisplay:"+strDisplay);
//Chage style
var obj=document.getElementById('showText');
obj.setAttribute("style",strDisplay);
}
若有不懂之處,歡迎大家發問
ps:歡迎轉貼,但請註明出處,謝謝^^