上一页 下一页

A basic example



#include <X11/Xlib.h>
#define HELLO "hello world"

main(int argc, char **argv)
{

	Display *display;  // X server connection
	Window *win;       //  Window ID

	GC  gc;                // Grpahics context
	int bw = 2 		// Border width
	int bc = BlackPixel;  // Border color
	int bgc = WhitePixel;  // Window background color
	XSizeHints   hints;     // Window sizing hints
	XEvent  event;            // Event data Structure

	// Open the display
	display = XOpenDisplay(NULL);

	// Set hints for window size and position
	hints.x = 200; hints.y = 200;
	hints.width=300; hints.height=200;
	hints.flags= PPosition| PSize;

	// Create the window
	win = XCreateSimpleWindow(display, DefaultRootWindow(display), hints.x, hints.y,  hints.width, hints.heigth, bw, bc, bgc);

	XSetStandardProperties(display, win, HELLO, HELLO, argv, argc, &hints);

	// Create a grpahics context for writing the text
	gc = XCreateGC(dispaly, win, NULL, NULL);

	// Specify events of interest (only exposure)
	XSelectInput(display, win, ExposureMask);

	// Show the window
	XMapWindow(display, win);  

	// Event loop
	while (TRUE) {
	   XNextEvent(display, &event);
	   // Discard all but the most recent expose event
	   if (event.type == Expose && event.xexpose.count == 0){
               int x, y;
               while (XCheckTypedEvent(display, Expose, &Event));
               XClearWindow(display, win);
               //********  Print "hello world" !! ******
               XDrawImageString(display, win, gc, 50, 50, HELLO, strlen(HELLO));
       }  //End if
    }  // end while

    exit(0);

}

A Motif example

图2:一个Motif编程的例子

#include <Xm/Xm.h> #include <Xm/PushB.h> main(int argc, char **argv) { Widget top_wid, button; XtAppContext app; top_wid = XtVaAppInitialize(&app, "Push", NULL, 0, &argc, argv, NULL, NULL); button = XmCreatePushButton(top_wid, "Push_me", NULL, 0);

/* tell Xt to manage button */ XtManageChild(button); /* attach fn to widget */ XtAddCallback(button, XmNactivateCallback, pushed_fn, NULL); XtRealizeWidget(top_wid); /* display widget hierarchy */ XtAppMainLoop(app); /* enter processing loop */ } void pushed_fn(Widget w, XtPointer client_data, XmPushButtonCallbackStruct *cbs) { printf("Don't Push Me!!\n"); }

Event dispatching can be handled in one of three ways.

上一页 下一页