C++主进程中创建窗口绘制gif图片并不停接收消息循环,同时jni调用Java创建可输入窗口,当java窗口创建成功时,向C++端发送关闭gif图片窗口的信息并显示java窗口
时间: 2024-03-11 15:50:55 浏览: 116
下面是一个示例代码,实现了在 C++ 主进程中创建窗口绘制 GIF 图片并不停接收消息循环,同时通过 JNI 调用 Java 创建可输入窗口,当 Java 窗口创建成功时,向 C++ 端发送关闭 GIF 图片窗口的信息并显示 Java 窗口:
```c++
#include <iostream>
#include <windows.h>
#include <wingdi.h>
#include <jni.h>
#include <string>
using namespace std;
// 窗口类名
const char szClassName[] = "MyClass";
// Java 类名
const char* className = "com/example/MyJavaClass";
// Java 方法名
const char* methodName = "createWindow";
const char* methodSig = "()V";
// GIF 图片路径
const char* gifPath = "test.gif";
// GIF 图片宽度和高度
int gifWidth = 0;
int gifHeight = 0;
// 窗口句柄
HWND hWnd = NULL;
// GIF 图片句柄
HBITMAP hBmp = NULL;
// 窗口过程
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// 绘制 GIF 图片
HDC hdcMem = CreateCompatibleDC(hdc);
HGDIOBJ hOld = SelectObject(hdcMem, hBmp);
BITMAP bm;
GetObject(hBmp, sizeof(bm), &bm);
gifWidth = bm.bmWidth;
gifHeight = bm.bmHeight;
BitBlt(hdc, 0, 0, gifWidth, gifHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hOld);
DeleteDC(hdcMem);
EndPaint(hWnd, &ps);
break;
}
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
// 创建窗口
BOOL CreateMyWindow()
{
HINSTANCE hInstance = GetModuleHandle(NULL);
// 注册窗口类
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = szClassName;
if (!RegisterClass(&wc))
return FALSE;
// 创建窗口
hWnd = CreateWindow(szClassName, "My Window", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
return FALSE;
// 加载 GIF 图片
hBmp = (HBITMAP)LoadImage(NULL, gifPath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
if (!hBmp)
return FALSE;
// 显示窗口
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
return TRUE;
}
int main()
{
// 创建窗口
CreateMyWindow();
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// 释放 GIF 图片句柄
if (hBmp)
DeleteObject(hBmp);
// 卸载 Java 虚拟机
JavaVM* jvm;
JNIEnv* env;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString = "-Djava.class.path=./";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_FALSE;
jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (res == JNI_ERR)
{
return -1;
}
jclass cls = env->FindClass(className);
if (cls == nullptr)
{
return -1;
}
jmethodID mid = env->GetStaticMethodID(cls, methodName, methodSig);
if (mid == nullptr)
{
return -1;
}
env->CallStaticVoidMethod(cls, mid);
// 发送关闭窗口消息
if (hWnd)
SendMessage(hWnd, WM_CLOSE, 0, 0);
// 显示 Java 窗口
HWND javaHWnd = FindWindowA(NULL, "Java Window");
if (javaHWnd)
ShowWindow(javaHWnd, SW_SHOW);
return 0;
}
```
上述代码中,CreateMyWindow 函数用于创建窗口并加载 GIF 图片,WndProc 函数用于处理窗口消息。在主函数中,先创建窗口并启动消息循环,然后使用 JNI 调用 Java 创建窗口,接着向 C++ 端发送关闭窗口消息,最后显示 Java 窗口。
阅读全文