Bagikan melalui


Menggunakan Kursor

Bagian ini membahas topik berikut.

Membuat Kursor

Contoh berikut membuat dua handle kursor: satu untuk kursor jam pasir standar dan satu untuk kursor kustom yang disertakan sebagai sumber daya dalam berkas definisi sumber daya aplikasi.

HINSTANCE hinst;            // handle to current instance 
HCURSOR hCurs1, hCurs2;     // cursor handles 
 
// Create a standard hourglass cursor. 
 
hCurs1 = LoadCursor(NULL, IDC_WAIT); 
 
// Create a custom cursor based on a resource. 
 
hCurs2 = LoadCursor(hinst, MAKEINTRESOURCE(240)); 

Aplikasi harus menerapkan kursor kustom sebagai sumber daya dan menggunakan LoadCursor, LoadCursorFromFile, atau LoadImage daripada membuat kursor pada waktu proses. Menggunakan sumber daya kursor menghindari ketergantungan perangkat, menyederhanakan pelokalan, dan memungkinkan aplikasi berbagi desain kursor.

Contoh berikut menggunakan fungsi CreateCursor untuk membuat kursor monokrom kustom pada waktu proses. Contohnya disertakan di sini untuk menggambarkan bagaimana sistem menginterpretasikan masker kursor.

HINSTANCE hinst;            // handle to current instance  
HCURSOR hCurs1, hCurs2;     // cursor handles 
 
HCURSOR hCurs3;             // cursor handle 
 
// Yin-shaped cursor AND mask (32x32x1bpp)
 BYTE ANDmaskCursor[] =
{
    0xFF, 0xFC, 0x3F, 0xFF,   // ##############----##############
    0xFF, 0xC0, 0x1F, 0xFF,   // ##########---------#############
    0xFF, 0x00, 0x3F, 0xFF,   // ########----------##############
    0xFE, 0x00, 0xFF, 0xFF,   // #######---------################
    0xF8, 0x01, 0xFF, 0xFF,   // #####----------#################
    0xF0, 0x03, 0xFF, 0xFF,   // ####----------##################
    0xF0, 0x03, 0xFF, 0xFF,   // ####----------##################
    0xE0, 0x07, 0xFF, 0xFF,   // ###----------###################
    0xC0, 0x07, 0xFF, 0xFF,   // ##-----------###################
    0xC0, 0x0F, 0xFF, 0xFF,   // ##----------####################
    0x80, 0x0F, 0xFF, 0xFF,   // #-----------####################
    0x80, 0x0F, 0xFF, 0xFF,   // #-----------####################
    0x80, 0x07, 0xFF, 0xFF,   // #------------###################
    0x00, 0x07, 0xFF, 0xFF,   // -------------###################
    0x00, 0x03, 0xFF, 0xFF,   // --------------##################
    0x00, 0x00, 0xFF, 0xFF,   // ----------------################
    0x00, 0x00, 0x7F, 0xFF,   // -----------------###############
    0x00, 0x00, 0x1F, 0xFF,   // -------------------#############
    0x00, 0x00, 0x0F, 0xFF,   // --------------------############
    0x80, 0x00, 0x0F, 0xFF,   // #-------------------############
    0x80, 0x00, 0x07, 0xFF,   // #--------------------###########
    0x80, 0x00, 0x07, 0xFF,   // #--------------------###########
    0xC0, 0x00, 0x07, 0xFF,   // ##-------------------###########
    0xC0, 0x00, 0x0F, 0xFF,   // ##------------------############
    0xE0, 0x00, 0x0F, 0xFF,   // ###-----------------############
    0xF0, 0x00, 0x1F, 0xFF,   // ####---------------#############
    0xF0, 0x00, 0x1F, 0xFF,   // ####---------------#############
    0xF8, 0x00, 0x3F, 0xFF,   // #####-------------##############
    0xFE, 0x00, 0x7F, 0xFF,   // #######----------###############
    0xFF, 0x00, 0xFF, 0xFF,   // ########--------################
    0xFF, 0xC3, 0xFF, 0xFF,   // ##########----##################
    0xFF, 0xFF, 0xFF, 0xFF    // ################################
};
 
