Advertisement
Guest User

velichko mouse

a guest
Mar 28th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <conio.h>
  6. #include <cmath>
  7. using namespace std;
  8. HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
  9. HANDLE hIn=GetStdHandle(STD_INPUT_HANDLE);
  10. const int ncolors=3;
  11. int colors[ncolors]={BACKGROUND_RED|BACKGROUND_INTENSITY,BACKGROUND_GREEN|BACKGROUND_INTENSITY,BACKGROUND_BLUE|BACKGROUND_INTENSITY};
  12. int colors2[ncolors]={FOREGROUND_RED|FOREGROUND_INTENSITY,FOREGROUND_GREEN|FOREGROUND_INTENSITY,FOREGROUND_BLUE|FOREGROUND_INTENSITY};
  13.  
  14. struct Window
  15. {
  16.     char title[100];
  17.     int left,top,right,bottom;
  18. };
  19. void DrawWindow(Window w)
  20. {
  21.     COORD c;
  22.     c.X=w.left;
  23.     c.Y=w.top;
  24.     //устанавливает курсор
  25.     SetConsoleCursorPosition(hOut,c);
  26.     SetConsoleTextAttribute(hOut,FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY|BACKGROUND_BLUE);
  27.     for(int i=0; i<w.right-w.left; ++i)
  28.     {
  29.         if(i<strlen(w.title))
  30.             cout<<w.title[i];
  31.         else
  32.             cout<<' ';
  33.     }
  34.     for(int i=1; i<w.bottom-w.top; ++i)
  35.     {
  36.         SetConsoleTextAttribute(hOut,BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_RED);
  37.         c.Y=w.top+i;
  38.         SetConsoleCursorPosition(hOut,c);
  39.         for(int j=0; j<w.right-w.left; ++j)
  40.             cout<<' ';
  41.         SetConsoleTextAttribute(hOut,0);
  42.             cout<<' ';
  43.     }
  44.     c.Y=w.bottom;
  45.     ++c.X;
  46.     SetConsoleCursorPosition(hOut,c);
  47.     for(int j=0; j<w.right-w.left; ++j)
  48.             cout<<' ';
  49.     for(int i=0; i<ncolors; ++i)
  50.     {
  51.         c.X=w.right-(i+1);
  52.         c.Y=w.top;
  53.         SetConsoleCursorPosition(hOut,c);
  54.         SetConsoleTextAttribute(hOut,colors[i]);
  55.         cout<<' ';
  56.     }
  57. }
  58. void Set(Window & w, int x, int y, int width, int height, char * title)
  59. {
  60.     w.left=x;
  61.     w.right=x+width;
  62.     w.top=y;
  63.     w.bottom=y+height;
  64.     strcpy_s(w.title,100,title);
  65. }
  66.  
  67. int CheckWnd(Window * w, int count, COORD c)
  68. {
  69.     for(int i=count-1; i>-1; --i)
  70.     {
  71.         if(c.X>=w[i].left&&c.X<w[i].right&&c.Y>=w[i].top&&c.Y<w[i].bottom)
  72.         {
  73.             return i;
  74.         }
  75.     }
  76.     return -1;
  77. }
  78.  
  79. void BringToTop(Window * w, int count, int index)
  80. {
  81.     if(index<0||index>=count) return;
  82.     Window temp=w[index];
  83.     for(int i=index; i<count-1; ++i)
  84.         w[i]=w[i+1];
  85.     w[count-1]=temp;
  86. }
  87.  
  88. void Show(Window * w, int count)
  89. {
  90.     for(int i=0; i<count; ++i)
  91.     {  
  92.         DrawWindow(w[i]);
  93.     }
  94. }
  95.  
  96. void main()
  97. {
  98.     system("color a0");
  99.     srand((int)time(0));
  100.     CONSOLE_CURSOR_INFO cInfo={100,false};
  101.     DWORD l, mode;
  102.     INPUT_RECORD ir;
  103.     CONSOLE_SCREEN_BUFFER_INFO cbinf;
  104.     COORD c0={0,0};
  105.     SetConsoleCursorInfo(hOut,&cInfo);
  106.     int count=0;
  107.     Window * w=nullptr, wtemp;
  108.     int c;
  109.     GetConsoleMode(hIn,&mode);
  110.     SetConsoleMode(hIn,ENABLE_MOUSE_INPUT|ENABLE_WINDOW_INPUT);
  111.     char str[100],temp[10];
  112.     int wpos=-1;
  113.     int color=colors2[0];
  114.     do{
  115.        
  116.         while(ReadConsoleInput(hIn,&ir,1, &l))
  117.         {
  118.             if(ir.EventType==MOUSE_EVENT)
  119.             {
  120.                 if(ir.Event.MouseEvent.dwButtonState==FROM_LEFT_1ST_BUTTON_PRESSED)
  121.                 {
  122.                     c=CheckWnd(w,count,ir.Event.MouseEvent.dwMousePosition);
  123.                     if(c!=count-1)
  124.                     {
  125.                         BringToTop(w,count,c);
  126.                         DrawWindow(w[count-1]);
  127.                     }else if(c!=-1&&ir.Event.MouseEvent.dwMousePosition.Y>w[c].top)
  128.                     {
  129.                         SetConsoleTextAttribute(hOut,color|BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_RED);
  130.                         SetConsoleCursorPosition(hOut,ir.Event.MouseEvent.dwMousePosition);
  131.                         cout<<char(3);
  132.                     }else if(c!=-1&&ir.Event.MouseEvent.dwMousePosition.Y==w[c].top&&ir.Event.MouseEvent.dwMousePosition.X>=w[c].right-3)
  133.                     {
  134.                         color=colors2[w[c].right-ir.Event.MouseEvent.dwMousePosition.X-1];
  135.                     }else if(c!=-1&&ir.Event.MouseEvent.dwEventFlags!=MOUSE_MOVED)
  136.                     {
  137.                         wpos=ir.Event.MouseEvent.dwMousePosition.X-w[count-1].left;
  138.                     }
  139.                     if(ir.Event.MouseEvent.dwEventFlags==MOUSE_MOVED&&wpos!=-1)
  140.                     {
  141.                         w[count-1].top=ir.Event.MouseEvent.dwMousePosition.Y;
  142.                         w[count-1].left=ir.Event.MouseEvent.dwMousePosition.X-wpos;
  143.                         w[count-1].right=w[count-1].left+20;
  144.                         w[count-1].bottom=w[count-1].top+10;
  145.                         GetConsoleScreenBufferInfo(hIn,&cbinf);
  146.                         FillConsoleOutputAttribute(hOut,BACKGROUND_GREEN|BACKGROUND_INTENSITY,cbinf.dwSize.X*cbinf.dwSize.Y,c0,&l);
  147.                         FillConsoleOutputCharacter(hOut,' ',cbinf.dwSize.X*cbinf.dwSize.Y,c0,&l);
  148.                         Show(w,count);
  149.                         //DrawWindow(w[count-1]);
  150.                     }
  151.                 }else
  152.                 if(ir.Event.MouseEvent.dwButtonState==RIGHTMOST_BUTTON_PRESSED)
  153.                 {
  154.                     if(count) delete [] w;
  155.                     count=0;
  156.                     GetConsoleScreenBufferInfo(hIn,&cbinf);
  157.                     FillConsoleOutputAttribute(hOut,BACKGROUND_GREEN|BACKGROUND_INTENSITY,cbinf.dwSize.X*cbinf.dwSize.Y,c0,&l);
  158.                     FillConsoleOutputCharacter(hOut,' ',cbinf.dwSize.X*cbinf.dwSize.Y,c0,&l);
  159.                 }else
  160.                 {
  161.                     wpos=-1;
  162.                 }
  163.             }else
  164.             if(ir.EventType==KEY_EVENT)
  165.             {
  166.                 if(ir.Event.KeyEvent.bKeyDown)
  167.                 {
  168.                     c=ir.Event.KeyEvent.wVirtualKeyCode;
  169.                     switch(ir.Event.KeyEvent.wVirtualKeyCode)
  170.                     {
  171.                     case 9:
  172.                         wtemp=w[count-1];
  173.                         for(int i=count-1; i>0; --i)
  174.                             w[i]=w[i-1];
  175.                         w[0]=wtemp;
  176.                         Show(w,count);
  177.                         break;
  178.                     case VK_RETURN:
  179.                         Window * tempw=new Window[count+1];
  180.                         for(int i=0; i<count; ++i)
  181.                         {
  182.                             tempw[i]=w[i];
  183.                         }
  184.                         if(count) delete [] w;
  185.                         w=tempw;
  186.                         strcpy(str,"Title ");
  187.                         _itoa(count+1,temp,10);
  188.                         strcat(str,temp);
  189.                         Set(w[count++],rand()%20,rand()%10,20,10,str);
  190.                         Show(w,count);
  191.                         break;
  192.                     }
  193.                 }
  194.             }
  195.            
  196.         }
  197.     }while(c!=VK_ESCAPE);
  198.  
  199.     delete [] w;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement