22 KB 的十六进制编辑器组件

aardio 扩展库 com.hexEditor 用于开发十六进制编辑器程序,安装包仅 22 KB。在 「aardio 工具 > 扩展库」内双击 com.hexEditor 可以打开范例。

也可以直接双击运行「aardio 工具 > 文本文本 > 十六进制编辑器 」。


范例程序运行效果:

可直接拖放文件到窗口内,实现拖放功能的源码如下:

import com.hexEditor;
var hexEditor = com.hexEditor(mainForm.static);
hexEditor.setBuffer('请拖动文件到当前窗口');

mainForm.onDropFiles = function(files){
	hexEditor.load(files[1]);
}

支持查找替换,查找支持模式匹配语法(类正则表达式,语法更简单),也支持 \x 前缀的十六进制字节编码,实现该功能的源码如下:

//查找
mainForm.btnFind.oncommand = function(id,event){
  var str = string.unhex(mainForm.editFind.text,"\x");
  if(!#str){
    return mainForm.editFind.editBox.showErrorTip("请输入要查找的数据");
  }
  
  hexEditor.findNext(str);
}


//替换
mainForm.btnReplace.oncommand = function(id,event){
  var selStart,selEnd = hexEditor.getsel();
  if(!selEnd) return mainForm.btnFind.oncommand(); 
  
  var str = string.unhex(mainForm.editReplace.text,"\x");
  hexEditor.replace(str);
  
  mainForm.btnFind.oncommand();
}

可以在下拉框中切换文本编码 —— 默认为 65001 (UTF-8)。实现该功能的源代码如下:

mainForm.cmbCodePage.items = {"UTF-8(65001)";"Unicode(1200)";"Unicode BE(1201)";"ANSI(0)";"GB2312(936)";"BIG5(950)"};
mainForm.cmbCodePage.selIndex = 1;
mainForm.cmbCodePage.onListChange = function(){ 
  var text = mainForm.cmbCodePage.selText;
  var cp = tonumber(string.match(text,"\((\d+)"));
  if(cp!==null)  {
    cp = tonumber(cp);
    hexEditor.setCodePage(cp); 
  }
}

支持自定义右键菜单,实现该功能的源码如下:

import win.clip;
import win.ui.menu;
hexEditor.onContextMenu = function(x,y){ 
  var menu = win.ui.popmenu(mainForm.static);
  menu.add('复制	Ctrl+C',function(){
    hexEditor.copy();
  })
  
  menu.add('复制 16 进制编码',function(){
    var hex  = hexEditor.getSelHex() 
    if(#hex){
      win.clip.write(hex);
    }
  })
  
  menu.add('粘贴	Ctrl+V',function(){ 
      hexEditor.paste();
  }) 
  menu.popup(x,y,true); 
}

com.hexEditor 基于 VC++ 编写的组件,细节请参考该扩展库源代码。

用 aardio 编程语言调用 com.hexEditor 扩展库实现简单十六进制编辑器的完整代码:

import fonts.fontAwesome;
import win.ui;
/*DSG{{*/
mainForm = win.form(text="hexEditor";right=917;bottom=581;bgcolor=16777215)
mainForm.add(
btnFind={cls="plus";text="查找";left=707;top=523;right=778;bottom=550;align="left";color=3947580;db=1;dr=1;font=LOGFONT(h=-13);iconStyle={align="left";font=LOGFONT(h=-13;name='FontAwesome');padding={left=8}};iconText='';notify=1;textPadding={left=26};z=3};
btnReplace={cls="plus";text="替换";left=707;top=553;right=778;bottom=580;align="left";color=3947580;db=1;dr=1;font=LOGFONT(h=-13);iconStyle={align="left";font=LOGFONT(h=-13;name='FontAwesome');padding={left=8}};iconText='';notify=1;textPadding={left=26};z=5};
btnReplaceAll={cls="plus";text="全部替换";left=785;top=553;right=885;bottom=580;align="left";color=3947580;db=1;dr=1;font=LOGFONT(h=-13);iconStyle={align="left";font=LOGFONT(h=-13;name='FontAwesome');padding={left=8}};iconText='';notify=1;textPadding={left=26};z=6};
cmbCodePage={cls="combobox";left=784;top=526;right=911;bottom=552;db=1;dr=1;edge=1;items={"UTF-8(65001)";"Unicode(1200)";"Unicode BE(1201)";"ANSI(0)";"GB2312(936)";"BIG5(950)"};mode="dropdownlist";z=7};
editFind={cls="plus";left=16;top=520;right=701;bottom=546;align="right";border={bottom=1;color=-6908266};db=1;dl=1;dr=1;editable="edit";textPadding={top=6;bottom=2};z=2};
editReplace={cls="plus";left=16;top=549;right=701;bottom=575;align="right";border={bottom=1;color=-6908266};db=1;dl=1;dr=1;editable="edit";textPadding={top=6;bottom=2};z=4};
static={cls="static";left=0;top=0;right=919;bottom=514;bgcolor=16777215;db=1;dl=1;dr=1;dt=1;z=1}
)
/*}}*/