// Yin-shaped cursor XOR mask (32x32x1bpp)
BYTE XORmaskCursor[] =
{
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x03, 0xC0, 0x00,   // --------------####--------------
    0x00, 0x3F, 0x00, 0x00,   // ----------######----------------
    0x00, 0xFE, 0x00, 0x00,   // --------#######-----------------
    0x03, 0xFC, 0x00, 0x00,   // ------########------------------
    0x07, 0xF8, 0x00, 0x00,   // -----########-------------------
    0x07, 0xF8, 0x00, 0x00,   // -----########-------------------
    0x0F, 0xF0, 0x00, 0x00,   // ----########--------------------
    0x1F, 0xF0, 0x00, 0x00,   // ---#########--------------------
    0x1F, 0xE0, 0x00, 0x00,   // ---########---------------------
    0x3F, 0xE0, 0x00, 0x00,   // --#########---------------------
    0x3F, 0xE0, 0x00, 0x00,   // --#########---------------------
    0x3F, 0xF0, 0x00, 0x00,   // --##########--------------------
    0x7F, 0xF0, 0x00, 0x00,   // -###########--------------------
    0x7F, 0xF8, 0x00, 0x00,   // -############-------------------
    0x7F, 0xFC, 0x00, 0x00,   // -#############------------------
    0x7F, 0xFF, 0x00, 0x00,   // -###############----------------
    0x7F, 0xFF, 0x80, 0x00,   // -################---------------
    0x7F, 0xFF, 0xE0, 0x00,   // -##################-------------
    0x3F, 0xFF, 0xE0, 0x00,   // --#################-------------
    0x3F, 0xC7, 0xF0, 0x00,   // --########----######------------
    0x3F, 0x83, 0xF0, 0x00,   // --#######------#####------------
    0x1F, 0x83, 0xF0, 0x00,   // ---######------#####------------
    0x1F, 0x83, 0xE0, 0x00,   // ---######------####-------------
    0x0F, 0xC7, 0xE0, 0x00,   // ----######----#####-------------
    0x07, 0xFF, 0xC0, 0x00,   // -----#############--------------
    0x07, 0xFF, 0xC0, 0x00,   // -----#############--------------
    0x01, 0xFF, 0x80, 0x00,   // -------##########---------------
    0x00, 0xFF, 0x00, 0x00,   // --------########----------------
    0x00, 0x3C, 0x00, 0x00,   // ----------####------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00    // --------------------------------
};
 
// Create a custom cursor at run time. 
 
hCurs3 = CreateCursor( hinst,   // app. instance 
             19,                // horizontal position of hot spot 
             2,                 // vertical position of hot spot 
             32,                // cursor width 
             32,                // cursor height 
             ANDmaskCursor,     // AND mask 
             XORmaskCursor );   // XOR mask 

Untuk membuat kursor, CreateCursor menerapkan tabel kebenaran berikut ke masker AND dan XOR .

DAN masker Masker XOR Tampilan
0 0 Hitam
0 1 Putih
1 0 Layar
1 1 Layar terbalik

Untuk informasi selengkapnya, lihat Bitmap.

Membuat Kursor Berpendar Alfa

Ikuti langkah-langkah ini untuk membuat kursor atau ikon blending alfa pada waktu eksekusi:

  • Selesaikan struktur BITMAPV5HEADER sesuai dengan contoh kode berikut, untuk menentukan DIB dengan pencampuran alfa 32 bit per pixel (BPP).
  • Panggil fungsi CreateDIBSection untuk membuat bagian DIB berdasarkan struktur BITMAPV5HEADER yang Anda selesaikan.
  • Gunakan bitmap dan informasi alfa yang Anda inginkan untuk kursor atau ikon campuran alfa Anda untuk menyelesaikan bagian DIB.
  • Lengkapi struktur ICONINFO .
  • Tempatkan bitmap monokrom kosong di bidang hbmMask, lalu tempatkan bagian DIB yang dicampur alfa di bidang hbmColor.
  • Panggil fungsi CreateIconIndirect untuk membuat kursor atau ikon campuran alfa.

Kode berikut menunjukkan cara membuat kursor campuran alfa. Anda dapat menggunakan kode yang sama untuk membuat ikon campuran alfa dengan mengubah anggota fIcon struktur ICONINFO menjadi TRUE:

