namespace win.ui.atom

import winex;
import win.ui;
//assert( type(win.form)==type.class ,"必须首先导入win.ui库")//因为win.form是一个类，必须首先创建此类，才能正确的使用类的名字空间

/*intellisense(win.ui.atom)*/
GlobalAddAtom = ::Kernel32.api("GlobalAddAtomA","word(string lpString)") 
GlobalFindAtom = ::Kernel32.api("GlobalFindAtomA","word(string lpString)") 
GlobalDeleteAtom = ::Kernel32.api("GlobalDeleteAtom","word(word)") 
GetProp = ::User32.api("GetPropA","int(int hWnd,string lpString)") 
SetProp = ::User32.api("SetPropA","int(int hWnd,string lpString,int hData)")  
/*end intellisense*/

..win.form.metaProperty.atom = function( v ){
        if(!v)
        	if( owner[["uniqueId"]] ){
        		GlobalDeleteAtom(owner[["uniqueId"]] );
        		return;
        	}
        		
		v = tostring(v);
		var uniqueId =  GlobalFindAtom(v); //查找程序ID是否存在

		if (uniqueId) //程序ID存在,,激活已打开(的)程序实例(的)主窗口
		{		
			for hwnd,title,theadId,processId in winex.each(  ) {     
				if( GetProp(hwnd, "APP_ID")==uniqueId ){
					..win.setForeground(hwnd); 
					return false;
				}
			}
			
			GlobalDeleteAtom(uniqueId);
			uniqueId = null;
			owner[["uniqueId"]] = uniqueId;
			
		}
		uniqueId =  GlobalAddAtom(v); 
		SetProp(owner[["hwnd"]], "APP_ID", uniqueId);　
		owner[["uniqueId"]] = uniqueId;
		return true; 
}
 