C++ 小组界面 响应鼠标事件的二级菜单(附源码)

/*********************************************/
/* 程序功能:响应鼠标事件的二级菜单(下拉式)*/
/* 环境:VC6.0+EasyX                         */
/* 作者:nightchen                        */
/* 日期:2015-04-13                          */
/*********************************************/
#include <graphics.h>
#include <conio.h>
#include <time.h>
#define W 80   //菜单项矩形宽度
#define H 30    //菜单项矩形高度
#define OX 130 //菜单左上角起始点x坐标
#define OY 80 //菜单左上角起始点y坐标
#pragma comment ( lib , "winmm.lib" ) //背景音乐所需

void init (); //界面初始化
void cd_main (); //画主菜单
void cd1 (); //画子菜单1
void cd2 (); //画子菜单2
void cd3 (); //画子菜单2
void cd1_clear (); //清除子菜单1
void cd2_clear (); //清除子菜单2
void cd3_clear (); //清除子菜单3
void loca_mouse (); //获取鼠标所在菜单标号

int t1_x = OX,t2_x = OX + W + 5 ,t3_x = OX + 2 * (W + 5 ),t4_x = OX + 3 * (W + 5 ),t5_x = OX + 4 * (W + 5 );
//主菜单四个菜单项左边x坐标,菜单项间距为5个象素
int isSub1 = 0 ; //子菜单1是否已下拉
int isSub2 = 0 ; //子菜单2是否已下拉
int isSub3 = 0 ; //子菜单3是否已下拉
int ml; //鼠标所在菜单标号
MOUSEMSG m;

