输入法开发中,C++代码如下: if (composition != NULL) { ITfContextView * view; if…
- User
输入法开发中,C++代码如下: if (composition != NULL) { ITfContextView * view; if (actioncontext->GetActiveView(&view) == S_OK && (view != NULL)) { RECT rect; ULONG fetched = 0; TF_SELECTION tfselection; if (actioncontext->GetSelection(ec, TF_DEFAULT_SELECTION, 1, &tfselection, &fetched) == S_OK && fetched != 0) { BOOL fClipped; HRESULT hrx = view->GetTextExt(ec, tfselection.range, &rect, &fClipped); if (hrx != S_OK) {
code} textservice->window_manager->AdjustCompositionWindow(&rect, NULL); tfselection.range->Release(); } } }用C# 写应该怎么写
- Assistant
if (composition != null) { ITfContextView view; if (actioncontext.GetActiveView(out view) == HRESULT.S_OK && (view != null)) { RECT rect; uint fetched = 0; TF_SELECTION tfselection; if (actioncontext.GetSelection(ec, TF_DEFAULT_SELECTION, 1, out tfselection, out fetched) == HRESULT.S_OK && fetched != 0) { bool fClipped; HRESULT hrx = view.GetTextExt(ec, tfselection.range, out rect, out fClipped); if (hrx != HRESULT.S_OK) {
code} textservice.window_manager.AdjustCompositionWindow(ref rect, null); tfselection.range.Release(); } }}
- User
以上改写代码 tfselection.range.Release();出错,没有Release方法,怎么办?
- Assistant
在C#中,使用完COM对象需要手动调用Marshal.ReleaseComObject方法进行释放。修改代码如下:
if (composition != null) { ITfContextView view; if (actioncontext.GetActiveView(out view) == HRESULT.S_OK && (view != null)) { RECT rect; uint fetched = 0; TF_SELECTION tfselection; if (actioncontext.GetSelection(ec, TF_DEFAULT_SELECTION, 1, out tfselection, out fetched) == HRESULT.S_OK && fetched != 0) { bool fClipped; HRESULT hrx = view.GetTextExt(ec, tfselection.range, out rect, out fClipped); if (hrx != HRESULT.S_OK) {
code} textservice.window_manager.AdjustCompositionWindow(ref rect, null); Marshal.ReleaseComObject(tfselection.range); // 释放 } }}
- User
输入法开发中,C++代码如下: MeowWindow::MeowWindow(HINSTANCE _hinstance, MeowWindowImpl * _impl, MeowWindowManager * _manager) { hinstance = _hinstance; impl = _impl; manager = _manager;
codeWNDCLASSEX wcex; TCHAR * tc = impl->GetWndClass(); BOOL registered = GetClassInfoEx(hinstance, impl->GetWndClass(), &wcex); // if (!registered) { memset(&wcex, 0, sizeof(wcex)); wcex.cbSize = sizeof(wcex); wcex.style = CS_HREDRAW | CS_VREDRAW | CS_IME; wcex.hInstance = hinstance; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)COLOR_MENUHILIGHT;//(HBRUSH)(COLOR_WINDOW + 1); wcex.lpfnWndProc = WndProc; wcex.lpszClassName = impl->GetWndClass(); RegisterClassEx(&wcex); } hwnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE | WS_EX_TOPMOST, impl->GetWndClass(), TEXT(""), WS_POPUP, 0, 0, 0, 0, NULL, 0, hinstance, this); ShowWindow(hwnd, SW_SHOWNOACTIVATE);} 用C# 写应该怎么写
- Assistant
以下是对应的C#代码:
public MeowWindow(HINSTANCE _hinstance, MeowWindowImpl _impl, MeowWindowManager _manager) { hinstance = _hinstance; impl = _impl; manager = _manager;
code - User
如上代码中的CreateWindowEx函数,应该怎么声明呢
- Assistant
CreateWindowEx 函数的声明在 C# 中为:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern IntPtr CreateWindowEx( uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam );
需要使用
[DllImport]特性进行声明,并指定函数名称、字符集和 SetLastError 等参数。 各参数的类型和含义与 C++中的CreateWindowEx 函数一致。需要注意的是,C# 中的 uint 类型对应的是无符号 32 位整数,而 int 对应的是有符号 32 位整数。 - User
这样的话this这个参数传进去会出错的
- Assistant
确实,抱歉我犯了一个错误。在 C# 中,this 作为对象引用在 C++ 中的 void* 类型的参数应该声明为 IntPtr 类型,而不是直接使用 this 关键字。因此,调用 CreateWindowEx 函数的值应该是
IntPtr.Zero。修改代码如下:hwnd = User32.CreateWindowEx(User32Constants.WS_EX_TOOLWINDOW | User32Constants.WS_EX_NOACTIVATE | User32Constants.WS_EX_TOPMOST, className, "", User32Constants.WS_POPUP, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, hinstance, IntPtr.Zero);
再次感谢您指出我的错误,希望这次的回答能够帮到您!