一个基本的Windows C程序:
#include <windows.h> #include <stdio.h> LRESULT CALLBACK WinSunProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ) { WNDCLASS wndcls; wndcls.cbClsExtra=0; wndcls.cbWndExtra=0; wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); wndcls.hCursor=LoadCursor(NULL,IDC_CROSS); wndcls.hIcon=LoadIcon(NULL,IDI_ERROR); wndcls.hInstance=hInstance; wndcls.lpfnWndProc=WinSunProc; wndcls.lpszClassName="edgar108"; wndcls.lpszMenuName=NULL; wndcls.style=CS_HREDRAW | CS_VREDRAW; RegisterClass(&wndcls); HWND hwnd; hwnd=CreateWindow("edgar108","http://hi.baidu.com/edgar108",WS_OVERLAPPEDWINDOW, 0,0,600,400,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); MSG msg; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WinSunProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch(uMsg) { case WM_CHAR: char szChar[20]; sprintf(szChar,"char code is %d",wParam); MessageBox(hwnd,szChar,"char",0); break; case WM_LBUTTONDOWN: MessageBox(hwnd,"mouse clicked","message",0); HDC hdc; hdc=GetDC(hwnd); TextOut(hdc,0,50,"egdar108",strlen("edgar108")); //ReleaseDC(hwnd,hdc); break; case WM_PAINT: HDC hDC; PAINTSTRUCT ps; hDC=BeginPaint(hwnd,&ps); TextOut(hDC,0,0,"http://hi.baidu.com/edgar108",strlen("http://hi.baidu.com/edgar108")); EndPaint(hwnd,&ps); break; case WM_CLOSE: if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO)) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0; }
这里并不对这个C程序做太多的解释。一个最基本的Windows程序的流程为:
1.设计一个窗口类,即创建一个WNDCLASS结构体,给变量赋值。
2.注册窗口,即RegisterClass。
3.创建,显示,更新窗口,即CreateWindow、ShowWindow、UpdateWindow。
4.进入消息循环,即
while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
GetMessage不断从消息队列中抓取消息。如果这个消息是WM_QUIT,GetMessage返回0,从而结束while循环,进而结束整个程序。取到响应的消息后,进行转换,分发到响应的窗口。
5.此外,程序中还有一个"窗口过程函数",名字并不是固定的,在上面的程序中是WinSunProc,在创建WNDCLASS变量时赋值给lpfnWndProc,即wndcls.lpfnWndProc=WinSunProc;这个窗口过程函数是一个回调函数,所谓的回调函数通俗的说
就是程序员自己写的,但是却由系统或者上下文环境调用的,没法自己调用的函数。这个函数用来处理系统的发给窗口的
各种消息,是写程序真正逻辑的地方。
以上就是一个Windows程序的基本流程,一个SWT程序最终也会形成类似的结构。前面2篇文章中,我们已经看到了SWT中有:
if (OS.PeekMessage (msg, 0, 0, 0, OS.PM_REMOVE)) {
if (!filterMessage (msg)) {
OS.TranslateMessage (msg);
OS.DispatchMessage (msg);
}
runDeferredEvents ();
return true;
}
与C程序中的:
while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
相对应,但是其他的过程像CreateWindow、ShowWindow、还有那个窗口过程函数,我们现在都还没有看到。
那么这些函数到底在哪里呢?
现在我把前面的第一个SWT程序简化一下,简化成最短的程序:
package com.edgar;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
class TestDisplay {
public static void main(String[] args) {
Display display = new Display();// 创建一个display对象。
while (!display.isDisposed()) { // 原来是!shell.isDisposed,但是现在没有shell对象了,所以...
if (!display.readAndDispatch()) { // 如果display不忙
display.sleep(); // 休眠
}
}
display.dispose(); // 销毁display
}
}
运行这个程序,没有错误,也没有窗口出现。但是如果你进入Display的构造函数里面,一直跟踪代码,会发现Display中的protected void init()方法中出现了OS.RegisterClass (lpWndClass);和hwndMessage=OS.CreateWindowEx(.....)方法:
windowclass是窗口的名字。此处的值为SWT_Window0,后面调用了一个OS.SetWindowText()函数来设置窗口的
标题,title的值为"SWT_Window_SWT",但是虽然程序运行时没有显示窗口,但是通过VC6里面的Spy++可以看到
确实有这么一个窗口:
只不过这不是一个有效的窗口,为什么不是有效的窗口呢?我也不知道。。。。自己猜测因为没有调用OS.ShowWindow方法?
现在修改程序为:
package com.edgar;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
class TestDisplay {
public static void main(String[] args) {
Display display = new Display();// 创建一个display对象。
final Shell shell = new Shell(display);// shell是程序的主窗体
shell.setText("Java应用程序"); // 设置主窗体的标题
shell.setSize(200, 100); // 设置主窗体的大小
shell.open(); // 打开主窗体
while (!display.isDisposed()) { // 如果主窗体没有关闭则一直循环
if (!display.readAndDispatch()) { // 如果display不忙
display.sleep(); // 休眠
}
}
display.dispose(); // 销毁display
}
}
这个程序可以说是很多资料上SWT的第一个例子,属于"hello,world!"级别的,代码的功能也是一目了然。现在运行程序可以看到窗口了:
这个程序比上个程序多了一个shell对象,进入Shell的构造方法,得知最后调用的是这个构造方法:
Shell (Display display, Shell parent, int style, int /*long*/ handle, boolean embedded) {
super ();
checkSubclass ();
if (display == null) display = Display.getCurrent ();
if (display == null) display = Display.getDefault ();
if (!display.isValidThread ()) {
error (SWT.ERROR_THREAD_INVALID_ACCESS);
}
if (parent != null && parent.isDisposed ()) {
error (SWT.ERROR_INVALID_ARGUMENT);
}
this.center = parent != null && (style & SWT.SHEET) != 0;
this.style = checkStyle (parent, style);
this.parent = parent;
this.display = display;
this.handle = handle;
if (handle != 0 && !embedded) {
state |= FOREIGN_HANDLE;
}
reskinWidget();
createWidget ();
}
方法前面的代码都在做校验,最后调用了createWidget方法,从方法的名字我们也该猜到了--创建组件,进入方法,得知最后调用的是Control类的createWidget()方法:
void createWidget () {
state |= DRAG_DETECT;
foreground = background = -1;
checkOrientation (parent);
createHandle ();
checkBackground ();
checkBuffered ();
checkComposited ();
register ();
subclass ();
setDefaultFont ();
checkMirrored ();
checkBorder ();
if ((state & PARENT_BACKGROUND) != 0) {
setBackground ();
}
}
一次观察方法内调用的这些函数,发现了createHandle()方法:
void createHandle () {
int /*long*/ hwndParent = widgetParent ();
handle = OS.CreateWindowEx (
widgetExtStyle (),
windowClass (),
null,
widgetStyle (),
OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0,
hwndParent,
0,
OS.GetModuleHandle (null),
widgetCreateStruct ());
if (handle == 0) error (SWT.ERROR_NO_HANDLES);
int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
if ((bits & OS.WS_CHILD) != 0) {
OS.SetWindowLongPtr (handle, OS.GWLP_ID, handle);
}
if (OS.IsDBLocale && hwndParent != 0) {
int /*long*/ hIMC = OS.ImmGetContext (hwndParent);
OS.ImmAssociateContext (handle, hIMC);
OS.ImmReleaseContext (hwndParent, hIMC);
}
}
我们又看到createWindowEx函数了,第二个参数传的是windowClass ()的值,经过debug,得知最后的值还是SWT_Window0,从上文我们已经知道,在创建Display对象,
调用init()方法时,已经调用过OS.RegisterClass (lpWndClass)函数了。从程序的运行结果来看,确实是创建了一个有效地窗口。通过Spy++可以看到:
Window Caption(窗口标题):Java应用程序
Rectangle: 200 * 100
这些信息和我们程序中设定的是一样的。
所以,下一步我就自然而然的,要去看shell.setText("Java应用程序");这行代码的具体实现了。进入代码,得知调用的
是父类Decorations中的setText(String string)方法:
debug一下,可以看到就是嗲用OS.SetWindowText()函数来设置窗口的标题。