HCURSOR CreateAlphaCursor(void)
{
    HDC hMemDC;
    DWORD dwWidth, dwHeight;
    BITMAPV5HEADER bi;
    HBITMAP hBitmap, hOldBitmap;
    void *lpBits;
    DWORD x,y;
    HCURSOR hAlphaCursor = NULL;

    dwWidth  = 32;  // width of cursor
    dwHeight = 32;  // height of cursor

    ZeroMemory(&bi,sizeof(BITMAPV5HEADER));
    bi.bV5Size           = sizeof(BITMAPV5HEADER);
    bi.bV5Width           = dwWidth;
    bi.bV5Height          = dwHeight;
    bi.bV5Planes = 1;
    bi.bV5BitCount = 32;
    bi.bV5Compression = BI_BITFIELDS;
    // The following mask specification specifies a supported 32 BPP
    // alpha format for Windows XP.
    bi.bV5RedMask   =  0x00FF0000;
    bi.bV5GreenMask =  0x0000FF00;
    bi.bV5BlueMask  =  0x000000FF;
    bi.bV5AlphaMask =  0xFF000000; 

    HDC hdc;
    hdc = GetDC(NULL);

    // Create the DIB section with an alpha channel.
    hBitmap = CreateDIBSection(hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS, 
        (void **)&lpBits, NULL, (DWORD)0);

    hMemDC = CreateCompatibleDC(hdc);
    ReleaseDC(NULL,hdc);

    // Draw something on the DIB section.
    hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
    PatBlt(hMemDC,0,0,dwWidth,dwHeight,WHITENESS);
    SetTextColor(hMemDC,RGB(0,0,0));
    SetBkMode(hMemDC,TRANSPARENT);
    TextOut(hMemDC,0,9,"rgba",4);
    SelectObject(hMemDC, hOldBitmap);
    DeleteDC(hMemDC);

    // Create an empty mask bitmap.
    HBITMAP hMonoBitmap = CreateBitmap(dwWidth,dwHeight,1,1,NULL);

    // Set the alpha values for each pixel in the cursor so that
    // the complete cursor is semi-transparent.
    DWORD *lpdwPixel;
    lpdwPixel = (DWORD *)lpBits;
    for (x=0;x<dwWidth;x++)
       for (y=0;y<dwHeight;y++)
       {
           // Clear the alpha bits
           *lpdwPixel &= 0x00FFFFFF;
           // Set the alpha bits to 0x9F (semi-transparent)
           *lpdwPixel |= 0x9F000000;
           lpdwPixel++;
       }

    ICONINFO ii;
    ii.fIcon = FALSE;  // Change fIcon to TRUE to create an alpha icon
    ii.xHotspot = 0;
    ii.yHotspot = 0;
    ii.hbmMask = hMonoBitmap;
    ii.hbmColor = hBitmap;

    // Create the alpha cursor with the alpha DIB section.
    hAlphaCursor = CreateIconIndirect(&ii);

    DeleteObject(hBitmap);          
    DeleteObject(hMonoBitmap); 

    return hAlphaCursor;
}

Sebelum menutup, Anda harus menggunakan fungsi DestroyCursor untuk menghancurkan kursor apa pun yang Anda buat dengan CreateCursor atau CreateIconIndirect. Tidak perlu menghancurkan kursor yang dibuat oleh fungsi lain.

Mengetahui ukuran Kursor

Lihat Mendapatkan ukuran Ikon.

Menampilkan Kursor

Sistem secara otomatis menampilkan kursor kelas (kursor yang terkait dengan jendela tempat kursor menunjuk). Anda dapat menetapkan kursor suatu kelas ketika mendaftarkan kelas jendela. Contoh berikut mengilustrasikan ini dengan menetapkan handel kursor ke anggota hCursor dari struktur WNDCLASS yang diidentifikasi oleh parameter wc .

WNDCLASS  wc; 
 
// Fill the window class structure with parameters that 
// describe the main window. 
 
wc.style = NULL;                        // class style(s) 
wc.lpfnWndProc = (WNDPROC) MainWndProc; // window procedure 
wc.cbClsExtra = 0;           // no per-class extra data 
wc.cbWndExtra = 0;           // no per-window extra data 
wc.hInstance = hinst;        // application that owns the class 
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);     // class icon 
wc.hCursor = LoadCursor(hinst, MAKEINTRESOURCE(230)); // class cursor 
wc.hbrBackground = GetStockObject(WHITE_BRUSH); // class background 
wc.lpszMenuName =  "GenericMenu";               // class menu 
wc.lpszClassName = "GenericWClass"              // class name 
 