void main ()
{
    init ();
    cd_main ();
        IMAGE bj;
     loadimage ( & bj, "./fire/bj.jpg" , 1920 , 1020 );
     putimage ( 0 , 0 , & bj);

    PlaySound ( "zpxj.WAV" , NULL ,SND_ASYNC);   //背景音乐
    while ( true )
    {  
         loca_mouse ();   //获取鼠标所在的菜单编号
         switch (ml)
        {
         case 0
            { cd_main (); cd1_clear (); cd2_clear (); cd3_clear ();
             break ;}
         case 1 :
            { cd_main ();
             if (isSub1 == 1 ) cd1 ();
             cd2_clear ();
             cd3_clear ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t1_x,OY,t1_x + W,OY + H);
             setcolor (BLUE);
             outtextxy (t1_x + 20 ,OY + 8 , "开 始" );
             if (m. uMsg == WM_LBUTTONDOWN)
               cd1 ();
             break ;}        
         case 2 :
            { cd_main ();
             if (isSub2 == 1 ) cd2 ();
             cd1_clear ();
             cd3_clear ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t2_x,OY,t2_x + W,OY + H);
             setcolor (BLUE);
             outtextxy (t2_x + 20 ,OY + 8 , "程 序" );
             if (m. uMsg == WM_LBUTTONDOWN)
                cd2 ();
             break ;}
         case 3 :
            { cd_main ();
             if (isSub3 == 1 ) cd3 ();
             cd1_clear ();
             cd2_clear ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t3_x,OY,t3_x + W,OY + H);
             setcolor (BLUE);
             outtextxy (t3_x + 20 ,OY + 8 , "帮 助" );
             if (m. uMsg == WM_LBUTTONDOWN)
               cd3 ();
             break ;}
         case 4 :
            { cd_main ();
             cd1_clear ();
             cd2_clear ();
             cd3_clear ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t4_x,OY,t4_x + W,OY + H);
             setcolor (BLUE);
             outtextxy (t4_x + 20 ,OY + 8 , "其 他" );
             if (m. uMsg == WM_LBUTTONDOWN);
             break ;}
         case 5 :
            { cd_main ();
             cd1_clear ();
             cd2_clear ();
             cd3_clear ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t5_x,OY,t5_x + W,OY + H);
             setcolor (BLUE);
             outtextxy (t4_x + 20 ,OY + 8 , "拓 展" );
             if (m. uMsg == WM_LBUTTONDOWN);
             break ;}
         case 11 :
             if (isSub1 == 1 )
            {
             cd_main ();  cd1 ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t1_x,OY + 5 + H,t1_x + W,OY + 5 + 2 * H);          
             setcolor (BLUE);
             outtextxy (t1_x + 20 ,OY + 5 + H + 8 , "欢 迎" );
             if (m. uMsg == WM_LBUTTONUP)
             { cd_main (); cd1_clear (); cd2_clear (); cd3_clear ();
              system ( "下拉菜单.doc" );            
             }
            }
             break ;
         case 12 :
             if (isSub1 == 1 )
            {
             cd_main ();  cd1 ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t1_x,OY + 5 + 2 * H,t1_x + W,OY + 5 + 3 * H);            
             setcolor (BLUE);
             outtextxy (t1_x + 20 ,OY + 5 + 2 * H + 8 , "退 出" );
             if (m. uMsg == WM_LBUTTONUP)
                 exit ( 0 );
            }
             break ;
         case 13 :
             if (isSub1 == 1 )
            {
             cd_main ();  cd1 ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t1_x,OY + 5 + 3 * H,t1_x + W,OY + 5 + 4 * H);            
             setcolor (BLUE);
             outtextxy (t1_x + 20 ,OY + 5 + 3 * H + 8 , "" );
             if (m. uMsg == WM_LBUTTONUP)
                 system ( NULL );
            }
             break ;
         case 21 :
             if (isSub2 == 1 )
            {
             cd_main ();  cd2 ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t2_x,OY + 5 + H,t2_x + W,OY + 5 + 2 * H);          
             setcolor (BLUE);
             outtextxy (t2_x + 20 ,OY + 5 + H + 8 , "name1" );
             if (m. uMsg == WM_LBUTTONUP)
                 system ( "wt.txt" );
            }
             break ;
         case 22 :
             if (isSub2 == 1 )
            {
             cd_main ();  cd2 ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t2_x,OY + 5 + 2 * H,t2_x + W,OY + 5 + 3 * H);            
             setcolor (BLUE);
             outtextxy (t2_x + 20 ,OY + 5 + 2 * H + 8 , "name2" );
             if (m. uMsg == WM_LBUTTONUP)
                 system ( "ty.txt" );
            }
             break ;
         case 23 :
             if (isSub2 == 1 )
            {
             cd_main ();  cd2 ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t2_x,OY + 5 + 3 * H,t2_x + W,OY + 5 + 4 * H);            
             setcolor (BLUE);
             outtextxy (t2_x + 20 ,OY + 5 + 3 * H + 8 , "name3" );
             if (m. uMsg == WM_LBUTTONUP)
                 system ( "syy.txt" );
            }
             break ;
         case 24 :
             if (isSub2 == 1 )
            {
             cd_main ();  cd2 ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t2_x,OY + 5 + 4 * H,t2_x + W,OY + 5 + 5 * H);            
             setcolor (BLUE);
             outtextxy (t2_x + 20 ,OY + 5 + 4 * H + 8 , "name4" );
             if (m. uMsg == WM_LBUTTONUP)
                 system ( "main.exe" );
            }
             break ;
         case 31 :
            if (isSub3 == 1 )
            {
             cd_main ();  cd3 ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t3_x,OY + 5 + H,t3_x + W,OY + 5 + 2 * H);          
             setcolor (BLUE);
             outtextxy (t3_x + 20 ,OY + 5 + H + 8 , "关 于" );
             if (m. uMsg == WM_LBUTTONUP)
                 system ( NULL );
            }
             break ;
         case 32 :
            if (isSub3 == 1 )
            {
             cd_main ();  cd3 ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t3_x,OY + 5 + 2 * H,t3_x + W,OY + 5 + 3 * H);            
             setcolor (BLUE);
             outtextxy (t3_x,OY + 5 + 2 * H + 8 , "" );
             if (m. uMsg == WM_LBUTTONUP)
                 system ( NULL );
            }
             break ;
         case 33 :
             if (isSub3 == 1 )
            {
             cd_main ();  cd3 ();
             setfillcolor ( RGB ( 180 , 180 , 180 ));
             bar (t3_x,OY + 5 + 3 * H,t3_x + W,OY + 5 + 4 * H);            
             setcolor (BLUE);
             outtextxy (t3_x,OY + 5 + 3 * H + 8 , "" );
             if (m. uMsg == WM_LBUTTONUP)
                 system ( NULL );
            }
             break ;  
        }
}
        closegraph ();
}

