Bonjour
J'ai souhaité réaliser une application des plus basics mais orienté objet. Pour cela, j'ai une classe mère appelé Window que j'ai trouvé dans un code source de la GNU, il n'y a besoin que de l'entête.
Ensuite, j'ai le corps de ma class Form :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #ifndef WINDOW_CONTROL_H #define WINDOW_CONTROL_H class Window { public: Window(): _hInst(NULL), _hParent(NULL), _hSelf(NULL){}; virtual ~Window() {}; virtual void init(HINSTANCE hInst, HWND parent){_hInst = hInst;_hParent=parent;} virtual void destroy() = 0; // void destroy() = 0; virtual void display(bool toShow = true) const {::ShowWindow(_hSelf, toShow?SW_SHOW:SW_HIDE);}; virtual void reSizeTo(RECT & rc) // should NEVER be const !!! { ::MoveWindow(_hSelf, rc.left, rc.top, rc.right, rc.bottom, TRUE); redraw(); }; virtual void reSizeToWH(RECT & rc) // should NEVER be const !!! { ::MoveWindow(_hSelf, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE); redraw(); }; virtual void redraw(bool forceUpdate = false) const { ::InvalidateRect(_hSelf, NULL, TRUE); if (forceUpdate) ::UpdateWindow(_hSelf); }; virtual void getClientRect(RECT & rc) const { ::GetClientRect(_hSelf, &rc); }; virtual void getWindowRect(RECT & rc) const { ::GetWindowRect(_hSelf, &rc); }; virtual int getWidth() const { RECT rc; GetClientRect(_hSelf, &rc); return (rc.right - rc.left); }; virtual int getHeight() const { RECT rc; GetClientRect(_hSelf, &rc); if (::IsWindowVisible(_hSelf) == TRUE) return (rc.bottom - rc.top); return 0; }; virtual bool isVisible() const { return (::IsWindowVisible(_hSelf)?true:false); }; HWND getHSelf() const { return _hSelf;}; //assert(_hSelf); HWND getHParent() const { return _hParent; }; void getFocus() const { ::SetFocus(_hSelf); }; HINSTANCE getHinst() const { if (!_hInst) { ::MessageBox(NULL, TEXT("_hInst == NULL"), TEXT("class Window"), MB_OK); throw int(1999); } return _hInst; }; protected: HINSTANCE _hInst; HWND _hParent; HWND _hSelf; }; #endif //WINDOW_CONTROL_H
Suivi de son fichier cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <windows.h> #include "window.h" class Form :public Window { public: Form(HINSTANCE,HINSTANCE,LPSTR,int); ~Form(); LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); WPARAM Run(); void destroy() {} private: Form(); WNDCLASSEX wc; //HWND hwnd; MSG Msg; int nCmdShow; LPSTR lpCmdLine; };
et enfin le fichier lançant l'application
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <windows.h> #include "Form.h" #define IDC_MAIN_EDIT 101 LRESULT CALLBACK Form::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: { HFONT hfDefault; HWND hEdit; hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"),TEXT( ""), WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL); if(hEdit == NULL) MessageBox(hwnd, TEXT("Could not create edit box."), TEXT("Error"), MB_OK | MB_ICONERROR); hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT); SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0)); } break; case WM_SIZE: { HWND hEdit; RECT rcClient; GetClientRect(hwnd, &rcClient); hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT); SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER); } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } WPARAM Form::Run() { ShowWindow(_hSelf, nCmdShow); UpdateWindow(_hSelf); while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } Form::Form(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR aCmdLine,int aCmdShow) { HWND hwnd; lpCmdLine = aCmdLine; nCmdShow =aCmdShow; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInst; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT("myWindowClass");//g_szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK); exit(0); } hwnd = CreateWindowEx( 0, TEXT("Class de la fenêtre"), TEXT("C'est en forgeant que l'on deviens des forgeons"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 480, 320, NULL, NULL, _hInst, NULL); if(hwnd == NULL) { MessageBox(NULL,TEXT( "Window Creation Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK); exit(0); } this->init(hInst,hwnd); }
Le problème vient de la fonction Form::WndProc
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 #include "Form.h" #include <windows.h> //Form Aps; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Form App(hInstance,hPrevInstance,lpCmdLine,nCmdShow); return App.Run(); }
impossible de lier l'adresse à la WNDCLASSEX, j'ai "casté" et voilà ce que dit le compilateur :
1>e:\visual c++\apps\apps\form.cpp(67) : error C2440: 'cast de type'*: impossible de convertir de 'overloaded-function' en 'WNDPROC'
1> Aucune fonction ayant ce nom dans la portée ne correspond au type de la cible
Mon code est loin d'être optimisé, je le sais très bien, mon première objectif est que ça fonctionne ensuite, je verrai pour l'optimisation.
Cependant, toute critique constructive est bien venu. Merci
Partager