// Register the window class. 
 
return RegisterClass(&wc); 

Ketika kelas jendela terdaftar, kursor yang diidentifikasi oleh 230 dalam file definisi sumber daya aplikasi adalah kursor default untuk semua jendela berdasarkan kelas .

Aplikasi Anda dapat mengubah desain kursor dengan menggunakan fungsi SetCursor dan menentukan handel kursor yang berbeda. Namun, ketika kursor bergerak, sistem menguraikan ulang kursor kelas di lokasi baru. Untuk mencegah kursor kelas digambar ulang, Anda harus memproses pesan WM_SETCURSOR . Setiap kali kursor bergerak dan input mouse tidak diambil, sistem mengirim pesan ini ke jendela tempat kursor bergerak.

Anda dapat menentukan kursor yang berbeda untuk kondisi yang berbeda saat memproses WM_SETCURSOR. Misalnya, contoh berikut menunjukkan cara menampilkan kursor setiap kali kursor berpindah di atas ikon aplikasi yang diminimalkan.

case WM_SETCURSOR: 
 
    // If the window is minimized, draw the hCurs3 cursor. 
    // If the window is not minimized, draw the default 
    // cursor (class cursor). 
 
    if (IsIconic(hwnd)) 
    { 
        SetCursor(hCurs3); 
        break; 
    } 

Ketika jendela tidak diminimalkan, sistem menampilkan kursor kategori.

Anda dapat mengganti kursor kelas dengan menggunakan fungsi SetClassLong . Fungsi ini mengubah pengaturan jendela default untuk semua jendela kelas tertentu. Contoh berikut mengganti kursor kelas yang ada dengan hCurs2 kursor.

// Change the cursor for window class represented by hwnd. 
 
SetClassLongPtr(hwnd,    // window handle 
    GCLP_HCURSOR,        // change cursor 
    (LONG_PTR) hCurs2);  // new cursor 

Untuk informasi selengkapnya, lihat Kelas Jendela dan Input Mouse.

Membatasi Kursor

Contoh berikut membatasi kursor ke jendela aplikasi lalu memulihkan kursor ke jendela sebelumnya. Contoh menggunakan fungsi GetClipCursor untuk merekam area tempat kursor dapat berpindah dan fungsi ClipCursor untuk membatasi dan memulihkan kursor.

RECT rcClip;           // new area for ClipCursor
RECT rcOldClip;        // previous area for ClipCursor
 
// Record the area in which the cursor can move. 
 
GetClipCursor(&rcOldClip); 
 
// Get the dimensions of the application's window. 
 
GetWindowRect(hwnd, &rcClip); 
 
// Confine the cursor to the application's window. 
 
ClipCursor(&rcClip); 
 
   // 
   // Process input from the confined cursor. 
   // 
 
// Restore the cursor to its previous area. 
 
ClipCursor(&rcOldClip); 

Karena hanya ada satu kursor pada satu waktu yang tersedia dalam sistem, aplikasi yang membatasi kursor harus memulihkan kursor sebelum melepaskan kontrol ke jendela lain.

Menggunakan Fungsi Kursor untuk Membuat Perangkap Mouse

Contoh berikut menggunakan fungsi SetCursorPos, GetCursorPos, CreateCursor, LoadCursor, dan SetCursor untuk membuat perangkap mouse sederhana. Ini juga menggunakan fungsi kursor dan timer untuk memantau posisi kursor setiap 10 detik. Jika posisi kursor tidak berubah dalam 10 detik terakhir dan jendela utama aplikasi diminimalkan, aplikasi mengubah kursor dan memindahkannya ke ikon perangkap mouse.

Contoh untuk perangkap tikus serupa disertakan dalam Ikon. Ini menggunakan fungsi LoadCursor dan LoadIcon alih-alih fungsi CreateCursor dan CreateIcon yang lebih bergantung pada perangkat.

HICON hIcon1;               // icon handles 
POINT ptOld;                // previous cursor location 
HCURSOR hCurs1;             // cursor handle 
 
 
// The following cursor masks are defined in a code 
// example that appears earlier in this section. 
 
// Yin-shaped cursor AND and XOR masks 
 