void init ()
{
     initgraph ( 600 , 400 );

     setbkcolor ( RGB ( 240 , 240 , 240 ));
     setbkmode (TRANSPARENT); 
     cleardevice (); //以背景色清屏
     setfillstyle (BS_SOLID); 
}

void cd_main () //画主菜单,四项
{


    setfillcolor ( RGB ( 240 , 240 , 240 ));
     setcolor ( RGB ( 180 , 180 , 180 ));
     rectangle (OX - 5 ,OY - 5 ,OX + W * 4 + 20 ,OY + H + 5 );  
     fillrectangle (t1_x,OY,t1_x + W,OY + H);
     fillrectangle (t2_x,OY,t2_x + W,OY + H);
     fillrectangle (t3_x,OY,t3_x + W,OY + H);
     fillrectangle (t4_x,OY,t4_x + W,OY + H);
     fillrectangle (t5_x,OY,t5_x + W,OY + H);
     setcolor (BLUE);
     outtextxy (t1_x + 20 ,OY + 8 , "开 始" );
     outtextxy (t2_x + 20 ,OY + 8 , "程 序" );
     outtextxy (t3_x + 20 ,OY + 8 , "帮 助" );
     outtextxy (t4_x + 20 ,OY + 8 , "其 他" );
     outtextxy (t5_x + 20 ,OY + 8 , "拓 展" );
}

void cd1 () //画子菜单1,三个菜单项
{
     setcolor ( RGB ( 180 , 180 , 180 ));
     setfillcolor ( RGB ( 240 , 240 , 240 ));
     fillrectangle (t1_x,OY + 5 + H,t1_x + W,OY + 5 + 2 * H);
     fillrectangle (t1_x,OY + 5 + 2 * H,t1_x + W,OY + 5 + 3 * H);
     fillrectangle (t1_x,OY + 5 + 3 * H,t1_x + W,OY + 5 + 4 * H);
     setcolor (BLUE);
     outtextxy (t1_x + 20 ,OY + 5 + H + 8 , "欢 迎" );
     outtextxy (t1_x + 20 ,OY + 5 + 2 * H + 8 , "退 出" );
     outtextxy (t1_x + 20 ,OY + 5 + 3 * H + 8 , "" );
    isSub1 = 1 ;
}
void cd1_clear () //清除子菜单1
{
     setfillcolor ( RGB ( 240 , 240 , 240 ));
     bar (t1_x,OY + 5 + H,t1_x + W,OY + 5 + 4 * H);
     setcolor ( RGB ( 180 , 180 , 180 ));
     rectangle (OX - 5 ,OY - 5 ,OX + W * 4 + 20 ,OY + H + 5 ); //清除子菜单后,补画主菜单边框  
    isSub1 = 0 ;
}

void cd2 () //画子菜单2
{   
     setcolor ( RGB ( 180 , 180 , 180 ));
     setfillcolor ( RGB ( 240 , 240 , 240 ));
     fillrectangle (t2_x,OY + 5 + H,t2_x + W,OY + 5 + 2 * H);
     fillrectangle (t2_x,OY + 5 + 2 * H,t2_x + W,OY + 5 + 3 * H);
     fillrectangle (t2_x,OY + 5 + 3 * H,t2_x + W,OY + 5 + 4 * H);
     fillrectangle (t2_x,OY + 5 + 4 * H,t2_x + W,OY + 5 + 5 * H);
     setcolor (BLUE);
     outtextxy (t2_x + 20 ,OY + 5 + H + 8 , "name1" );
     outtextxy (t2_x + 20 ,OY + 5 + 2 * H + 8 , "name2" );
     outtextxy (t2_x + 20 ,OY + 5 + 3 * H + 8 , "namen3" );
     outtextxy (t2_x + 20 ,OY + 5 + 4 * H + 8 , "name4" );
    isSub2 = 1 ;
}
void cd2_clear () //清除子菜单2
{
     setfillcolor ( RGB ( 240 , 240 , 240 ));
     bar (t2_x,OY + 5 + H,t2_x + W,OY + 5 + 5 * H);
     setcolor ( RGB ( 180 , 180 , 180 ));
     rectangle (OX - 5 ,OY - 5 ,OX + W * 5 + 20 ,OY + H + 5 );
    isSub2 = 0 ;
}

