项目需要,功能需要实现列表编辑,输入判断,背景色修改,通过查询论坛代码: http://bbs.aardio.com/forum.php?mod=viewthread&tid=22568
获得了大神实现的库,但发现一些问题,比如
1.背景色修改之后无法恢复到原来的状态, 得手动记录修改前的值
2.无法获知编辑通知
3.无法限制输入是数字还是字符串
所以根据需要修改了下库代码
效果图
使用方法:
lvGrid.ctrl( listview ); //开启改色编辑, listview 必须有 fullRow=1 属性
listview.preventEtidCol(1,3); //阻止指定例编辑, 示例 阻止第1与第3列编辑
listview.nthChild(隔的行数, 隔行色值, 其它行色值); //隔行变色, 第一第二参数要设置, 第三是可选
listview.setItemColor(行号, 色值); //设置行的文字颜色 ,不给颜色值就是恢复原本颜色
listview.setItemColorBk(行号, 色值); //设置行的背景颜色,不给颜色值就是恢复原本颜色
listview.editChanged(原来的文本,更新的文本,行号,列号)//回调函数
库代码:
//lvGrid 可编辑列表视图
/***********************************************************************
改色的 是 lujjjh 的代码 论坛帖子 http://www.aau.cn/thread-7601-1-1.html
原地编辑 是 adsadss 的代码 论坛帖子 http://www.aau.cn/thread-5446-1-1.html
整合上述代码的帖子:http://bbs.aardio.com/forum.php?mod=viewthread&tid=22568
在上述基础上根据自己的项目添加了恢复颜色, 变更回调,和输入类型判断
lvGrid.ctrl( listview ); //开启改色编辑, listview 必须有 fullRow=1 属性
listview.preventEtidCol(1,3); //阻止指定例编辑, 示例 阻止第1与第3列编辑
listview.nthChild(隔的行数, 隔行色值, 其它行色值); //隔行变色, 第一第二参数要设置, 第三是可选
listview.setItemColor(行号, 色值); //设置行的文字颜色 ,不给颜色值就是恢复原本颜色
listview.setItemColorBk(行号, 色值); //设置行的背景颜色,不给颜色值就是恢复原本颜色
listview.editChanged(原来的文本,更新的文本,行号,列号)//回调函数,注意在回调这个的时候,listview可能还未刷新,直接获取list项目值需要延迟一下
***********************************************************************/
import win;
import util;
import win.imageList;
import win.util.tray;
import key.ime;
import mouse;
import win.ui;
namespace lvGrid;
ctrl = (function () {
var NMLVCUSTOMDRAW = class {
struct nmcd = {
struct hdr = ..win.ui.NMHDR();
INT dwDrawStage;
pointer hdc;
struct rc = ::RECT();
int dwItemSpec;
INT uItemState;
int lItemlParam;
};
INT clrText;
INT clrTextBk;
INT font;
int iSubItem;
INT dwItemType;
INT clrFace;
int iIconEffect;
int iIconPhase;
int iPartId;
int iStateId;
struct rcText = ::RECT();
INT uAlign;
}
var LVITEM = class { /* 项 */
INT mask;
int iItem;
int iSubItem;
INT state;
INT stateMask;
string text;
int cchTextMax;
int iImage;
int lParam;
int iIndent;
int iGroupId;
INT cColumns;
struct puColumns = ::POINT();
}
var LVCOLUMN = class { /* 列 */
INT mask;
int fmt;
int cx;
string text;
int cchTextMax;
int iSubItem;
int iImage;
int iOrder;
}
return function (listview) {
listview.preventetidcols = {};
listview.nthchilds = {};
listview.setsorttab = {};
listview.onlynum = {};
listview.currEditCol = {};
listview._colors1 = ..table.array(listview.count, 0);
listview._colors2 = ..table.array(listview.count, 0);
// listview.capture = true;
listview._colors1old = ..table.array(listview.count, 0);
listview._colors2old = ..table.array(listview.count, 0);
listview.setExtended(0x10000/*_LVS_EX_DOUBLEBUFFER*/);//启用双缓冲,避免闪烁
listview._prenotify = function (id,code,ptr) {
select(code) {
case 0xFFFFFFF4/*_NM_CUSTOMDRAW*/ {
var nmlvcd = NMLVCUSTOMDRAW();
..raw.convert(ptr, nmlvcd);
..raw.mixin(ptr, nmlvcd, { clrText = owner._colors1[nmlvcd.nmcd.dwItemSpec +1 ] });
..raw.mixin(ptr, nmlvcd, { clrTextBk = owner._colors2[nmlvcd.nmcd.dwItemSpec +1 ] });
select (nmlvcd.nmcd.dwDrawStage) {
case 0x1/*_CDDS_PREPAINT*/ {
return 0x20/*_CDRF_NOTIFYITEMDRAW*/
}
}
}
case 0xFFFFFF94/*_LVN_COLUMNCLICK*/ {
var nmlvw = ..win.ui.ctrl.listview.NMLISTVIEW();
..raw.convert(ptr, nmlvw);
var ind = nmlvw.iSubItem + 1;
if( owner.setsorttab[ind] )
owner.sort(ind, owner.setsorttab[ind]);
}
case (0xFFFFFFFE/*_NM_CLICK*/||0xFFFFFFFC/*_NM_RETURN*/ ){
var notifyMessage = owner.getNotifyMessage(code,ptr);
for(k,v in owner.preventetidcols)
if( notifyMessage.iSubItem == v) return ;
if( ! notifyMessage.iItem && notifyMessage.iSubItem ) return ;
var edit = owner.editlable
if( ! edit ){
owner.addCtrl(
editlable = {
cls="edit";font = LOGFONT( h = 15 );left = 0;top = 0;border=1;
right = 50;bottom = 50;autoResize = false ;hide = 1;
wndproc = function( hwnd, message, wParam, lParam ){
var update;
if( ( message = 0x8/*_WM_KILLFOCUS*/) || message == 0x101/*_WM_KEYUP*/ && wParam == 0xD/*_VK_RETURN*/ ) {
update = true;
}
elseif(message == 0x201/*_WM_LBUTTONDOWN*/ || message == 0x202/*_WM_LBUTTONUP*/ ){
var x,y = ..win.getMessagePos(lParam)
var rc = edit.clientRect;
if( ! ::PtInRect(rc,x,y) ){
update = true;
edit.capture = false;
}
else{
edit.capture = true;//是否捕获全局鼠标消息
}
}
//更新了
if( update&&owner.parent.editChanged ){
if(edit.oldText!=owner.text){
owner.parent.editChanged(edit.oldText,owner.text, owner.listViewNotifyMessage.iItem
, owner.listViewNotifyMessage.iSubItem);
edit.oldText = owner.text;
}
owner.parent.setItemText( owner.text, owner.listViewNotifyMessage.iItem
, owner.listViewNotifyMessage.iSubItem );
owner.show(false);
}
}
oncommand = function(id,event){
if(event==0x0400&&edit.onlyNum==1&&edit.text!=null){
if(edit.text=="-"){
edit.text="-0";
edit.setFocus(2, -1);
}elseif(edit.text=="0x"){
edit.text="0x0";
edit.setFocus(3, -1);
}elseif(edit.text==""||tonumber(edit.text)==null){
edit.showErrorTip("非法输入","只能输入实数")
edit.text="0";
edit.setFocus(0, -1);
}
}
}
}
)
edit = owner.editlable;
}else {
edit.onlyNum=0;
}
edit.listViewNotifyMessage = notifyMessage;
edit.text = owner.getItemText(notifyMessage.iItem,notifyMessage.iSubItem);
var rc = owner.getItemRect( notifyMessage.iItem,notifyMessage.iSubItem,,2/*_LVIR_LABEL*/ );
edit.setRect(rc);
edit.oldText = edit.text;
edit.setFocus(0, -1); //例行
edit.capture = true;
for(k,v in owner.onlynum){
if( notifyMessage.iSubItem == v){edit.onlyNum=1;break;}
}
if(edit.onlyNum==0){
..key.ime.setOpenStatus(1); //开启中文输入法
}else {
..key.ime.setOpenStatus(0); //关闭中文输入法
}
edit.show();
}
}
}
listview.preventEtidCol = function(...){ //阻止编辑例..让某例不能编辑
owner.preventetidcols = {...};
}
listview.nthChild = function(...){ //隔行变色
owner.nthchilds = {...};
}
listview.onlyNum = function(...){
owner.onlynum= {...};
}
listview.setItemColor = function (item, col) {
if(col){
owner._colors1[item] = col;
}else {
owner._colors1[item] = owner._colors1old[item];
}
}
listview.setItemColorBk = function (item, col) {
if(col){
owner._colors2[item] = col;
}else {
owner._colors2[item] = owner._colors2old[item];
}
}
listview.addItem = ..util.connect(listview, listview.addItem,
function (text, ind, ...) {
if (ind){
..table.insert(owner._colors1, 0, ind);
..table.insert(owner._colors1old, 0, ind);
}else{
..table.push(owner._colors1, 0);
..table.insert(owner._colors1old, 0);
}
if( #owner.nthchilds > 1 ){
var index = owner.count+1;
if( index % owner.nthchilds[1] == 0){
owner._colors2[index] = owner.nthchilds[2];
owner._colors2old[index] = owner.nthchilds[2];
}else{
owner._colors2[index] = owner.nthchilds[3];
owner._colors2old[index] = owner.nthchilds[3];
}
}
}
);
listview.delItem = ..util.connect(listview, listview.delItem,
function (item) {
..table.remove(owner._colors1, item);
}
);
}
})();
/**intellisense(lvGrid)
ctrl(__/*listview*/) = 使列表视图可编辑
ctrl() = !listview.
end intellisense**/