BYTE ANDmaskCursor[] = ... 
BYTE XORmaskCursor[] = ... 
 
// Yang-shaped icon AND mask (32x32x1bpp)
BYTE ANDmaskIcon[] =
{
    0xFF, 0xFF, 0xFF, 0xFF,   // ################################
    0xFF, 0xFF, 0xC3, 0xFF,   // ##################----##########
    0xFF, 0xFF, 0x00, 0xFF,   // ################--------########
    0xFF, 0xFE, 0x00, 0x7F,   // ###############----------#######
    0xFF, 0xFC, 0x00, 0x1F,   // ##############-------------#####
    0xFF, 0xF8, 0x00, 0x0F,   // #############---------------####
    0xFF, 0xF8, 0x00, 0x0F,   // #############---------------####
    0xFF, 0xF0, 0x00, 0x07,   // ############-----------------###
    0xFF, 0xF0, 0x00, 0x03,   // ############------------------##
    0xFF, 0xE0, 0x00, 0x03,   // ###########-------------------##
    0xFF, 0xE0, 0x00, 0x01,   // ###########--------------------#
    0xFF, 0xE0, 0x00, 0x01,   // ###########--------------------#
    0xFF, 0xF0, 0x00, 0x01,   // ############-------------------#
    0xFF, 0xF0, 0x00, 0x00,   // ############--------------------
    0xFF, 0xF8, 0x00, 0x00,   // #############-------------------
    0xFF, 0xFC, 0x00, 0x00,   // ##############------------------
    0xFF, 0xFF, 0x00, 0x00,   // ################----------------
    0xFF, 0xFF, 0x80, 0x00,   // #################---------------
    0xFF, 0xFF, 0xE0, 0x00,   // ###################-------------
    0xFF, 0xFF, 0xE0, 0x01,   // ###################------------#
    0xFF, 0xFF, 0xF0, 0x01,   // ####################-----------#
    0xFF, 0xFF, 0xF0, 0x01,   // ####################-----------#
    0xFF, 0xFF, 0xF0, 0x03,   // ####################----------##
    0xFF, 0xFF, 0xE0, 0x03,   // ###################-----------##
    0xFF, 0xFF, 0xE0, 0x07,   // ###################----------###
    0xFF, 0xFF, 0xC0, 0x0F,   // ##################----------####
    0xFF, 0xFF, 0xC0, 0x0F,   // ##################----------####
    0xFF, 0xFF, 0x80, 0x1F,   // #################----------#####
    0xFF, 0xFF, 0x00, 0x7F,   // ################---------#######
    0xFF, 0xFC, 0x00, 0xFF,   // ##############----------########
    0xFF, 0xF8, 0x03, 0xFF,   // #############---------##########
    0xFF, 0xFC, 0x3F, 0xFF    // ##############----##############
};
 
// Yang-shaped icon XOR mask (32x32x1bpp)
BYTE XORmaskIcon[] =
{
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x38, 0x00,   // ------------------###-----------
    0x00, 0x00, 0x7C, 0x00,   // -----------------#####----------
    0x00, 0x00, 0x7C, 0x00,   // -----------------#####----------
    0x00, 0x00, 0x7C, 0x00,   // -----------------#####----------
    0x00, 0x00, 0x38, 0x00,   // ------------------###-----------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00,   // --------------------------------
    0x00, 0x00, 0x00, 0x00    // --------------------------------
};

hIcon1 = CreateIcon(hinst, // handle to app. instance 
             32,           // icon width 
             32,           // icon height 
             1,            // number of XOR planes 
             1,            // number of bits per pixel 
             ANDmaskIcon,  // AND mask 
             XORmaskIcon); // XOR mask 
 
hCurs1 = CreateCursor(hinst, // handle to app. instance
             19,             // horizontal position of hot spot 
             2,              // vertical position of hot spot 
             32,             // cursor width 
             32,             // cursor height 
             ANDmaskCursor,  // AND mask 
             XORmaskCursor); // XOR mask 
 
// Fill in the window class structure. 
 
WNDCLASS  wc; 
 
wc.hIcon = hIcon1;                        // class icon 
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // class cursor 
 
//
// Register the window class and perform 
// other application initialization. 
//
 
// Set a timer for the mousetrap. 
 
GetCursorPos(&ptOld); 
 
