Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <string>
- #include <map>
- #include <vector>
- using namespace std;
- class WinControl {
- public:
- HWND Handle;
- WinControl::WinControl() : Handle(0) {}
- std::string getCaption() {
- char Text[255];
- GetWindowText(Handle, Text, 255);
- return std::string(Text);
- }
- void setCaption(std::string NewVal) {
- SetWindowText(Handle, NewVal.c_str());
- }
- inline HWND getHandle() const {
- return this->Handle;
- }
- };
- class Form: public WinControl {
- private:
- HINSTANCE hInst;
- public:
- int ID;
- int getNextID() {
- return ID++;
- }
- std::vector<WinControl*> controls;
- Form::Form(HINSTANCE hInst, int W, int H, char * Caption);
- static LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam,
- LPARAM lparam);
- bool Show();
- inline HINSTANCE getInstance() const {
- return this->hInst;
- }
- void OnCreate() {};
- void OnDestroy() {};
- void OnLButtonDown(int X, int Y) {};
- };
- class Control: public WinControl {
- public:
- Control::Control(Form* parent, int x, int y, int w, int h,
- std::string caption, std::string class_name, DWORD styles,
- DWORD styles_ex = 0) {
- int ID = parent->getNextID();
- if (styles_ex > 0) {
- Handle = CreateWindowEx(styles_ex, class_name.c_str(),
- caption.c_str(), styles, x, y, w, h, parent->getHandle(),
- (HMENU) ID, parent->getInstance(), NULL);
- } else {
- Handle = CreateWindow(class_name.c_str(), caption.c_str(), styles, x,y,w,h, parent->getHandle(), (HMENU)ID, parent->getInstance(), NULL);
- }
- }
- };
- class Edit : public Control
- {
- public:
- Edit::Edit(Form* parent, int x, int y, int w, int h, std::string caption)
- : Control(parent, x, y, w, h, caption, "EDIT", ES_LEFT | ES_AUTOHSCROLL | WS_CHILD | WS_VISIBLE, WS_EX_CLIENTEDGE)
- {
- }
- };
- class Global {
- public:
- static std::map<std::string, Form*> handle_map;
- };
- std::map<std::string, Form*> Global::handle_map;
- bool Form::Show() {
- MSG msg;
- ShowWindow(Handle, SW_SHOWNORMAL);
- UpdateWindow(Handle);
- while (GetMessage(&msg, NULL, 0, 0) > 0) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return true;
- }
- Form::Form(HINSTANCE hInst, int W, int H, char * Caption) :
- hInst(hInst), ID(0) {
- this->hInst = hInst;
- WNDCLASS wc;
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = WindowProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInst;
- wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
- wc.lpszMenuName = NULL;
- wc.lpszClassName = Caption;
- assert(RegisterClass(&wc));
- Global::handle_map[std::string(Caption)] = this;
- Handle = CreateWindow(Caption, Caption,
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,CW_USEDEFAULT, W, H,
- NULL,NULL,hInst,NULL);
- assert(Handle);
- }
- LRESULT CALLBACK Form::WindowProc(HWND hwnd, UINT msg, WPARAM wparam,
- LPARAM lparam)
- {
- char ClassName[100];
- GetClassName(hwnd, ClassName, 100);
- Form*Sender = Global::handle_map[ClassName];
- if (!Sender) {
- printf("Handle: %d\n", hwnd);
- return DefWindowProc(hwnd, msg, wparam, lparam);
- }
- switch (msg) {
- case WM_SHOWWINDOW:
- printf("WM_SHOWWINDOW: %s\n", Sender->getCaption().c_str());
- break;
- case WM_CREATE:
- Sender->OnCreate();
- break;
- case WM_DESTROY:
- Sender->OnDestroy();
- Global::handle_map.erase(ClassName);
- PostQuitMessage(0);
- break;
- case WM_LBUTTONDOWN:
- puts("Klik");
- printf("WM_LBUTTONDOWN: %s\n", Sender->getCaption().c_str());
- Sender->OnLButtonDown(LOWORD(lparam), HIWORD(lparam));
- break;
- default:
- return DefWindowProc(hwnd, msg, wparam, lparam);
- }
- return 0;
- }
- class MainForm : public Form
- {
- public:
- MainForm::MainForm(HINSTANCE hInst)
- : Form(hInst, 320, 240, "No hej xD")
- {
- puts("MainForm()");
- }
- void OnCreate()
- {
- Form::OnCreate();
- puts("WM_CREATE2");
- this->setCaption("Zmienilem captiona xDxD");
- }
- void OnDestroy()
- {
- puts("Destroy");
- }
- void OnLButtonDown(int X, int Y)
- {
- printf("Left Button Down (%d, %d)\n", X, Y);
- }
- };
- int WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nCmdShow) {
- Form f = MainForm(hInst);
- return f.Show();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement