[win32] - creating a form super class

i'm trying creating a form super class, but the form isn't created :(

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
form()
    {
        ++FormCount;
        strCaption=strCaption + to_string(FormCount);
        setParent(HWND_DESKTOP);
    }

 void setParent(HWND parent=HWND_DESKTOP)
    {

            WNDCLASS FormClass;
            HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);

            FormClass.hInstance = mod;
            FormClass.lpszClassName = TEXT("form");



            // replace it with local WNDPROC
            FormClass.lpfnWndProc = WndProcForm;
            FormClass.hbrBackground = (HBRUSH) ((COLOR_WINDOW)+1);

            // register the new window class"
            RegisterClass(&FormClass);

            hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        TEXT("form"),
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, mod, NULL);

            if (hwnd == NULL)
                MessageBox(NULL, "Can't create the control", "error", MB_OK);


            clrBackColor= GetBkColor(GetDC(HWND_DESKTOP));
            clrTextColor = GetTextColor(GetDC(HWND_DESKTOP));


        RECT a;
        GetClientRect(hwnd,&a);
        intTop=a.top;
        intLeft=a.left;
        intWidth=a.right-a.left;
        intHeight=a.bottom-a.top;
    }


//............
static LRESULT CALLBACK WndProcForm(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        static POINT PreviousLocation, Location;
        static bool Tracking = false;
        static MouseButtons MBButtons;
        static bool blControl=false;
        static bool blShift=false;
        static bool blResize=false;
        static int xPos=0;
        static int yPos=0;
        static UINT_PTR timerid;
        static bool blnDrag=false;

        //preparing the message loop for be used in super class way
        //Getting the parent procedure
        WNDPROC oldproc = (WNDPROC)GetProp(HWND_DESKTOP, formpropname);
        if (oldproc == NULL)
            MessageBox(NULL, "Can't find old procedure", "error", MB_OK | MB_ICONEXCLAMATION);

        //add the instance class to inst pointer
        form *inst = (form*)GetProp(hwnd,formclassprop);
        if (inst == NULL && msg == WM_NCCREATE)
        {
            inst = (form*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
            SetProp(hwnd, formclassprop, (HANDLE)inst);
        }
        if (inst == NULL)
            MessageBox(NULL, "Can't find the instance pointer", "error", MB_OK);

        //Working with messages
        switch(msg)
        {
            case WM_CREATE:
            {

                inst->Create(inst->intLeft, inst->intTop);
            }
            break;

i belive that is confused the Create and some variables, but, for now, what i'm doing wrong for create the form and connect it to message procedure?
i found some errors:

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
void setParent(HWND parent=HWND_DESKTOP)
    {

            WNDCLASSEX FormClass;
            char classname[]="form";
            HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);

            FormClass.cbSize        = sizeof(WNDCLASSEX);
            FormClass.style         = 0;
            FormClass.lpfnWndProc   = WndProcForm;
            FormClass.cbClsExtra    = 0;
            FormClass.cbWndExtra    = 0;
            FormClass.hInstance     = mod;
            FormClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
            FormClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
            FormClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
            FormClass.lpszMenuName  = NULL;
            FormClass.lpszClassName = classname;
            FormClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

            // register the new window class"
            RegisterClassEx(&FormClass);
            SetProp(parent, formpropname, (HANDLE)FormClass.lpfnWndProc);
            
            hwnd = CreateWindowEx(0, classname, "The title of my window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, parent, NULL, mod, (LPVOID) this);

            if (hwnd == NULL)
                MessageBox(NULL, "Can't create the control", "error", MB_OK);
            SetProp(hwnd, formclassprop, (HANDLE)this);
            ShowWindow(hwnd, SW_NORMAL);
            UpdateWindow(hwnd);

            clrBackColor= GetBkColor(GetDC(parent));
            clrTextColor = GetTextColor(GetDC(parent));


        RECT a;
        GetClientRect(hwnd,&a);
        intTop=a.top;
        intLeft=a.left;
        intWidth=a.right-a.left;
        intHeight=a.bottom-a.top;
    }

now the form is showed. my problem is with window procedure:
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
static LRESULT CALLBACK WndProcForm(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        static POINT PreviousLocation, Location;
        static bool Tracking = false;
        static MouseButtons MBButtons;
        static bool blControl=false;
        static bool blShift=false;
        static bool blResize=false;
        static int xPos=0;
        static int yPos=0;
        static UINT_PTR timerid;
        static bool blnDrag=false;


        //preparing the message loop for be used in super class way
        //Getting the parent procedure
        WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), formpropname);
        if (oldproc == NULL)
            MessageBox(NULL, "Can't find old procedure", "error", MB_OK | MB_ICONEXCLAMATION);

        //add the instance class to inst pointer
        form *inst = (form*)GetProp(hwnd, formclassprop);
        if (inst == NULL && msg == WM_NCCREATE)
        {
            inst = (form*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
            SetProp(hwnd, formclassprop, (HANDLE)inst);
        }
        if (inst == NULL)
            MessageBox(NULL, "Can't find the instance pointer", "error", MB_OK);

        RegisterHotKey( inst->hwnd, inst->altmessage, MOD_ALT, inst->altkey );

        //Working with messages
        switch(msg)
        {
            case WM_CREATE:
            {
                //inst->Create(inst->intLeft, inst->intTop);
            }
            break;
//...................... 

because i'm having problems connect to the pointer :(
what i'm doing wrong?
my problem is that i don't receive the oldproc correctly. that's why i can't get the correct inst pointer.
i know these, because the WM_CREATE message is ignored :(
what can anyone tell me?
I noticed one thing.
I think the break; on line 40 needs to be in the curly braces.
after some work and test the WM_CREATE is realy activated.
so why i can't find the old procedure?
why i can't get the inst pointer?
(these code works fin with cild controls)

even these line:
1
2
3
4
5
case WM_COMMAND:
            {
                SendMessage(hwnd,WM_COMMAND,wParam,lParam);
            }
            break;

mayke the OS give me an error and close the program, when i click on button
Topic archived. No new replies allowed.