Version 1 of custom cursors

Updated 2003-03-28 21:55:50

TR The -cursor option of many Tk widgets does not only allow to choose from the builtin cursors but also to used own creations. To accomplish this, you must specify the file, where the cursor lives:

 # windows:
 $widget configure -cursor "@path/to/cursorfile.cur"
 # unix:
 $widget configure -cursor "@path/to/cursorfile.xbm black"

So Windows takes a *.cur file and unix takes an *.xbm file and a color. On unix the cursor will be displayed in the size it was defined. Here is a 16x16 pixel cursor named hand.xbm:

 #define hand_width 16
 #define hand_height 16
 #define hand_x_hot 6
 #define hand_y_hot 0
 static unsigned char panPointer_bits[] = {
    0x00, 0x00, 0x80, 0x01, 0x58, 0x0e, 0x64, 0x12, 0x64, 0x52, 0x48, 0xb2,
    0x48, 0x92, 0x16, 0x90, 0x19, 0x80, 0x11, 0x40, 0x02, 0x40, 0x04, 0x40,
    0x04, 0x20, 0x08, 0x20, 0x10, 0x10, 0x20, 0x10 };

This file has two extra lines compared to "normal" xbm files that define where the so-called "hot spot" is. This is the point in the bitmap where a click event appears to come from. The coordinates count from left to right (x) and from top to bottom (y), just like the canvas coordinate system.

On Windows you should always create your cursor files with a size of 32x32 pixels even if you do not need so much space. If you created a 16x16 pixel cursor (as with unix above) it would get enlarged by a factor of two when displayed. A windows cursor file can easily be created from a bmp or gif file by the free Windows program IconShop [L1 ] (yes, it can also create windows icons). The only problem is, you cannot specify your own hotspot. But since the cur file format is known, we can do that with Tcl. Thus:

 set cur [open myCursorFile.cur r+]
 fconfigure $cur -encoding binary -translation binary
 # this is the file position of the x coord of the hot spot:
 seek $cur 10
 # set x=6
 puts -nonewline $cur [binary format c 6]
 # dto. for the y coord of the hot spot:
 seek $cur 12
 # set y=0
 puts -nonewline $cur [binary format c 0]
 close $cur

Now you have a nice cursor for your Unix and Windows programs.


Tk syntax help - Category Command