void setparent(HWND parent)
{
static int i=i+1;
string strclass=labelclassprop + to_string(i);
labelclassprop=strclass.c_str();
WNDCLASS wc;
HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
ZeroMemory(&wc, sizeof(WNDCLASS));
GetClassInfo(mod, "STATIC", &wc);
wc.hInstance = mod;
wc.lpszClassName = labelclassprop;
wc.hbrBackground = CreateSolidBrush(RGB(255,0,0));
// store the old WNDPROC of the EDIT window class
// store the old WNDPROC of the EDIT window class
SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
//SetProp(parent, labelclassprop, (HANDLE)this);
//UpdateWindow(hwnd);
// replace it with local WNDPROC
wc.lpfnWndProc = WndProc;
// register the new window class, "ShEdit"
if (!RegisterClass(&wc))
MessageBox(NULL, "error in register", "error", MB_OK);
hwnd = CreateWindowEx(
WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
labelclassprop,
labelclassprop,
SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
100, 100, 100, 100,
parent,
NULL,
mod,
(LPVOID)this);
if (hwnd == NULL)
MessageBox(NULL, "error in create", "error", MB_OK);
//SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
SetProp(hwnd, labelclassprop, (HANDLE)this);
UpdateWindow(hwnd);
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
i have 1 problem, the i isn't incremented.
if it's static, it should be incremented, but maybe because i'm using it in a diferent instance, isn't incremented.
(after fix these, i will show you 1 thing with my code)
No. This doesn't do what you expect. Effectively this statement is only executed once for the first time setparent() is called. Subsequently it is not executed.
You need something like this
Code:
static int i = 0;
i++;
This can be shown using a simple test program.
Code:
#include <iostream>
using namespace std;
int inc()
{
static int i = i +1;
return i;
}
int main()
{
for (int k = 0; k < 10; k++)
cout << inc() << " ";
cout << endl;
return 0;
}
This produces the output
Code:
1 1 1 1 1 1 1 1 1 1
whereas
Code:
#include <iostream>
using namespace std;
int inc()
{
static int i = 0;
i = i + 1;
return i;
}
int main()
{
for (int k = 0; k < 10; k++)
cout << inc() << " ";
cout << endl;
return 0;
}
produces the output
Code:
1 2 3 4 5 6 7 8 9 10
Last edited by 2kaud; January 5th, 2014 at 06:51 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
No. This doesn't do what you expect. Effectively this statement is only executed once for the first time setparent() is called. Subsequently it is not executed.
You need something like this
Code:
static int i = 0;
i++;
This can be shown using a simple test program.
Code:
#include <iostream>
using namespace std;
int inc()
{
static int i = i +1;
return i;
}
int main()
{
for (int k = 0; k < 10; k++)
cout << inc() << " ";
cout << endl;
return 0;
}
This produces the output
Code:
1 1 1 1 1 1 1 1 1 1
whereas
Code:
#include <iostream>
using namespace std;
int inc()
{
static int i = 0;
i = i + 1;
return i;
}
int main()
{
for (int k = 0; k < 10; k++)
cout << inc() << " ";
cout << endl;
return 0;
}
????
why, i belive, it's wrong??? because if i move the mouse in label 1 the window procedure works, but with hwn of label 2, because changes everything in label 2.
see the image anexed
As I explained in a previous post, it's because you are setting the labelclassproperty that contains the class inst pointer for the label parent rather than the label window itself. See my code in posts #120 and #121 for possible changes to WndPric and SetParent. So every label window gets the same class inst. So the class inst has to be set for the label window. However, the WM_CREATE message is sent before the property is set. So the class inst needs to be passed to WM_CREATE via the CREATESTRUCT. So if the label control property is not available and the message is WM_CREATE then the class inst can be picked up via the lParam pointer to the CREATESTRUCT.
Code:
void setparent(HWND parent)
{
static int i = 0;
i++;
string strclass=labelclassprop + to_string(i);
labelclassprop=strclass.c_str();
WNDCLASS wc;
HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
ZeroMemory(&wc, sizeof(WNDCLASS));
GetClassInfo(mod, "STATIC", &wc);
wc.hInstance = mod;
wc.lpszClassName = labelclassprop;
wc.hbrBackground = CreateSolidBrush(RGB(255,0,0));
// store the old WNDPROC of the EDIT window class
SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
// replace it with local WNDPROC
wc.lpfnWndProc = WndProc;
// register the new window class, "ShEdit"
if (!RegisterClass(&wc))
MessageBox(NULL, "error in register", "error", MB_OK);
hwnd = CreateWindowEx(
WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
labelclassprop,
labelclassprop,
SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
100, 100, 100, 100,
parent,
NULL,
mod,
(LPVOID)this);
if (hwnd == NULL)
MessageBox(NULL, "error in create", "error", MB_OK);
SetProp(hwnd, labelclassprop, (HANDLE)this);
UpdateWindow(hwnd);
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
As I explained in a previous post, it's because you are setting the labelclassproperty that contains the class inst pointer for the label parent rather than the label window itself. See my code in posts #120 and #121 for possible changes to WndPric and SetParent. So every label window gets the same class inst. So the class inst has to be set for the label window. However, the WM_CREATE message is sent before the property is set. So the class inst needs to be passed to WM_CREATE via the CREATESTRUCT. So if the label control property is not available and the message is WM_CREATE then the class inst can be picked up via the lParam pointer to the CREATESTRUCT.
Code:
void setparent(HWND parent)
{
static int i = 0;
i++;
string strclass=labelclassprop + to_string(i);
labelclassprop=strclass.c_str();
WNDCLASS wc;
HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
ZeroMemory(&wc, sizeof(WNDCLASS));
GetClassInfo(mod, "STATIC", &wc);
wc.hInstance = mod;
wc.lpszClassName = labelclassprop;
wc.hbrBackground = CreateSolidBrush(RGB(255,0,0));
// store the old WNDPROC of the EDIT window class
SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
// replace it with local WNDPROC
wc.lpfnWndProc = WndProc;
// register the new window class, "ShEdit"
if (!RegisterClass(&wc))
MessageBox(NULL, "error in register", "error", MB_OK);
hwnd = CreateWindowEx(
WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
labelclassprop,
labelclassprop,
SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
100, 100, 100, 100,
parent,
NULL,
mod,
(LPVOID)this);
if (hwnd == NULL)
MessageBox(NULL, "error in create", "error", MB_OK);
SetProp(hwnd, labelclassprop, (HANDLE)this);
UpdateWindow(hwnd);
}
let me ask: why i can't use 'this' inside of window procedure?
Because WndProc is static and static functions can't access 'this'. WndProc has to be static due to the way it is used.
sorry the inst is NULL
For which message?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
let me ask: why i can't use 'this' inside of window procedure?
The Windows API is 'C' based. A window procedure has no idea about the C++ language. Therefore, there is no such thing as "this", objects, etc. in the Windows API.
If this still says that inst is null, show the value of msg in the messagebox.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
Last edited by 2kaud; January 5th, 2014 at 05:51 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.