SetTimer(hwnd, IDT_CURSOR, 10000, (TIMERPROC) NULL); 
 
LONG APIENTRY MainWndProc( 
    HWND hwnd,          // window handle 
    UINT message,       // type of message 
    UINT wParam,        // additional information 
    LONG lParam)        // additional information 
{ 
 
    HDC hdc;            // handle to device context 
    POINT pt;           // current cursor location 
    RECT rc;            // minimized window location 
 
    switch (message) 
    { 
        //
        // Process other messages. 
        // 
        case WM_TIMER: 
        // If the window is minimized, compare the 
        // current cursor position with the one 10 
        // seconds before. If the cursor position has 
        // not changed, move the cursor to the icon. 
 
            if (IsIconic(hwnd)) 
            { 
                GetCursorPos(&pt); 
 
                if ((pt.x == ptOld.x) && (pt.y == ptOld.y)) 
                { 
                    GetWindowRect(hwnd, &rc); 
                    SetCursorPos(rc.left + 20, rc.top + 4); 
 
                    // Note that the additional constants 
                    // (20 and 4) are application-specific 
                    // values to align the yin-shaped cursor 
                    // and the yang-shaped icon. 
 
                } 
                else 
                { 
                    ptOld.x = pt.x; 
                    ptOld.y = pt.y; 
                } 
            } 
 
            return 0; 
 
        case WM_SETCURSOR: 
        // If the window is minimized, draw hCurs1. 
        // If the window is not minimized, draw the 
        // default cursor (class cursor). 
 
            if (IsIconic(hwnd)) 
            { 
                SetCursor(hCurs1); 
                break; 
            } 
 
        case WM_DESTROY: 
        // Destroy timer. 
 
            KillTimer(hwnd, IDT_CURSOR); 
 
            PostQuitMessage(0); 
            break; 
    } 
} 

Menggunakan Keyboard untuk Memindahkan Kursor

Karena sistem tidak memerlukan mouse, aplikasi harus dapat mensimulasikan tindakan mouse dengan keyboard. Contoh berikut menunjukkan cara mencapai ini dengan menggunakan fungsi GetCursorPos dan SetCursorPos dan dengan memproses input dari tombol panah.

HCURSOR hCurs1, hCurs2;    // cursor handles 
 
POINT pt;                  // cursor location  
RECT rc;                   // client area coordinates 
static int repeat = 1;     // repeat key counter 
 
// 
// Other declarations and initialization. 
// 
 
switch (message) 
{ 
// 
// Process other messages. 
// 
 
    case WM_KEYDOWN: 
 
        if (wParam != VK_LEFT && wParam != VK_RIGHT && 
        wParam != VK_UP && wParam != VK_DOWN) 
        { 
            break; 
        } 
 
        GetCursorPos(&pt); 
 
        // Convert screen coordinates to client coordinates. 
 
        ScreenToClient(hwnd, &pt); 
 
        switch (wParam) 
        { 
        // Move the cursor to reflect which 
        // arrow keys are pressed. 
 
            case VK_LEFT:               // left arrow 
                pt.x -= repeat; 
                break; 
 
            case VK_RIGHT:              // right arrow 
                pt.x += repeat; 
                break; 
 
            case VK_UP:                 // up arrow 
                pt.y -= repeat; 
                break; 
 
            case VK_DOWN:               // down arrow 
                pt.y += repeat; 
                break; 
 
            default: 
                return 0; 
        } 
 
        repeat++;           // Increment repeat count. 
 
        // Keep the cursor in the client area. 
 
        GetClientRect(hwnd, &rc); 
 
        if (pt.x >= rc.right) 
        { 
            pt.x = rc.right - 1; 
        } 
        else 
        { 
            if (pt.x < rc.left) 
            { 
                pt.x = rc.left; 
            } 
        } 
 
        if (pt.y >= rc.bottom) 
            pt.y = rc.bottom - 1; 
        else 
            if (pt.y < rc.top) 
                pt.y = rc.top; 
 
        // Convert client coordinates to screen coordinates. 
 
        ClientToScreen(hwnd, &pt); 
        SetCursorPos(pt.x, pt.y); 
        return 0; 

 
    case WM_KEYUP: 
 
        repeat = 1;            // Clear repeat count. 
        return 0; 
 
}