Tiens, j´étais en train de faire un TP, je te donne mes résultats:
- include <afxwin.h>
class IG1App : public CWinApp
{
public:
virtual BOOL InitInstance();
};
IG1App app;
- define ID_BUTTON1 100
class IG1Wnd : public CFrameWnd
{
CButton * button;
CPoint *LePoint;
public:
IG1Wnd();
~IG1Wnd();
afx_msg void ClicBouton();
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
//moi amélioration
afx_msg void OnLButtonDblClk( UINT nFlags, CPoint point ) ;
void ShowMousePos(int x, int y);
afx_msg void OnLButtonUp( UINT nFlags, CPoint point ) ;
afx_msg void OnLButtonDown( UINT nFlags, CPoint point ) ;
void DrawLine(CDC *pDC, int x1, int y1, int x2, int y2);
};
BOOL IG1App::InitInstance()
{
m_pMainWnd = new IG1Wnd();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
IG1Wnd::IG1Wnd()
{
Create(NULL, "IG1", WS_OVERLAPPEDWINDOW, CRect(0, 0, 200, 200));
button = new CButton();
button->Create("Click me", WS_CHILD|WS_VISIBLE|SS_CENTER, CRect(50, 80, 150, 150), this, ID_BUTTON1);
LePoint = new CPoint();
LePoint->x = 0;
LePoint->y = 0;
}
IG1Wnd::~IG1Wnd()
{
delete button;
delete LePoint;
}
void IG1Wnd : :ClicBouton()
{
AfxMessageBox("Clic sur bouton") ;
}
void IG1Wnd : :OnPaint()
{
//le code de dessin
CRect r ;
GetClientRect(r);
CPaintDC dc(this);
CPen pen(PS_SOLID, 2, RGB(255,0,0)), *oldpen;
oldpen = dc.SelectObject(&);
dc.MoveTo(0, 0) ;
dc.LineTo(r.Width(), r.Height()) ;
dc.MoveTo(0, r.Height());
dc.LineTo(r.Width(), 0);
dc.SelectObject(oldpen);
//un peu de fun
for(int i=0, j=0; i<r.Width(); j+=10, i+=10)
{
dc.MoveTo(i, j) ;
dc.LineTo(i, r.Height()) ;
}
//cela ne fonctionne pas, comprendre pourquoi
button->SetWindowPos(this,r.Width()/2,r.Height()/
2,150,150,SWP_FRAMECHANGED);
}
BEGIN_MESSAGE_MAP(IG1Wnd, CFrameWnd)
ON_BN_CLICKED(ID_BUTTON1, ClicBouton)
ON_WM_PAINT()
//autres nouveautés
ON_WM_LBUTTONDBLCLK()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
//nouveauté pour que les exercices soient resolues
void IG1Wnd : :OnLButtonDblClk( UINT nFlags, CPoint point )
{
//AfxMessageBox("Clic") ;
ShowMousePos(point.x, point.y);
}
void IG1Wnd::ShowMousePos(int x, int y)
{
char s[255];
sprintf(s, "%d %d ", x, y);
CRect r;
GetClientRect(r);
CClientDC dc(this);
dc.TextOut(r.Width()/2, r.Height()/2, s);
}
void IG1Wnd::OnLButtonUp( UINT nFlags, CPoint point )
{
CPaintDC dc(this);
dc.MoveTo(0, 0) ;
dc.LineTo(50, 50) ;
DrawLine(&, 10,15,100,100);
}
void IG1Wnd::OnLButtonDown( UINT nFlags, CPoint point )
{
CClientDC dc(this);
dc.MoveTo( LePoint->x , LePoint->y) ;
LePoint->x = point.x;
LePoint->y = point.y;
dc.LineTo(point.x, point.y) ;
}
void IG1Wnd::DrawLine(CDC *pDC, int x1, int y1, int x2, int y2)
{
pDC->MoveTo(x1, y1);
pDC->LineTo(x2, y2);
}