如何在没有窗口句柄的情况下修改窗口类

How do I modify a window class without a window handle?

本文关键字:情况下 修改 窗口 窗口句柄      更新时间:2023-10-16

我目前正在尝试使用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,您将获得第一个与类匹配的窗口的句柄。当然,如果没有窗口实例,您将失败。但是在这里,最好假设对应的窗口类还没有注册。

相关文章: