• Viernes 26 de Abril de 2024, 07:05

Autor Tema:  Problema al intentar repintar control  (Leído 955 veces)

krnl64

  • Miembro activo
  • **
  • Mensajes: 72
    • Ver Perfil
Problema al intentar repintar control
« en: Martes 25 de Noviembre de 2008, 09:23 »
0
Hola a todos, tengo un problema que no sé solucionar con un control que me estoy creando.
Verán, estoy intentando hacerme un control para hacer mapas para rpg's a base de tiles.
Comencé a hacerlo como una clase que hereda de un panel y dentro otro panel que es donde dibujo una malla.
No usé el diseñador, y lo hice de esta manera porque asi el scroll me lo hace solo.
Bueno, pues hago el code, me pinta y todo muy bonito pero cuando la ventana pierde el foco, lo que he pintao dentro del 2º panel se pierde.
He intentando hookear el WndProc del panel interior, pero no se hacerlo.
Pueden decirme que puedo hacer para repintar el contenido del control ?
Les dejo aqui el code:
Código: Text
  1.  
  2.   class Grid2:Panel
  3.     {
  4.         Panel p2;
  5.                        
  6.         private int hcells;
  7.         private int vcells;
  8.        
  9.         private int theigth;
  10.         private int twidth;
  11.  
  12.         private const int WM_PAINT = 0xF;
  13.        
  14.    
  15.         public Grid2(int sizetile, int height, int width)
  16.         {
  17.             this.AutoScroll=true;
  18.             this.BorderStyle=BorderStyle.FixedSingle;
  19.             this.Width=600;
  20.             this.Height=600;
  21.            
  22.             // N1 de celdas
  23.             this.hcells=height;
  24.             this.vcells=width;
  25.            
  26.             this.twidth=sizetile;
  27.             this.theigth=sizetile;
  28.            
  29.            
  30.             p2=new Panel();
  31.            
  32.             p2.Height=this.vcells*this.theigth;
  33.             p2.Width=this.hcells*this.twidth;
  34.            
  35.             p2.Paint += new PaintEventHandler(p2_Paint);
  36.        
  37.            
  38.            
  39.             p2.MouseMove += new MouseEventHandler(p2_MouseMove);                                    
  40.             this.Controls.Add(p2);
  41.            
  42.            
  43.            
  44.            
  45.         }
  46.  
  47.         void p2_MouseMove(object sender, MouseEventArgs e)                
  48.         {
  49.        
  50.             if (e.Button==MouseButtons.Left) {
  51.        
  52.                     int x;
  53.                     int y;
  54.                    
  55.                     Panel p;
  56.                     Pen pn=new Pen(Color.Green,2);
  57.                     Graphics g;
  58.                    
  59.                         x=(e.X/32); // Averiguo en que casilla esta el raton
  60.                         y=(e.Y/32);
  61.                        
  62.                         g=((Panel)sender).CreateGraphics();
  63.                        
  64.                         // DrawRectangle (x,y(origen),ancho, alto)
  65.                         g.DrawRectangle(pn,new Rectangle(x*this.twidth,y*this.theigth,this.twidth,this.theigth));
  66.                      
  67.                         g.Dispose();
  68.             }
  69.         }
  70.        
  71.         #region Propiedades
  72.        
  73.         /// <summary>
  74.         /// Nº de celdas de ancho
  75.         /// </summary>
  76.         public int Hcells
  77.         {
  78.             get
  79.             {
  80.                 return(this.hcells);
  81.             }
  82.             set
  83.             {
  84.                 this.hcells=value;
  85.             }
  86.         }
  87.        
  88.        
  89.         /// <summary>
  90.         /// Nº de celdas de alto
  91.         /// </summary>
  92.         public int Vcells
  93.         {
  94.             get
  95.             {
  96.                 return(this.vcells);
  97.             }
  98.            
  99.             set
  100.             {  
  101.                 this.vcells=value;
  102.             }
  103.         }
  104.        
  105.        
  106.         /// <summary>
  107.         /// Alto del tile
  108.         /// </summary>
  109.         public int Theigth
  110.         {
  111.             get
  112.             {
  113.                 return(this.theigth);
  114.             }
  115.            
  116.             set
  117.             {
  118.                 this.theigth=value;
  119.             }            
  120.         }
  121.        
  122.         /// <summary>
  123.         /// Ancho del tile
  124.         /// </summary>
  125.         public int Twidth
  126.         {
  127.             get
  128.             {
  129.                 return(this.twidth);
  130.             }
  131.            
  132.             set
  133.             {
  134.                 this.twidth=value;
  135.             }
  136.         }
  137.        
  138.         #endregion
  139.  
  140.         void p2_Paint(object sender, PaintEventArgs e)
  141.         {
  142.                      
  143.             int x;
  144.             int y;
  145.        
  146.      
  147.            
  148.             Pen mypen=new Pen(Color.Gray);
  149.            
  150.                 // lineas verticales
  151.                  for (x=0;x<this.hcells*this.twidth; x+=this.twidth)
  152.                     e.Graphics.DrawLine(mypen,x,0,x, this.hcells*this.twidth);              
  153.              
  154.                 // lineas horizontales                        
  155.                for (y = 0; y < this.vcells * this.theigth; y += this.theigth)
  156.                     e.Graphics.DrawLine(mypen, 0, y, this.hcells * this.theigth, y);
  157.                                                                                
  158.                    e.Graphics.DrawRectangle(mypen, 1, 1, this.theigth * this.hcells - 2, this.twidth * this.vcells - 2);
  159.                    
  160.                
  161.         }
  162.        
  163.     }
  164. }
  165.  
  166.  

Gracias