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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include "stdafx.h"
TMainFrame::TMainFrame()
{ _hSelf = NULL;
this->_hParent = NULL;
this->_pApp = NULL;
this->_pDrawer = new Drawer();
}
BOOL TMainFrame::Create(Programme* aApp)
{
this->_pApp = aApp;
// classe de fenêtre
TCHAR szWndClass[] = _T("Drawer_MainFrame");
// initialisation classe de fenêtre
WNDCLASS wc;
ZeroMemory(&wc, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = this->_pApp->GetHInst();
wc.hIcon = LoadIcon(this->_pApp->GetHInst(), MAKEINTRESOURCE(IDI_DRAWER));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_DRAWER);
wc.lpszClassName = szWndClass;
wc.hbrBackground = NULL;
// enregistrement classe de fenêtre
if(!RegisterClass(&wc))
return FALSE;
// création fenêtre principale
this->_hSelf = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, szWndClass, this->_pApp->GetAppTitle(), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
NULL, NULL, this->_pApp->GetHInst(), this);
if(this->GetDrawer()->Create(this))
this->GetDrawer()->display();
return (_hSelf != NULL);
}
LRESULT CALLBACK TMainFrame::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// affecter l'objet TMainFrame lors de la création de la fenêtre, contenu dans la
// structure CREATESTRUCT pointée par lParam
if(msg == WM_CREATE)
{
LPCREATESTRUCT lpcs = (LPCREATESTRUCT) lParam;
SetWindowLong(hWnd, GWL_USERDATA, (LONG)lpcs->lpCreateParams);
}
// récupération objet TMainFrame associé à la fenêtre
TMainFrame* pWnd = (TMainFrame*) GetWindowLong(hWnd, GWL_USERDATA);
if(!pWnd)
return DefWindowProc(hWnd, msg, wParam, lParam);
switch(msg)
{
case WM_CREATE : return pWnd->MsgCreate (hWnd, wParam, lParam);
case WM_CLOSE : return pWnd->MsgClose (wParam, lParam);
case WM_DESTROY : return pWnd->MsgDestroy (wParam, lParam);
case WM_COMMAND : return pWnd->MsgCommand (wParam, lParam);
default : return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
LRESULT TMainFrame::MsgCreate(HWND hWnd, WPARAM wParam, LPARAM lParam) { this->_hSelf=hWnd; return 0;}
LRESULT TMainFrame::MsgClose(WPARAM wParam, LPARAM lParam) { this->destroy(); return 0;}
LRESULT TMainFrame::MsgDestroy(WPARAM wParam, LPARAM lParam) { PostQuitMessage(0); return 0;}
LRESULT TMainFrame::MsgCommand(WPARAM wParam, LPARAM lParam)
{
// on laisse la vue de la zone cliente commencer par traiter la commande
/*if(SendMessage(this->_pDrawer->getHSelf(), WM_COMMAND, wParam, lParam))
return TRUE;
*/
int nIDCtl = LOWORD(wParam); // en fonction de la commande
switch(nIDCtl)
{ case IDM_FILE_NEW :MessageBox(this->getHSelf(),TEXT("ça marche!"),TEXT("Ok"),MB_OK);break;
case IDM_EXIT : CmdFileQuit(); break;
default : return FALSE;
}
return TRUE;// la commande a été traitée
}
void TMainFrame::CmdFileQuit() { SendMessage(_hSelf, WM_CLOSE, 0, 0);}
Programme::Programme()
{ this->_hInst=NULL;
this->_pMainFrame=NULL;
LoadString(_hInst, IDS_APP_TITLE, _szTitle, MAX_LOADSTRING);
LoadString(_hInst, IDS_DRAWER, _szWindowClass, MAX_LOADSTRING);
}
BOOL Programme::Init(HINSTANCE hInst,int nCmdShow)
{
_hInst = hInst;
if (!_hInst){::MessageBox(NULL, TEXT("_hInst == NULL"), TEXT("class Window"), MB_OK);throw int(1999);}
this->_pMainFrame = new TMainFrame(); // création fenêtre principale
if(!this->_pMainFrame->Create(this))
return FALSE;
ShowWindow(this->_pMainFrame->getHSelf(), nCmdShow);
UpdateWindow(this->_pMainFrame->getHSelf());
return TRUE; // initialisation réussie
}
WPARAM Programme::Run()
{ MSG _msg;
while (GetMessage(&_msg, NULL, 0, 0)) // Boucle de messages principale*:
{
TranslateMessage(&_msg);
DispatchMessage(&_msg);
}
return (int) _msg.wParam;
}
BOOL Drawer::Create(TMainFrame* pMainFrame)
{
TCHAR szWndClass[] = TEXT("Paint_ChildView");
WNDCLASSEX wc;
wc.hInstance = pMainFrame->GetProgramme()->GetHInst();
wc.lpszClassName= szWndClass;
//OldWndProc = wc.lpfnWndProc;
wc.lpfnWndProc = this->WndProc;
wc.hbrBackground = NULL;
wc.style = CS_DBLCLKS;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszMenuName = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
if ( !RegisterClassEx( &wc ) )MessageBox (NULL, TEXT("Registering Error"), TEXT("Information"), MB_OK |MB_ICONSTOP);
this->_hParent = pMainFrame->getHSelf();
_hSelf = CreateWindowEx(0, szWndClass, NULL, WS_CHILD|WS_VISIBLE,0, 0,500, 500, pMainFrame->getHSelf(), NULL, pMainFrame->GetProgramme()->GetHInst(), this);
return (_hSelf != NULL);
}
LRESULT CALLBACK Drawer::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// affecter l'objet Drawer lors de la création de la vue, contenu dans la
// structure CREATESTRUCT pointée par lParam
if(msg == WM_CREATE)
{
LPCREATESTRUCT lpcs = (LPCREATESTRUCT) lParam;
SetWindowLong(hWnd, GWL_USERDATA, (LONG)lpcs->lpCreateParams);
}
// récupération objet Drawer associé à la fenêtre
Drawer* pWnd = (Drawer*) GetWindowLong(hWnd, GWL_USERDATA);
if(!pWnd)
return DefWindowProc(hWnd, msg, wParam, lParam);
// en fonction du message
switch(msg)
{
case WM_MOUSEMOVE : return pWnd->MsgMouseMove (wParam, lParam);
case WM_CREATE : return pWnd->MsgCreate (wParam, lParam);
//case WM_LBUTTONDOWN:case WM_RBUTTONDOWN:break;
default : return FALSE;
}
}
LRESULT Drawer::MsgCreate(WPARAM wParam, LPARAM lParam)
{
return FALSE;
}
LRESULT Drawer::MsgMouseMove (WPARAM wParam, LPARAM lParam)
{
POINT point;
PAINTSTRUCT ps;
HDC hdc= BeginPaint(_hSelf,&ps);
TCHAR bf2[5];
TCHAR bf1[5];
GetCursorPos(&point);
ScreenToClient(_hSelf,&point);
_itow_s(point.x,bf1,5,10);
_itow_s(point.y,bf2,5,10);
TCHAR aText[MAX_LOADSTRING] =TEXT("Coordonnée cliente :");wcscat(aText,bf1);wcscat(aText,TEXT(","));wcscat(aText,bf2);
SetWindowText(this->getHParent(),aText);
MoveToEx(hdc,0,0,NULL);
LineTo(hdc,point.x,point.y);
EndPaint(this->getHSelf(), &ps);
return 0;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
Programme Apps;
if(Apps.Init(hInstance,nCmdShow))
return Apps.Run();
else return 0;
} |
Partager