如何在没有窗口句柄的情况下修改窗口类
How do I modify a window class without a window handle?
我目前正在尝试使用JNI将一些WIN32API函数翻译为Java。其中一个函数是RegisterClassEx。通常,我认为你会为你注册的每个窗口类指定一个不同的回调函数,但因为我正在将回调转换为Java,所以这不起作用。
因此,当前的计划是将jobject
(定义为_jobject*
)附加到窗口类中,并在回调中使用它。问题是:您只能使用HWND更改附加到窗口类的数据。据我所知,MSDN文档没有指定一个函数,该函数可以仅使用窗口类ATOM或名称来修改窗口类。
因此我的问题:是否有一种方法来改变窗口类(使用SetClassLongPtr之类的东西),而不必使用有效的HWND?
Java端(我最终将添加一个公共函数来完成我实际需要做的事情):
public class c_winjni implements i_jnisystem {
public interface i_wnd_proc {
public int wnd_proc(long il_hwnd, int im_message, long im_wparam, long im_lparam);
}
private class c_wndclassex {
public int im_style = 0;
public i_wnd_proc ds_wnd_proc = null;
public int im_cls_extra = 0;
public int im_wnd_extra = 0;
public long il_instance = 0L;
public long il_icon = 0L;
public long il_small_icon = 0L;
public long il_cursor = 0L;
public long il_background = 0L;
public String str_menu_name = null;
public String str_class_name = null;
}
private static native short registerClassEx(c_wndclassex ds_wcx, int[] imr_error);
}
c++一边:
LRESULT CALLBACK default_window_callback_proc(HWND ds_hwnd, UINT im_message, WPARAM im_w_param, LPARAM im_l_param) {
return DefWindowProc(ds_hwnd, im_message, im_w_param, im_l_param);
}
/*
* Class: c_winjni
* Method: registerClassEx
* Signature: (Lc_winjni/c_wndclassex;[I)S
*/
JNIEXPORT_EX jshort JNICALL Java_c_1winjni_registerClassEx
(JNIEnv *ads_env, jclass /*jds_class*/, jobject jds_wcx, jintArray jimr_error)
JNI_CPPEXCEPTION_TRAP_BEGIN {
c_jnienv jds_env(ads_env);
jint *aim_error = NULL;
if (jimr_error && jds_env.get_array_length(jimr_error) > 0) {
aim_error = jds_env.get_array_elements(jimr_error, NULL);
}
WNDCLASSEX ds_wcx;
ds_wcx.cbSize = sizeof(WNDCLASSEX);
ds_wcx.style = jds_env.get_int_field(jds_wcx, "im_style");
// Imagine I'm checking whether field ds_wnd_proc in object jds_wcx is null.
// If it is, use the default callback (as shown below).
// If it isn't, set ds_wcx.lpfnWndProc to some other callback that reads
// custom class data and calls a Java function of the object attached to the window class.
ds_wcx.lpfnWndProc = default_window_callback_proc;
ds_wcx.cbClsExtra = jds_env.get_int_field(jds_wcx, "im_cls_extra") + sizeof(LONG_PTR);
ds_wcx.cbWndExtra = jds_env.get_int_field(jds_wcx, "im_wnd_extra");
ds_wcx.hInstance = (HINSTANCE) jds_env.get_long_field(jds_wcx, "il_instance");
ds_wcx.hIcon = (HICON) jds_env.get_long_field(jds_wcx, "il_icon");
ds_wcx.hIconSm = (HICON) jds_env.get_long_field(jds_wcx, "il_small_icon");
ds_wcx.hCursor = (HCURSOR) jds_env.get_long_field(jds_wcx, "il_cursor");
ds_wcx.hbrBackground = (HBRUSH) jds_env.get_long_field(jds_wcx, "il_background");
ct_jstring<TCHAR, 256> str_menu_name(ads_env, (jstring) jds_env.get_string_field(jds_wcx, "str_menu_name"));
ds_wcx.lpszMenuName = str_menu_name.get_data();
ct_jstring<TCHAR, 256> str_class_name(ads_env, (jstring) jds_env.get_string_field(jds_wcx, "str_class_name"));
ds_wcx.lpszClassName = str_class_name.get_data();
jshort result = RegisterClassEx(&ds_wcx);
if (result == NULL && aim_error) {
*aim_error = GetLastError();
}
// commit changes and invalidate pointer
if (aim_error) {
jds_env.release_array_elements(jimr_error, aim_error, 0);
}
return result;
} JNI_CPPEXCEPTION_TRAP_END2(ads_env, 0)
对您的问题的简单回答是,只能使用SetClassLongPtr
修改额外的类内存。您需要一个有效的窗口句柄
有趣的问题…WNDCLASSEX是一个类,而HWND是一个窗口句柄。您可以将前者视为OO类,将后者视为对象(实例)的"指针"或"引用"。然而,要修改类,似乎必须遍历该类的一个实例……奇怪:)我可以想象这是最常见的场景(有HWND)(顺便说一句,为什么你没有HWND?)
一个想法:是否可以使用ATOM创建一个窗口(隐藏),并使用返回的HWND作为SetClassLongPtr
的"引用"?
要获得注册类ATOM的有效HWND,可以使用FindWindow函数。
通过为lpWindowName
参数传递NULL
,您将获得第一个与类匹配的窗口的句柄。当然,如果没有窗口实例,您将失败。但是在这里,最好假设对应的窗口类还没有注册。
相关文章:
- QT QOpenGLWidget:如何在不使用数据块复制的情况下修改VBO中的单个顶点值?
- 在 C++ 中,我可以在不修改类的情况下定义对类的隐式转换吗?
- 类(可能是代理)的命名,允许在不修改基础容器的情况下对项目进行排序和删除
- 如何在不进行任何其他修改的情况下"pretty print" C++的XML字符串?
- 如何使用Winforms C#或C++.Net在不更改文件中其他内容的情况下修改jpg文件中的Orientation e
- 是否可以在不修改父类的情况下将成员变量初始化推迟到继承的类?
- 在不编写显式 setter 的情况下修改私有类数据成员的便捷方法是什么?模板有用吗?
- 在没有root权限的情况下安装NTL,并在生成文件中修改其构建路径
- 如何在不修改C 中的LHS参数的情况下实现` `运算符
- 如何在不知道大小的情况下编写过程来修改动态数组的元素和大小
- 在不使用public方法的情况下修改类private外部类
- 如何在不修改现有函数的情况下反转数组中除第一个和最后一个元素之外的所有元素
- 如何在不违反封装的情况下合法访问和修改私有字段矢量和映射
- 在没有 setter 的情况下修改基类中的私有数据,c++ visual studio 2013
- 在<algorithm>不使用C++函数的情况下修改数组?
- 在不访问StackType对象的情况下修改栈顶
- 如何在没有窗口句柄的情况下修改窗口类
- 如何在不调用成员函数的情况下修改对象
- 如何在没有模板方法的情况下修改函数行为
- 嵌入的铬 - 在不扩展类C++的情况下修改虚拟方法参数值