dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: window color does not change when key press

  1. #1
    Join Date
    Sep 2008
    Posts
    5

    window color does not change when key press

    Code:
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    #include <X11/keysymdef.h>
    
    int main ( int argc , char *argv )
    {
    	Display *mydisplay ;
    	Window baseWindow;
    	XSetWindowAttributes myat ;
    	XSizeHints wmsize ;
    	XWMHints wmhints ;
    	XTextProperty windowName, iconName ;
    	XEvent baseEvent ;
    	GC mygc ;
    	XGCValues myGCvalues ;
    	XFontStruct * font1 ;
    	char *window_name = " Text window swi tching " ;
    	char *icon_name = " Swt " ;
    	int screen_num , done , y , i , index , charinc , count ;
    	unsigned long mymask ;
    	char bytes [ 3 ] ;
    	KeySym character ;
    	XComposeStatus cs ;
    	struct { /* Input window ring s t ruc tur e */
    		Window id ;
    		int last ;
    		char array [20] ;
    	} ring [ 4 ] ;
    	/* 1 . open connection to the server */
    	mydisplay = XOpenDisplay ( NULL );
    	/* 2 . c r e a t e a top-l eve l window */
    	screen_num = DefaultScreen (mydisplay ) ;
    	myat . border_pixel = BlackPixel (mydisplay , screen_num ) ;
    	myat . background_pixel = WhitePixel (mydisplay , screen_num ) ;
    	myat . event_mask = ExposureMask ;
    	mymask = CWBackPixel | CWBorderPixel | CWEventMask ;
    	baseWindow = XCreateWindow(mydisplay , RootWindow(mydisplay , screen_num) ,
    		100 , 100 , 250 , 270 , 2 ,DefaultDepth (mydisplay , screen_num) , InputOutput ,
    		DefaultVisual (mydisplay , screen_num) , mymask, &myat ) ;
    	/* 3 . give the Window Manager hint s */
    	wmsize . flags = USPosition | USSize ;
    	XSetWMNormalHints (mydisplay , baseWindow, &wmsize ) ;
    	wmhints . initial_state = NormalState ;
    	wmhints . flags = StateHint ;
    	XSetWMHints (mydisplay , baseWindow, &wmhints ) ;
    	XStringListToTextProperty (&window_name , 1 , &windowName ) ;
    	XSetWMName(mydisplay , baseWindow, &windowName ) ;
    	XStringListToTextProperty (&icon_name , 1 , &iconName ) ;
    	XSetWMIconName (mydisplay , baseWindow, &iconName ) ;
    	/* 4 . e s t a b l i s h window r e sour c e s */
    	myGCvalues . background = WhitePixel (mydisplay , screen_num ) ;
    	myGCvalues . foreground = BlackPixel (mydisplay , screen_num ) ;
    	mymask = GCForeground | GCBackground ;
    	mygc = XCreateGC(mydisplay , baseWindow, mymask, &myGCvalues ) ;
    	font1 = XLoadQueryFont (mydisplay , "6*10");
    	XSetFont (mydisplay , mygc , font1->fid ) ;
    	charinc = font1->per_char->width ;
    	/* 5 . c r e a t e a l l the other windows needed */
    	y = 30 ;
    	mymask = CWBackPixel | CWBorderPixel | CWEventMask ;
    	myat . event_mask = KeyPressMask ;
    	int default = DefaultDepth (mydisplay , screen_num) ;
    	int visual = DefaultVisual (mydisplay , screen_num)
    	int win1 = XCreateWindow(mydisplay , baseWindow, 70 , 0 , 20* charinc , 20 , 2 , depth, InputOutput , visual, mymask, &myat ) ;
    	int win2 = XCreateWindow(mydisplay , baseWindow, 70 , 60 , 20* charinc , 20 , 2 , depth, InputOutput , visual, mymask, &myat ) ;
    	XSetWindowBackground(mydisplay , win1  0xffff0000) ;
    	XSetWindowBackground(mydisplay , win2,  0xffff0000) ;
    	XMapWindow(mydisplay , win1) ;
    	XMapWindow(mydisplay , win2) ;
    	/* 8 . ent e r the event loop */
    	index = 0 ;
    	done = 0 ;
    	while ( done == 0 ) {
    	XNextEvent (mydisplay , &baseEvent ) ;
    	switch ( baseEvent . type ) {
    	case Expose :
    		break ;
    	case KeyPress :
    		count = XLookupString(&baseEvent . xkey , bytes , 3 , &character , &cs ) ;
    		index = 0 ;
    		for ( i =0; i <4; i ++)
    		if ( ring [i] . id == baseEvent . xkey .window ) index = i ;
    		switch ( count ) {
    		case 0 : /* Control cha r a c t e r */
    		switch ( character ) {
    		case XK_Up: /* Up arrow key */
    		case XK_KP_Up:
    		XSetWindowBackground(mydisplay , ring[index].id,  0xff00FF00) ;
    		index--;
    		XSetWindowBackground(mydisplay , ring[index].id,  0xFF0000FF) ;
    		if ( index < 0 ) index = 3 ;
    
    		XWarpPointer (mydisplay , None , ring [ index ] . id ,
    		0 , 0 , 0 , 0 , ring [ index ] . last * charinc , 10 ) ;
    		break ;
    		case XK_Down: /* Down arrow key */
    		case XK_KP_Down:
    		XSetWindowBackground(mydisplay , ring[index].id,  0xff00FF00) ;
    		index++;
    		XSetWindowBackground(mydisplay , ring[index].id,  0xFF0000FF) ;
    		if ( index > 3 ) index = 0 ;
    		XWarpPointer (mydisplay , None , ring [ index ] . id ,
    		0 , 0 , 0 , 0 , ring [ index ] . last * charinc , 10 ) ;
    		break ;
    		}
    		break ;
    		case 1 : /* Pr int abl e cha r a c t e r */
    		ring [ index ] . array [ ring [ index ] . last ] = bytes [ 0 ] ;
    		XDrawString (mydisplay , ring [ index ] . id , mygc , ring [ index ] . last * charinc , 15 , bytes , 1 ) ;
    		ring [ index ] . last ++;
    		break ;
    		}
    		break ;
    		}
    	}
    	/* 9 . c lean up before e x i t ing */
    	XUnmapWindow(mydisplay , baseWindow ) ;
    	return 0;
    }
    Last edited by 2kaud; October 13th, 2018 at 02:59 AM. Reason: Added code tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,288

    Re: window color does not change when key press

    It is
    Forum: C++ and WinAPI
    Discuss Windows API related issues using C++ (and Visual C++). This is a non-MFC forum.
    So, what "Windows API related issues" are you going to discuss?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,136

    Re: window color does not change when key press

    That code is really unreadable.
    Also, your code uses the X11 windowing system, which as Victor said, is not Windows API related.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)