inside SWT: start from OS

SWT程序设计中,Display充当SWT与底层操作系统、UI线程之间的桥梁,而OS类是Display实现的基础。

OS对界面部分操作系统底层API进行了封装,其父类C实现了内存操作函数,从而使得java中也可以像C/C++一样设计界面。

我们知道,基本的windows程序设计一般需要注册窗口类、创建窗口、处理消息循环三个过程,通过OS我们完全可以用java代码实现以上过程,看例子:纯OS实现的hello world!

 

import org.eclipse.swt.SWT;
import org.eclipse.swt.internal.Callback;
import org.eclipse.swt.internal.win32.MSG;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.internal.win32.PAINTSTRUCT;
import org.eclipse.swt.internal.win32.RECT;
import org.eclipse.swt.internal.win32.TCHAR;
import org.eclipse.swt.internal.win32.WNDCLASS;

public class App {
	public static void main(String[] args) {

		App app = new App();
		app.registerClass();
		app.InitInstance();
		app.readAndDispatchMessage();

	}

	private TCHAR windowClass;
	private int hInstance;
	private Callback windowCallback;
	private int windowProc;
	private int hwndMessage;
	private boolean quit;
	private final PAINTSTRUCT lpPaint = new PAINTSTRUCT();
	private final RECT rect = new RECT();

	private void error(int errorNoMoreCallbacks) {
		throw new RuntimeException("error");
	}

	private void InitInstance() {
		// 创建窗口
		hwndMessage = OS.CreateWindowEx(0, windowClass, null, OS.WS_BORDER | OS.WS_VISIBLE | OS.WS_SYSMENU, 0, 0, 0, 0, 0, 0, hInstance, null);
		String title = "SWT_Window";
		OS.SetWindowText(hwndMessage, new TCHAR(0, title, true));
		OS.SetWindowPos(hwndMessage, 0, 100, 100, 400, 300, 0);
		OS.ShowWindow(hwndMessage, OS.SW_SCROLLCHILDREN);
		OS.UpdateWindow(hwndMessage);
	}

	// 消息循环
	public void readAndDispatchMessage() {
		MSG msg = new MSG();
		while (!quit) {
			if (OS.PeekMessage(msg, 0, 0, 0, OS.PM_REMOVE)) {
				OS.TranslateMessage(msg);
				OS.DispatchMessage(msg);
			}
			OS.WaitMessage();
		}

	}

	// 注册窗口类
	protected void registerClass() {
		// 回调函数
		windowCallback = new Callback(this, "windowProc", 4); //$NON-NLS-1$
		windowProc = windowCallback.getAddress();
		if (windowProc == 0) {
			error(SWT.ERROR_NO_MORE_CALLBACKS);
		}

		// 注册窗口类
		windowClass = new TCHAR(0, "SWT_Window", true);

		int hHeap = OS.GetProcessHeap();
		hInstance = OS.GetModuleHandle(null);
		WNDCLASS lpWndClass = new WNDCLASS();
		lpWndClass.hInstance = hInstance;
		lpWndClass.lpfnWndProc = windowProc;
		lpWndClass.style = OS.CS_HREDRAW | OS.CS_VREDRAW | OS.CS_GLOBALCLASS;
		lpWndClass.hCursor = OS.LoadCursor(0, OS.IDC_ARROW);
		lpWndClass.hbrBackground = OS.BLACK_BRUSH;
		int byteCount = windowClass.length() * TCHAR.sizeof;
		lpWndClass.lpszClassName = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
		OS.MoveMemory(lpWndClass.lpszClassName, windowClass, byteCount);
		OS.RegisterClass(lpWndClass);
		OS.HeapFree(hHeap, 0, lpWndClass.lpszClassName);
	}

	// 回调函数
	int windowProc(int hwnd, int msg, int wParam, int lParam) {
		String helloWorld = "hello World!";
		switch (msg) {
		case OS.WM_PAINT:

			int hDC = OS.BeginPaint(hwnd, lpPaint);
			OS.GetWindowRect(hwndMessage, rect);
			rect.right -= rect.left;
			rect.left = 0;
			rect.bottom -= rect.top;
			rect.top = 0;
			OS.DrawTextW(hDC, helloWorld.toCharArray(), helloWorld.length(), rect, OS.DT_CENTER);
			OS.EndPaint(hwnd, lpPaint);
			break;
		case OS.WM_CLOSE:
			quit = true;
			break;
		default:
			return OS.DefWindowProc(hwnd, msg, wParam, lParam);
		}
		return 0;
	}
}

 运行上面的程序生成窗口如下:

inside SWT: start from OS

你可能感兴趣的:(eclipse,C++,c,C#,OS)