在本机sample_extension示例中,我添加了一个函数来获取当前活动窗口标题。 在飞镖内我想定期调用并打印getActivetWindowTitle
import "sample_extension.dart";
import "dart:async";
void main() {
var isLittleEndian = SysInfo.isLittleEndian;
var pageSize = SysInfo.pageSize;
var sizeOfInt = SysInfo.sizeOfInt;
var version = SysInfo.version;
print('Dart VM (${sizeOfInt * 8} bit): $version');
print('Page size: $pageSize KB');
print('Endianness: ${isLittleEndian ? 'Little-endian' : 'Big-endian'}');
new Timer.periodic(new Duration(seconds:1), (timer) => pr() );
}
void pr(){
var activeWindow = SysInfo.activeWindow;
print('Active window: $activeWindow');
}
我的dart文件sample_extension.dart是
library sample_extension;
import "dart-ext:sample_extension";
class SysInfo {
//<se>
static bool isLittleEndian = _isLittleEndian();
static final int pageSize = _getPageSize();
static final int sizeOfInt = _getSizeOfInt();
static final String version = _getVersionString();
static bool _isLittleEndian() native "IsLittleEndian";
static int _getPageSize() native "GetPageSize";
static int _getSizeOfInt() native "GetSizeOfInt";
static String _getVersionString() native "GetVersionString";
//</se>
//<aui>
static final String activeWindow = _getActiveWindowString();
static String _getActiveWindowString() native "GetActiveWindowString";
//</aui>
}
额外的cc部分是
void GetActiveWindowString(Dart_NativeArguments arguments) {
Dart_Handle result;
HWND hwnd;
LPTSTR lpString;
Dart_EnterScope();
hwnd=GetForegroundWindow();
GetWindowText(hwnd, lpString, 128);
result = Dart_NewStringFromCString(lpString);
Dart_SetReturnValue(arguments, result);
Dart_ExitScope();
}
我添加了函数查找函数列表
{"GetActiveWindowString", GetActiveWindowString},
加载dartapp时,我得到了正确的活动窗口。
但是在运行时,程序只会打印在start时初始化的值。不是实际的活动窗口。
我做错了什么?
答案 0 :(得分:4)
您的activeWindow
被标记为final
:A final variable can be set only once;
这意味着它在初始化后不会更改,并且会在{{1}开始时初始化在那个时刻。
试试这个:
getActiveWindowString()
static String get activeWindow => _getActiveWindowString();
现在是一个在通话时返回活动窗口的属性。
编辑:
activeWindow
实施中存在错误:
_getActiveWindowString
应该是:
LPTSTR lpString; // lpString is not initialized and points to random location
...
GetWindowText(hwnd, lpString, 128); // overrides the random location