2013-01-02 10 views
5

Ho creato un programma di test che dovrebbe cambiare il backcolor di un pannello avanti e indietro ripetutamente sotto Linux (PCLinuxOS), ma in realtà non funziona molto bene. O aggiorna solo il backcolor dei pannelli solo quando si fa clic su qualcosa o si passa il mouse su una winform, quindi si interrompe o il programma si arresta completamente dopo aver funzionato per poco tempo.Perché il pannello winform si aggiorna solo quando si passa il mouse o si fa clic su?

Ecco come la winform guarda con 2 pannelli, un pulsante e un timer:

enter image description here

ecco il codice dietro di esso:

namespace TestIndicator; 

interface 

uses 
    System.Drawing, 
    System.Collections, 
    System.Collections.Generic, 
    System.Windows.Forms, 
    System.ComponentModel; 

type 
    /// <summary> 
    /// Summary description for MainForm. 
    /// </summary> 
    MainForm = partial class(System.Windows.Forms.Form) 
    private 
    method d_Click(sender: System.Object; e: System.EventArgs); 
    method timer1_Tick(sender: System.Object; e: System.EventArgs); 
    protected 
    method Dispose(disposing: Boolean); override; 
    public 
    constructor; 
    end; 

var 
    TurnOnRx, TurnOnTx:Boolean; 

implementation 

{$REGION Construction and Disposition} 
constructor MainForm; 
begin 
    // 
    // Required for Windows Form Designer support 
    // 
    InitializeComponent(); 

    // 
    // TODO: Add any constructor code after InitializeComponent call 
    // 
    TurnOnRx := true; 
    TurnOnTx := true; 
end; 

method MainForm.Dispose(disposing: Boolean); 
begin 
    if disposing then begin 
    if assigned(components) then 
     components.Dispose(); 

    // 
    // TODO: Add custom disposition code here 
    // 
    end; 
    inherited Dispose(disposing); 
end; 
{$ENDREGION} 

method MainForm.d_Click(sender: System.Object; e: System.EventArgs); 
begin 
    timer1.Enabled := not timer1.Enabled; 
end; 

method MainForm.timer1_Tick(sender: System.Object; e: System.EventArgs); 
begin 
    if TurnOnTx then 
    begin 
     TurnOnTx:=false; 
     TxLight.BackColor := Color.Red; 
    end 
    else 
    begin 
     TurnOnTx:=true; 
     TxLight.BackColor := Color.black; 
    end; 

    if TurnOnRx then 
    begin 
     TurnOnRx := false; 
     RxLight.BackColor := Color.Lime; 
    end 
    else 
    begin 
     TurnOnRx := true; 
     RxLight.BackColor := Color.Black; 
    end; 
end; 

end. 
+0

Funziona solo quando si fa clic sul modulo perché si sta abilitando il timer solo con il gestore dei clic. Se si desidera che i pannelli lampino immediatamente, abilitare (o avviare) il timer nel costruttore. E qual è l'errore che si ottiene con il mouse? Non vedo alcun gestore di mouse ovunque. In ogni caso .. – nawfal

+0

@nawfal, volevo dire una volta che il timer è stato avviato o attivato facendo clic sul pulsante, il pannello backcolor non si aggiorna ma solo quando si sposta il puntatore del mouse su un pulsante e/oppure fare clic sul pulsante o sulla barra degli strumenti di Winform anche se il timer è abilitato. Altre volte non fa nulla. Tuttavia, posso prendere questo stesso programma ed eseguirlo su Windows funziona come previsto. – ThN

+0

Tuttavia, posso prendere questo stesso programma ed eseguirlo su Windows funziona come previsto. Sì, non ho alcun evento di mouse sopra. Il programma agisce come se inviasse il flag del messaggio WM_Paint solo per ridisegnare o aggiornare quando si esegue qualcosa al winform. – ThN

risposta

0

Forse timer di intervallo è troppo breve e i colori cambiano troppo velocemente?

namespace TestIndicator; 
interface 
uses 
    System.Drawing, 
    System.Collections, 
    System.Collections.Generic, 
    System.Windows.Forms, 
    System.ComponentModel; 

type 
    /// <summary> 
    /// Summary description for MainForm. 
    /// </summary> 
    MainForm = partial class(System.Windows.Forms.Form) 
    private 
    method d_Click(sender: System.Object; e: System.EventArgs); 
    method timer1_Tick(sender: System.Object; e: System.EventArgs); 
    protected 
    method Dispose(disposing: Boolean); override; 
    public 
    constructor; 
    end; 

var 
    TurnOnRx, TurnOnTx:Boolean; 

implementation 

{$REGION Construction and Disposition} 
constructor MainForm; 
begin 
    // 
    // Required for Windows Form Designer support 
    // 
    InitializeComponent(); 

    // 
    // TODO: Add any constructor code after InitializeComponent call 
    // 
    TurnOnRx := true; 
    TurnOnTx := true; 

    timer1.Inverval := 1000; 
end; 

method MainForm.Dispose(disposing: Boolean); 
begin 
    if disposing then begin 
    if assigned(components) then 
     components.Dispose(); 

    // 
    // TODO: Add custom disposition code here 
    // 
    end; 
    inherited Dispose(disposing); 
end; 
{$ENDREGION} 

method MainForm.d_Click(sender: System.Object; e: System.EventArgs); 
begin 
    timer1.Enabled := not timer1.Enabled; 
end; 

method MainForm.timer1_Tick(sender: System.Object; e: System.EventArgs); 
begin 
    if TurnOnTx then 
    begin 
     TurnOnTx:=false; 
     TxLight.BackColor := Color.Red; 
    end 
    else 
    begin 
     TurnOnTx:=true; 
     TxLight.BackColor := Color.black; 
    end; 

    if TurnOnRx then 
    begin 
     TurnOnRx := false; 
     RxLight.BackColor := Color.Lime; 
    end 
    else 
    begin 
     TurnOnRx := true; 
     RxLight.BackColor := Color.Black; 
    end; 

    TxLight.Refresh(); 
    RxLight.Refresh(); 

    Application.DoEvents(); 
end; 

end. 
Problemi correlati