void cd3 () //画子菜单3
{
     setfillcolor ( RGB ( 240 , 240 , 240 ));
     setcolor ( RGB ( 180 , 180 , 180 ));
     fillrectangle (t3_x,OY + 5 + H,t3_x + W,OY + 5 + 2 * H);
     fillrectangle (t3_x,OY + 5 + 2 * H,t3_x + W,OY + 5 + 3 * H);
     fillrectangle (t3_x,OY + 5 + 3 * H,t3_x + W,OY + 5 + 4 * H);
     setcolor (BLUE);
     outtextxy (t3_x + 20 ,OY + 5 + H + 8 , "关 于" );
     outtextxy (t3_x + 20 ,OY + 5 + 2 * H + 8 , "" );
     outtextxy (t3_x + 20 ,OY + 5 + 3 * H + 8 , "" );
    isSub3 = 1 ;
}
void cd3_clear () //清除子菜单3
{
     setfillcolor ( RGB ( 240 , 240 , 240 ));
     bar (t3_x,OY + 5 + H,t3_x + W,OY + 5 + 4 * H);
     setcolor ( RGB ( 180 , 180 , 180 ));
     rectangle (OX - 5 ,OY - 5 ,OX + W * 4 + 20 ,OY + H + 5 );
    isSub3 = 0 ;
}

void loca_mouse () //  获取鼠标所在菜单标号,主菜单编号为1、2、3、4,子菜单编号为11、12、13、21、22、23、31、32、33
{
    m = GetMouseMsg ();
     if (m. x >= t1_x && m. x <= t1_x + W)
    {
         if (m. y >= OY && m. y <= OY + H)
            ml = 1 ;
         else if (m. y >= OY + 5 + H && m. y <= OY + 5 + 2 * H)
            ml = 11 ;
         else if (m. y >= OY + 5 + 2 * H && m. y <= OY + 5 + 3 * H)
            ml = 12 ;
         else if (m. y >= OY + 5 + 3 * H && m. y <= OY + 5 + 4 * H)
            ml = 13 ;
    }       
     else if (m. x >= t2_x && m. x <= t2_x + W)
    {
         if (m. y >= OY && m. y <= OY + H)
            ml = 2 ;
         else if (m. y >= OY + 5 + H && m. y <= OY + 5 + 2 * H)
            ml = 21 ;
         else if (m. y >= OY + 5 + 2 * H && m. y <= OY + 5 + 3 * H)
            ml = 22 ;
         else if (m. y >= OY + 5 + 3 * H && m. y <= OY + 5 + 4 * H)
            ml = 23 ;
         else if (m. y >= OY + 5 + 4 * H && m. y <= OY + 5 + 5 * H)
            ml = 24 ;
    }       
     else if (m. x >= t3_x && m. x <= t3_x + W)
    {
         if (m. y >= OY && m. y <= OY + H)
            ml = 3 ;
         else if (m. y >= OY + 5 + H && m. y <= OY + 5 + 2 * H)
            ml = 31 ;
         else if (m. y >= OY + 5 + 2 * H && m. y <= OY + 5 + 3 * H)
            ml = 32 ;
         else if (m. y >= OY + 5 + 3 * H && m. y <= OY + 5 + 4 * H)
            ml = 33 ;
    }       
     else if (m. x >= t4_x && m. x <= t4_x + W && m. y >= OY && m. y <= OY + H)
    {
         if (m. y >= OY && m. y <= OY + H)
            ml = 4 ;
         else if (m. y >= OY + 5 + H && m. y <= OY + 5 + 2 * H)
            ml = 41 ;
         else if (m. y >= OY + 5 + 2 * H && m. y <= OY + 5 + 3 * H)
            ml = 42 ;
         else if (m. y >= OY + 5 + 3 * H && m. y <= OY + 5 + 4 * H)
            ml = 43 ;
    }
     else if (m. x >= t5_x && m. x <= t5_x + W && m. y >= OY && m. y <= OY + H)
    {
         if (m. y >= OY && m. y <= OY + H)
            ml = 5 ;
    }
     else
        ml = 0 ;
}

猜你喜欢

转载自blog.csdn.net/nightchenright/article/details/80867135
今日推荐