import com.hexEditor;
var hexEditor = com.hexEditor(mainForm.static);
hexEditor.setBuffer('请拖动文件到当前窗口');

mainForm.onDropFiles = function(files){
	hexEditor.load(files[1]);
}

import win.clip;
import win.ui.menu;
hexEditor.onContextMenu = function(x,y){ 
	var menu = win.ui.popmenu(mainForm.static);
	menu.add('复制	Ctrl+C',function(){
		hexEditor.copy();
	})
	
	menu.add('复制 16 进制编码',function(){
		var hex  = hexEditor.getSelHex() 
		if(#hex){
			win.clip.write(hex);
		}
	})
	
	menu.add('粘贴	Ctrl+V',function(){ 
			hexEditor.paste();
	}) 
	menu.popup(x,y,true); 
}

mainForm.btnFind.oncommand = function(id,event){
	var str = string.unhex(mainForm.editFind.text,"\x");
	if(!#str){
		return mainForm.editFind.editBox.showErrorTip("请输入要查找的数据");
	}
	
	hexEditor.findNext(str);
}

mainForm.editFind.setCueBannerText("请输入查找字符串,支持模式匹配、\x前缀 16 进制字节编码");
mainForm.editReplace.setCueBannerText("请输入查找字符串,支持\x前缀的 16 进制字节编码");

mainForm.btnReplace.oncommand = function(id,event){
	var selStart,selEnd = hexEditor.getsel();
	if(!selEnd) return mainForm.btnFind.oncommand(); 
	
	var str = string.unhex(mainForm.editReplace.text,"\x");
	hexEditor.replace(str);
	
	mainForm.btnFind.oncommand();
}

mainForm.btnReplaceAll.oncommand = function(id,event){
	var strFind = string.unhex(mainForm.editFind.text,"\x");
  	var strReplace = string.unhex(mainForm.editReplace.text,"\x");	
  	
  	var buf = hexEditor.getBuffer();
  	buf = ..string.replace(buf,strFind,strReplace );
	hexEditor.setBuffer(buf);
}

mainForm.btnFind.skin({
	color={
		active=0xFF00FF00;
		default=0xFF3C3C3C;
		disabled=0xFF6D6D6D;
		hover=0xFFFF0000
	}
})

mainForm.btnReplace.skin({
	color={
		active=0xFF00FF00;
		default=0xFF3C3C3C;
		disabled=0xFF6D6D6D;
		hover=0xFFFF0000
	}
})

mainForm.btnReplaceAll.skin({
	color={
		active=0xFF00FF00;
		default=0xFF3C3C3C;
		disabled=0xFF6D6D6D;
		hover=0xFFFF0000
	}
})

mainForm.cmbCodePage.selIndex = 1;
mainForm.cmbCodePage.onListChange = function(){ 
	var text = mainForm.cmbCodePage.selText;
	var cp = tonumber(string.match(text,"\((\d+)"));
	if(cp!==null)  {
		cp = tonumber(cp);
		hexEditor.setCodePage(cp); 
	}
}

mainForm.show();
win.loopMessage();
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章