[WinCE] Création d'un contrôle graphique + courbe
Bonjour @ tous les développeurs,
Sujet du jour : la création d'un custom controle pour permettre l'affichage d'une courbe.
Contraintes : Développement en MFC pour Windows CE, IDE eMbedded Visual C++ 4.0.
Détails :
- Pour les besoins d'une application de visualisation d'une acquisition, j'ai besoin d'afficher des points sur un graphique.
- Création d'un Custom Control OK
- Ajout du Custom Control dans le projet OK
- Affichage du Custom Control OK
- Implementation du Custom Control .... Pas OK --> KO.
Explications :
- J'ai défini plusieurs variable membres pour les paramètres (Unités, Echelle, Taille, Couleur)
- Ensuite j'ai défini 2 CDC, un pour la grille et les éléments de celle-ci (m_dcGrid), et l'autre pour la courbe (m_dcPlot).
Il est évident que le m_dcPlot est plus petit que le m_dcGrid.
- Quand je place la grille sur mon dcGrid elle s'affiche parfaitement, cependant les points du dcPlot disparraissent ... c'est génant.
- J'ai alors cherché du coté du BitBlt entre les valeurs SRCAND, SRCPAINT, SRCCOPY ... mais apparament ça ne vient pas de là (si je change rectangle noir apparait)
- J'ai beau cherché partout, je retombe toujours sur les memes explications floues, Je ne souhaite pas faire quelques choses de compliqué, juste afficher des pixels en guise de courbe ! (SetPixel)
- Un dessin de que je souhaite faire et du code vous feront surement mieu comprendre :
http://crossrobotik.free.fr/GraphType.JPG
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
|
void CGraph::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CDC memDC ;
CBitmap memBitmap ;
CBitmap* oldBitmap ;
memDC.CreateCompatibleDC(&dc) ;
memBitmap.CreateCompatibleBitmap(&dc, m_nClientWidth, m_nClientHeight) ;
oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap) ;
if (memDC.GetSafeHdc() != NULL)
{
memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight, &m_dcGrid, 0, 0, SRCCOPY) ;
memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight, &m_dcPlot, 0, 0, SRCPAINT) ;
dc.BitBlt(0, 0, m_nClientWidth, m_nClientHeight, &memDC, 0, 0, SRCCOPY) ;
}
memDC.SelectObject(oldBitmap) ;
// Do not call CWnd::OnPaint() for painting messages
} |
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
|
void CGraph::InvalidateCtrl()
{
CClientDC dc(this) ;
CPen *oldPen ;
CPen solidPen(PS_SOLID, 0, m_crGridColor) ;
if (m_dcGrid.GetSafeHdc() == NULL)
{
m_dcGrid.CreateCompatibleDC(&dc) ;
m_bitmapGrid.CreateCompatibleBitmap(&dc, m_nClientWidth, m_nClientHeight) ;
m_bitmapOldGrid = m_dcGrid.SelectObject(&m_bitmapGrid) ;
}
m_dcGrid.SetBkColor (m_crBackColor) ;
m_dcGrid.FillRect(m_rectClient, &m_brushBack) ;
m_dcGrid.SetTextColor (m_crGridColor) ;
oldPen = m_dcGrid.SelectObject (&solidPen) ;
m_dcGrid.MoveTo (m_rectPlot.left-1, m_rectPlot.top-1) ;
m_dcGrid.LineTo (m_rectPlot.right+1, m_rectPlot.top-1) ;
m_dcGrid.LineTo (m_rectPlot.right+1, m_rectPlot.bottom+1) ;
m_dcGrid.LineTo (m_rectPlot.left-1, m_rectPlot.bottom+1) ;
m_dcGrid.LineTo (m_rectPlot.left-1, m_rectPlot.top-1) ;
m_dcGrid.SelectObject (oldPen) ;
//InvalidateRect(m_rectClient) ;
if (m_dcPlot.GetSafeHdc() == NULL)
{
m_dcPlot.CreateCompatibleDC(&dc) ;
m_bitmapPlot.CreateCompatibleBitmap(&dc, m_nPlotWidth, m_nPlotHeight) ;
m_bitmapOldPlot = m_dcPlot.SelectObject(&m_bitmapPlot) ;
}
m_dcPlot.SetBkColor (m_crBackColor);
//m_dcPlot.FillRect(m_rectPlot, &m_brushBack);
//CPen solidPenPlot(PS_SOLID, 0, m_crPlotColor) ;
//oldPen = m_dcPlot.SelectObject (&solidPenPlot);
m_dcPlot.SetPixel(120,20,m_crGridColor);
m_dcPlot.SetPixel(120,21,m_crGridColor);
m_dcPlot.SetPixel(121,20,m_crGridColor);
m_dcPlot.SetPixel(121,21,m_crGridColor);
//m_dcPlot.SelectObject (oldPen) ;
//InvalidateRect(m_rectPlot) ;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| void CGraph::DrawPlot(int x, int y, COLORREF color)
{
if (x >= this->m_XMax)
{
this->m_XMax = x+1;
}
if (y >= this->m_YMax)
{
this->m_YMax = y+1;
}
m_dcPlot.SetPixel(this->m_rectPlot.left+x, this->m_nPlotHeight-y, color);
this->InvalidateCtrl();
} |
Pour ceux qui n'ont pas compris clairement ce que j'ai expliqué plus haut je reformule, avec le code ci-dessus, ça devrait etre plus facile.
J'ai 2 zones d'affichages, la partie grille dcGrid et la partie courbe dcPlot.
La zone dcGrid d'affiche bien, par contre la zone dcPlot n'affiche rien (m_dcPlot.SetPixel() qui ne fait rien)
Qu'est ce que j'ai oublié de faire ?
Merci de vos réponses, quelles qu'elles soient !