用aardio写一个桌面挂件(简易版)-闲聊论坛-闲聊-阿甘软件

用aardio写一个桌面挂件(简易版)

2021年给小伙伴写的桌面挂件,用aardio很方便。

功能概述

  • 切换图片:双击选图窗体。(只能切换一次,设计如此)
  • 鼠标穿透:鼠标无视该窗体,可以正常点击它的底层窗体。
  • 自定义图片:拖放图片到悬浮窗上。
  • 隐藏挂件:双击托盘图标。

运行截图

image

源码&工程文件

import console;
import mouse;
import win.ui;
import win.cur;
import win.ui.menu;
import win.util.tray;
import win.ui.layered;
import win.dlg.message;
/*DSG{{*/
var winform = win.form(text="悬浮窗体";right=215;bottom=39;border="none";exmode="toolwindow";mode="popup")
winform.add(
plusForm={cls="plus";left=0;top=0;right=212;bottom=57;acceptfiles=1;notify=1;z=1}
)
/*}}*/

//tips
var tips_flag = true;
var msgDlg = win.dlg.message(winform);
var tips = function(){
	if(tips_flag){
		tips_flag = false;	//先false,避免多次调用下面代码
	    var form = msgDlg.create('拖放喜欢的图片上去试试~',,true)
		
		for(i=1;100;1){
			if(!form.valid){ break; }
			form.progress.progressPos = form.progress.progressPos + 1;
			win.delay(15) 
		}
		form.close();
	}
} 

var help = function(){
	winform.msgSmile('鼠标穿透:鼠标无视该窗体,可以正常点击它的底层窗体。\n自定义图:拖放图片到悬浮窗体上。\n隐藏挂件:双击托盘图标。\n彩蛋:双击挂件——悬浮窗体变更为彩蛋图片。\n');
}

var about = function(){
	winform.msgSmile('起源于2020/12/25[圣诞节]部分小伙伴桌面上的挂件——圣诞树');
}


//display winform
win.ui.layered(winform);


//tray
tray = win.util.tray(winform.hwnd);
tray.message = 0xACCF/*_WM_TRAYMESSAGE*/;


//popmenu
winform.popmenu = win.ui.popmenu(winform);
pop_flag = true;
winform.popmenu.add('开启鼠标穿透',function(id){
    if(pop_flag){
    	pop_flag = false;
    	winform.msgSmile('已开启!穿透:可以点击被遮挡的窗口,找到托盘图标可以关闭。');
    }
    winform.modifyStyleEx(,0x20/*_WS_EX_TRANSPARENT*/ | 0x80000/*_WS_EX_LAYERED*/ );
});
winform.popmenu.add('关闭鼠标穿透',function(id){
    winform.modifyStyleEx(0x20/*_WS_EX_TRANSPARENT*/ ,,);
})
winform.popmenu.add();
winform.popmenu.add('提示',function(id){
    help();
})
winform.popmenu.add('关于',function(id){
    about();
})
winform.popmenu.add();
winform.popmenu.add('退出',function(id){
    winform.close();
})


//show image
var showImg = function(img_path){
	var default_img = $"\res\tray.ICO";
	img_path = file == null ? default_img : file;
	
	screen_width, screen_height = win.getScreen();
	var max_img_width = screen_width/5.5;
	var max_img_height = screen_height/3.5;
	var min_img_width_height = 30;
	
	var img = gdip.image(img_path);
	var img_width = img.width;
	var img_height = img.height;
	
	var pos_img_width = null;	//img呈现的宽
	var pos_img_height = null;	//img呈现的高
	
	if(img_width > max_img_width or img_height > max_img_height){
		/*When the image width height > maximum width height, the image will be scaled in equal proportion.*/
		var zoom_multiple = img_width > img_height ? img_width/max_img_width : img_height/max_img_height;
		pos_img_width = math.round(img_width / zoom_multiple, 0);
		pos_img_height = math.round(img_height / zoom_multiple, 0);
	}
	elseif(img_width < min_img_width_height and img_height < min_img_width_height){
		var out_multiple = img_width > img_height ? min_img_width_height / img_height : min_img_width_height / img_width
		pos_img_width = math.round(img_width * out_multiple, 0);
		pos_img_height = math.round(img_height * out_multiple, 0);
	}
	else {
		/*Original size*/
		pos_img_width = img_width;
		pos_img_height = img_height;
	}
	
	winform.plusForm.setPos(, , pos_img_width, pos_img_height);
	winform.setPos(, , pos_img_width, pos_img_height);
	winform.plusForm.background = img_path;
	
	winform.show(true);
	win.setTopmost(winform.hwnd);
}

var hand = win.cur.load(0x7F86/*_IDC_SIZEALL*/)
//img plusForm message
winform.plusForm.wndproc = function(hwnd,message,wParam,lParam){
	select(message){
		case 0x233/*_WM_DROPFILES*/{
			file = win.getDropFile(wParam)[1];
			try{
				showImg(file);
			}
			catch(e){
				winform.msgErr("不支持的文件格式!");	
			}
		}
		case 0x203/*_WM_LBUTTONDBLCLK*/{
			file = $"\res\default2.png";
			showImg(file);
		}
		case 0x201/*_WM_LBUTTONDOWN*/{
			winform.hitCaption();
			win.cur.setCur(hand);
		}
		case 0x204/*_WM_RBUTTONDOWN*/{
            x,y = mouse.getPos();
            winform.popmenu.enable(2, false);
            winform.popmenu.popup( x,y,true );
		}
		case 0x200/*_WM_MOUSEMOVE*/{
			tips();
			win.cur.setCur(hand);
		}
	}
}


//winform message
winform.wndproc = function(hwnd,message,wparam,lparam){
    if( message == 0xACCF/*_WM_TRAYMESSAGE*/ ) { //托盘回调信息
        select(lparam){
            case 0x203/*_WM_LBUTTONDBLCLK*/{ //双击托盘图标打开窗口
                if( win.isVisible(winform.hwnd) ){
                    win.show(winform.hwnd,false);
                }
                else {
                    win.show(winform.hwnd,true);
                }
            }
            case 0x205/*_WM_RBUTTONUP*/ {//单击鼠标右键弹出菜单
                x,y = mouse.getPos();
                winform.popmenu.enable(2, true);
                winform.popmenu.popup( x,y,true );
            }  
            case 0x201/*_WM_LBUTTONDOWN*/ {//单击鼠标左键前置窗口
                win.setForeground(winform.hwnd)
            }        
        }   
    }
    if(message == 0x10/*_WM_CLOSE*/){
    	tray.delete();
    } 
}


//default show img
showImg(img_path);


win.loopMessage();
 
Topmost winform工程源文件
请登录后发表评论

    没有回复内容