我在结合 GeckoFX 和 C# 时遇到了 2 个问题。
1.当我单击一个按钮时,我的应用程序将打开一个 OpenFileDialog(由 C# 代码生成)来更改 img 标签的 src 属性。我为此 img 使用上下文菜单来做到这一点。我的问题是,如果我单击一次打开 OpenFileDialog 的按钮,然后单击 img(没有上下文菜单),OpenFileDialog 将再次打开。
2.当我为此img选择新图像时,我无法删除旧文件(使用C#代码)这是我的代码
[HTML 和 Javascript 代码]
<script type="text/javascript">
$(document).ready(function(){
$('.div_image).bind('contextmenu',function(){
$('#contextmenu_image').css({top: e.pageY+'px',left: e.pageX+'px'}).show();
});
});
</script>
<div class="div_image" style="position: absolute; top: '20px;left:'20px;"><img id="img123" class="image" src="" style="width: 100%;height: 100%;"/></div>
<ul class="contextmenu" id="contextmenu_image" style="width: 100px; display: none;">
<li class="properties">Properties</li>
<li class="del">Delete</li>
<button id="choose_image">Choose image</button>
</ul>
[C#代码]
private void ChooseImage()
{
if (geckoWebBrowser1.Document.ActiveElement.GetAttribute("id") == "choose_image")
{
OpenFileDialog open = new OpenFileDialog();
open.Filter =
"Image (*.BMP;*.JPG;*.GIF;*.PNG;*.JPEG)|*.BMP;*.JPG;*.GIF;*.PNG;*.JPEG|" +
"All files (*.*)|*.*";
open.Title = "Choose an image";
DialogResult result = open.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
string srcFile = open.FileName;
string fileName = System.IO.Path.GetFileName(srcFile);
string fileExtent = System.IO.Path.GetExtension(srcFile);
string desDir = Application.StartupPath + "\\test\\images\\";
Random r = new Random();
string newFileName = "i_";
for (int i = 1; i <= 10; i++)
{
newFileName += Convert.ToString(r.Next(0, 9));
}
newFileName += fileExtent;
System.IO.File.Copy(srcFile, desDir + newFileName);
//Find old image
string old_image = geckoWebBrowser1.Document.GetElementById("img123").GetAttribute("src");
geckoWebBrowser1.Document.GetElementById("img123").SetAttribute("src", "images/" + newFileName);
if (old_image != "")
System.IO.File.Delete(desDir + old_image);//Delete old file,but unable
}
}
}
private void geckoWebBrowser1_DomClick(object sender, GeckoDomEventArgs e)
{
ChooseImage();
}
对不起,因为我的英语不好