[C++ Builder] KeyPress ne fonctionne pas.
Bonjour ou bonsoir !
Voilà, je passe directement à mon soucis.
Explication : Mon TP consiste à faire un champ miné, soit, un démineur mais au lieu que se soit la souris, c'est le clavier que l'on prend en compte.
Pour se faire, je fais un tableau d'image dans un GroupBox, un tableau de 12 x 12.
Je démarre en bas à gauche, position [11][0]. Lorsque je clique sur l'un des chiffre du clavier numérique, je dois avoir une image d'un bonhomme en [10][0] CEPENDANT, ma fonction ne veut pas fonctionner, pourquoi ? =S
Sauf que là j'ai tester avec un message windows, normalement sa devrais fonctionner mais aucun message ne s'affiche
Voici mon code .cpp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UTP_071.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Form1->KeyPreview = true;
Form1->Label1->Caption = "Entrez le nombre de monstres :";
Form1->Label1->Top = 384;
Form1->Label1->Left = 100;
Form1->Edit1->Top = 380;
Form1->Edit1->Left = 312;
Form1->Edit1->Text = "20";
Form1->GroupBox1->Top = 8;
Form1->GroupBox1->Left = 16;
Form1->GroupBox1->Width = 470;
Form1->GroupBox1->Height = 470;
Form1->Memo1->Top = 56;
Form1->Memo1->Left = 41;
Form1->Memo1->Width = 415;
Form1->Memo1->Height = 288;
Form1->Memo1->Alignment = taCenter;
Form1->Memo1->ReadOnly = true;
Form1->Button1->Top = 420;
Form1->Button1->Left = 208;
Form1->Button1->Caption = "Commencer";
Form1->Button1->Height = 25;
Form1->Label2->Caption = "Nombre de coup : 0";
Form1->Label2->Top = 528;
Form1->Label2->Left = 24;
Form1->Label2->Visible = false;
Form1->Label3->Caption = "Vous êtes à coté de 0 monstres";
Form1->Label3->Top = 592;
Form1->Label3->Left = 75;
Form1->Label3->Visible = false;
Form1->Label4->Caption = "Touche de déplacement";
Form1->Label4->Top = 504;
Form1->Label4->Left = 320;
Form1->Label4->Visible = false;
Form1->Image1->Stretch = true;
Form1->Image1->Visible = false;
Form1->Image1->Width = 153;
Form1->Image1->Height = 153;
Form1->Image1->Top = 528;
Form1->Image1->Left = 312;
for(int i = 0; i < 12; i++)
{
for(int j = 0; j < 12; j++)
{
TerrainJeu[i][j]= 0 ;
GrilleJeu[i][j] = new TImage(GroupBox1);
GrilleJeu[i][j]->Parent= GroupBox1;
GrilleJeu[i][j]->Picture->LoadFromFile("C:\\Users\\Mathieu\\Desktop\\Img_Carre.BMP");
GrilleJeu[i][j]->Stretch = true ;
GrilleJeu[i][j]->Height = 40 ;
GrilleJeu[i][j]->Width = 40 ;
GrilleJeu[i][j]->Top = (39 * i) ;
GrilleJeu[i][j]->Left = (39 * j) ;
GrilleJeu[i][j]->Visible = false ;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form1->Label1->Visible = false;
Form1->Memo1->Visible = false;
Form1->Edit1->Visible = false;
Form1->Button1->Visible = false;
Form1->Label2->Visible = true;
Form1->Label3->Visible = true;
Form1->Label4->Visible = true;
Form1->Image1->Visible = true;
nbmonstres = StrToInt(Form1->Edit1->Text);
int Posx = 0;
int Posy = 11;
for(int i = 0 ; i<12 ; i++)
{
for(int j = 0 ; j<12 ; j++)
{
GrilleJeu[i][j]->Visible = true ;
}
}
GrilleJeu[Posy][Posx]->Picture->LoadFromFile("C:\\Users\\Mathieu\\Desktop\\Img_Coureur2.bmp");
randomize();
for(int k = 0 ; k <= nbmonstres ; k++)
{
int randi = random(12);
int randj = random(12);
TerrainJeu[randi][randj]= 9;
}
//void MessageRadar(void); Affiche le nmbre de mines autour du joueur
AfficheMines();
}
//---------------------------------------------------------------------------
//affiche les mines du tableaiu
void TForm1::AfficheMines(void)
{
for(int i = 0 ; i<12 ; i++)
{
for(int j = 0 ; j<12 ; j++)
{
if(TerrainJeu[i][j] == 9)
{
GrilleJeu[i][j]->Picture->LoadFromFile("C:\\Users\\Mathieu\\Desktop\\Img_Carre2.BMP");
GrilleJeu[i][j]->Stretch = true;
}
}
}
}
//----------------------------------------------------------------------------
void __fastcall TForm1::FormKeyPress(TObject *Sender, char &Key)
{
if( Key == VK_NUMPAD2)
{
ShowMessage("You pressed Enter");
}
}
//---------------------------------------------------------- |
J'ai bien déclarer dans mon .h la fonction :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| //---------------------------------------------------------------------------
#ifndef UTP_071H
#define UTP_071H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // Composants gérés par l'EDI
TGroupBox *GroupBox1;
TMemo *Memo1;
TLabel *Label1;
TEdit *Edit1;
TButton *Button1;
TLabel *Label2;
TLabel *Label3;
TLabel *Label4;
TImage *Image1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormKeyPress(TObject *Sender, char &Key);
private: // Déclarations de l'utilisateur
public: // Déclarations de l'utilisateur
__fastcall TForm1(TComponent* Owner);
void AfficheMines(void);
void MessageRadar(void);
TImage * GrilleJeu[12][12];
char TerrainJeu[12][12];
char nbmonstres;
int nbcoup, PosX, PosY;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif |
Toute aide est bonne à prendre , même s'il s'agit d'un algo.
Même si je travaille en c++, tout peut etre adopter en Programmation Objet je pense xD
Merci de votre aide,
Tubasa