ncurses指南——第十二章 使用鼠标部分修正版

        本人在阅读nucurses指南中,发现该指南中的第十二章中示例代码有些许错误,网上搜索修正未果,自行对其修正,花费不少时间,在此撰下此文,望后来者能免遭耽搁,提高效率。

错误:

1.缺少 keypad()函数

2.button1_pressed,而非button1_clicked(可选)

1——使用鼠标,必须要有keypad()函数,指向要进行点击的窗口,并设置为true.

2——第二点为可选,源代码上使用的是button1_pressed,需要长按左键才可看到变化,本人在此改为button1_clicked,仅用点击就可变化,更为直观,此错误可不修改。

#include <stdio.h>
#include <ncurses.h>

#define WIDTH 30

#define HEIGHT 10 

int startx = 0;

int starty = 0;

char *choices[] = {   

						"Choice 1",

            "Choice 2",

            "Choice 3",

            "Choice 4",

            "Exit",

          };

 

int n_choices = sizeof(choices) / sizeof(char *);

void print_menu(WINDOW *menu_win, int highlight);

void report_choice(int mouse_x, int mouse_y, int *p_choice);

int main()

{    

int c, choice = 0;

    WINDOW *menu_win;

    MEVENT event;


 

    initscr();

    clear();

    noecho();

    cbreak();    
 

    /*  在屏幕上输出窗口  */

    startx = (80 - WIDTH) / 2;

    starty = (24 - HEIGHT) / 2;

    

    attron(A_REVERSE);

    mvprintw(23, 1, "Click on Exit to quit");


    refresh();

    attroff(A_REVERSE);

 

    /* Print the menu for the first time */

    menu_win = newwin(HEIGHT, WIDTH, starty, startx);
		keypad(menu_win, TRUE);

    print_menu(menu_win, 1);

    /* Get all the mouse events */

    mousemask(ALL_MOUSE_EVENTS, NULL);

    

    while(1)

    {    c = wgetch(menu_win);

        switch(c)

        {    case KEY_MOUSE:


            if(getmouse(&event) == OK)

            {    /*  一旦用户按下左键  */
						

                if(event.bstate&BUTTON1_CLICKED)

                {    
    								mvprintw(22, 1, "Click on Exit to quit");
										refresh();
										report_choice(event.x + 1, event.y + 1, &choice);

                    if(choice == -1) //Exit chosen

                        goto end;

                    mvprintw(22, 1, "Choice made is : %d String Chosen is /%10s/", choice, choices[choice-1]);

                    refresh(); 

                }

            }

            print_menu(menu_win, choice);

            break;

        }

    }        

end:

    endwin();

    return 0;

}

 

void print_menu(WINDOW *menu_win, int highlight)

{

    int x, y, i;    

 

    x = 2;

    y = 2;

    box(menu_win, 0, 0);

    for(i = 0; i < n_choices; ++i)

    {    if(highlight == i + 1)

        {    wattron(menu_win, A_REVERSE); 

            mvwprintw(menu_win, y, x, "%s", choices[i]);

            wattroff(menu_win, A_REVERSE);

        }

        else

            mvwprintw(menu_win, y, x, "%s", choices[i]);

        ++y;

    }

    wrefresh(menu_win);

}

 

void report_choice(int mouse_x, int mouse_y, int *p_choice)

{    int i,j, choice;

 

    i = startx + 2;

    j = starty + 3;

    

    for(choice = 0; choice < n_choices; ++choice)

        if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice]))

        {    if(choice == n_choices - 1)

                *p_choice = -1;        

            else

                *p_choice = choice + 1;    

            break;

        }

}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值