是否可以为Java生成JNI包装代码,因为我已经在visual studio 2013中编写了CPP GUI程序?我被困在如何为GUI创建SWIG接口文件,我看到的例子是使用类和简单的代码。
#include "resource.h"
#include "GUI.h"
const char g_szClassName[] = "myWindowClass";
// Global Variable
HBITMAP bitmap;
boolean displayLogo = TRUE;
// The DigProc
BOOL CALLBACK DigProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch (LOWORD(wparam))
{
case IDOK:
EndDialog(hwnd, IDOK);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
// The Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
OPENFILENAME ofn = { 0 };
char szFileName[MAX_PATH] = { 0 };
ofn.lStructSize = sizeof(OPENFILENAME);
switch (msg)
{
case WM_CAPTURECHANGED:
InvalidateRect(hwnd, NULL, true);
break;
case WM_CREATE:
bitmap = (HBITMAP)LoadImage(NULL, "SIT.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
break;
case WM_PAINT:
{
// Find the current windows size
if (displayLogo){
RECT rect;
int width = 0;
int height = 0;
if (GetWindowRect(hwnd, &rect))
{
width = rect.right - rect.left;
height = rect.bottom - rect.top;
}
hdc = BeginPaint(hwnd, &ps);
ClearWindow(hwnd);
HDC hMemDC = CreateCompatibleDC(hdc);
SelectObject(hMemDC, bitmap);
int x = (width - 30) / 2 - 300 / 2;
int y = (height - 60) / 2 - 169 / 2;
BitBlt(hdc, x, y, 300, 169, hMemDC, 0, 0, SRCCOPY);
DeleteDC(hMemDC);
EndPaint(hwnd, &ps);
}
else{
drawMaze(hwnd, fileName);
}
}
break;
case WM_COMMAND:
displayLogo = TRUE;
switch (LOWORD(wParam))
{
case ID_FILE_NEWMAZE:
// Open maze from text file
ofn.Flags = OFN_EXPLORER;
ofn.lpstrFilter = "Text File (*.txt)\0*.txt\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
if (GetOpenFileName(&ofn))
{
fileName = ofn.lpstrFile;
drawMaze(hwnd, fileName);
displayLogo = FALSE;
}
else{
MessageBox(NULL, "Load file unsuccessful!", "ERROR", MB_ICONERROR);
}
break;
case ID_PERFECTMAZE_DFS:
displayLogo = FALSE;
break;
case ID_PERFECTMAZE_SIMPLEPRIM:
displayLogo = FALSE;
break;
case ID_IMPERFECTMAZE_DFS:
displayLogo = FALSE;
break;
case ID_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case ID_ABOUT:
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DigProc);
break;
}
break;
// END NEW CODE
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);;
wc.lpszClassName = g_szClassName;
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 32, 32, 0);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"ICT1009 Maze",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, WIN_WIDTH, WIN_HEIGHT,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
// Draw maze
void drawMaze(HWND hwnd, string fileName){
if (fileName == "")
MessageBox(NULL, "File not found!", "ERROR", MB_ICONERROR);
else{
loadFromTextFile *maze = new loadFromTextFile(fileName);
if (!maze->isFileLoaded())
MessageBox(NULL, "Failed to generate maze!\n(File uploaded must be in form of binary!)", "Invalid Maze!", MB_ICONERROR);
else{
int cellSize = 5;
int row = 0, col = 0;
vector<vector<string>> nodeGrid = maze->getNodeGrid();
int rowSize = maze->getRows();
int colSize = maze->getColumns();
static HBRUSH redBrush = CreateSolidBrush(RGB(255, 0, 0));
static HBRUSH blackBrush = CreateSolidBrush(RGB(0, 0, 0));
COLORREF penColor = RGB(255, 0, 0);
HPEN gPen = CreatePen(PS_NULL, 3, penColor);
HDC hdc;
PAINTSTRUCT ps;
//ClearWindow(hwnd);
hdc = GetDC(hwnd);
SelectObject(hdc, gPen);
for (row = 0; row < rowSize; row++){
for (col = 0; col < colSize; col++){
RECT rec = { col*cellSize, row*cellSize, (col + 1)*cellSize, (row + 1)*cellSize };
if (nodeGrid[row][col] == "1")
FillRect(hdc, &rec, blackBrush);
else
FillRect(hdc, &rec, redBrush);
}
}
EndPaint(hwnd, &ps);
}
}
}
void ClearWindow(HWND hwnd)
{
HDC hdc = GetDC(hwnd); // Get the windows device context
RECT rect = { 0 }; // A RECT that will define the "client area" of the window
GetClientRect(hwnd, &rect); // Set "rect" to the client area of the window
// This will fill the window with all white
FillRect(hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
// Free Memory
ReleaseDC(hwnd, hdc);
}
void SetClientSize(HWND hwnd, int clientWidth, int clientHeight)
{
if (IsWindow(hwnd))
{
DWORD dwStyle = GetWindowLongPtr(hwnd, GWL_STYLE);
DWORD dwExStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
HMENU menu = GetMenu(hwnd);
RECT rc = { 0, 0, clientWidth, clientHeight };
if (!AdjustWindowRectEx(&rc, dwStyle, menu ? TRUE : FALSE, dwExStyle))
MessageBox(NULL, "AdjustWindowRectEx Failed!", "Error", MB_OK);
SetWindowPos(hwnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top,
SWP_NOZORDER | SWP_NOMOVE);
#ifdef _DEBUG
RECT newClientRC;
GetClientRect(hwnd, &newClientRC);
assert((newClientRC.right - newClientRC.left) == clientWidth);
assert((newClientRC.bottom - newClientRC.top) == clientHeight);
#endif
}
}