优化IME光标位置更新:使用作用域退出机制简化资源管理

This commit is contained in:
daiqingshuang
2025-05-14 14:54:04 +08:00
parent 188aa2d7b8
commit a1f09f9f10
2 changed files with 16 additions and 29 deletions

View File

@@ -4,6 +4,7 @@
#include <windows.h>
#include "misc/log_util.h"
#include "misc/scope_exit.h"
#include "platform_window/platform_window.h"
bool ime::update_cursor_pos(void* in_window_native) {
@@ -13,14 +14,17 @@ bool ime::update_cursor_pos(void* in_window_native) {
log_error("IME: failed to get IME context");
return false;
}
ON_SCOPE_EXIT{
ImmReleaseContext(hwnd, himc);
};
// 将客户区坐标转换为屏幕坐标
POINT point = { cursor_pos_.x(), cursor_pos_.y() };
// 设置组合窗口位置(跟随光标)
COMPOSITIONFORM cf;
cf.dwStyle = CFS_POINT;
cf.ptCurrentPos.x = cursor_pos_.x();
cf.ptCurrentPos.y = cursor_pos_.y();
cf.dwStyle = CFS_POINT;
cf.ptCurrentPos = point;
ImmSetCompositionWindow(himc, &cf);
ImmReleaseContext(hwnd, himc);
return true;
}

View File

@@ -323,12 +323,14 @@ namespace ime_events {
}
LRESULT handle_window_ime_set_context(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) {
// 获取输入法上下文
HIMC himc = ImmGetContext(hwnd);
if (!himc)
return DEFAULT_PROCESS;
ime::get().update_cursor_pos(hwnd);
// 对于WM_IME_SETCONTEXT如果要显示系统的输入法UI
if (w_param == TRUE) {
// 保持系统默认值以显示输入法UI
l_param |= ISC_SHOWUICOMPOSITIONWINDOW;
return DEFAULT_PROCESS;
}
return 0;
}
@@ -345,26 +347,7 @@ namespace ime_events {
if (!ime::get().has_processors())
return DEFAULT_PROCESS;
// 获取输入法上下文
HIMC himc = ImmGetContext(hwnd);
if (!himc)
return DEFAULT_PROCESS;
ime::get().update_cursor_pos(hwnd);
// 设置组合窗口字体
LOGFONT lf;
GetObject(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
ImmSetCompositionFont(himc, &lf);
ImmReleaseContext(hwnd, himc);
// 对于WM_IME_SETCONTEXT如果要显示系统的输入法UI
if (msg == WM_IME_SETCONTEXT && w_param == TRUE) {
// 保持系统默认值以显示输入法UI
l_param |= ISC_SHOWUICOMPOSITIONWINDOW;
return DEFAULT_PROCESS;
}
return 0;
}