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
|
void setParent(HWND parent=WindowMain)
{
static WNDCLASS ButtonClass;
if (hwnd==NULL)
{
HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
ZeroMemory(&ButtonClass, sizeof(WNDCLASS));
GetClassInfo(mod, TEXT("BUTTON"), &ButtonClass);
ButtonClass.hInstance = mod;
ButtonClass.lpszClassName = TEXT("CBUTTON");
ButtonClass.style=CS_DBLCLKS;
// store the old WNDPROC of the button window class
SetProp(parent, buttonpropname, (HANDLE)ButtonClass.lpfnWndProc);
// replace it with local WNDPROC
ButtonClass.lpfnWndProc = WndProcButton;
ButtonClass.hbrBackground = (HBRUSH)GetStockBrush(NULL_BRUSH);
// register the new window class"
RegisterClass(&ButtonClass);
hwnd = CreateWindowEx(
0,
TEXT("CBUTTON"),//these must be the same of ButtonClass.lpszClassName.. registed class
strCaption.c_str(),
WS_VISIBLE | WS_CHILD |WS_TABSTOP| BS_OWNERDRAW | BS_NOTIFY | WS_CLIPSIBLINGS, //onerdraw for add images
intLeft, intTop, intWidth, intHeight,
parent,
NULL,
mod,
this);
if (hwnd == NULL)
MessageBox(NULL, "Can't create the control", "error", MB_OK);
if (SetProp(hwnd, buttonclassprop, (HANDLE)this) == 0)
MessageBox(NULL, "can't set the class property", "error", MB_OK);
clrBackColor= GetBkColor(GetDC(GetParent(hwnd)));
clrTextColor = GetTextColor(GetDC(hwnd));
}
else
{
if(hwnd!=NULL)
{
SetProp(GetParent(hwnd), buttonpropname, (HANDLE)NULL);
SetProp(parent, buttonpropname, (HANDLE)ButtonClass.lpfnWndProc);
SetParent(hwnd,parent);
}
//SendMessage(parent,WM_CHILDTOPARENT,(WPARAM)hwnd, (LPARAM)NULL);
}
}
|