Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Use of this : I am currently developing a custom window control (a child window). It's just a basic typical button.

What I have done : I'm only using a one Window Procedure for all the same type of new window controls. I want to store some information for each of these controls (same type ones).All these information are in a type of a structure called "BUTTONINFO".

C++
typedef struct tagBUTTONINFO{
    int xPos;
    int yPos;
    TCHAR * szText;
    int Lenght;
    int Width;
    int otherInfo;
}BUTTONINFO;


I have allocated some memory for this in the Window Class of my child window control as follows :

WNDCLASS wndclass;
wndclass.cbWndExtra    = sizeof(BUTTONINFO) ;


The Problem : For Assigning a Value for the above extra field I have to use SetWindowLong function (As I know). Here is the Function :

BUTTONINFO buttonInfo;
SetWindowLong(hwnd,0,buttonInfo);


The 3rd Parameter to SetWindowLong is a type of a LONG. So how Do I Assign or Retrieve this Information ?

Please Help !
Posted

Assumed, your structure (a class would be better)
has its constructor(to allocate and set the szText pointer) and destructor(to free the szText pointer) :) :
C++
{
  // setting phase
  SetWindowLong(hwnd, 0, (LONG) new CYorButtonInfo(/*list of construtor's parameters*/);
}
{
  // destroying phase
  CYourButtonInfo* pcInfo = (CYourButtonInfo*) GetWindoLong(hwnd, 0);
  delete pcInfo;
}
 
Share this answer
 
v2
Comments
Captain Price 1-Jul-12 10:27am    
Sorry, I didn't understand. Please explain !
Eugen Podsypalnikov 1-Jul-12 14:21pm    
Your structure and all its members should be allocated in heap.
When it could be passed to the SetWindowLong(..) function per its pointer.
After this action you can get its pointer per GetWindowLong(..) function.
Captain Price 2-Jul-12 8:57am    
can you give me some example code please ! That would be very helpful ! Please ! I'm trying this for days, but I still don't understand clearly.
Eugen Podsypalnikov 2-Jul-12 9:30am    
{ BUTTONINFO* pButtonInfo(new BUTTONINFO);
pButtonInfo->xPos = 12;
//..
pButtonInfo->szText = new TCHAR[80];
_tcscpy_s(pButtonInfo->szText,
_countof(pButtonInfo->szText),
_T("Hello world !"));
pButtonInfo->Length = 120;
//..
::SetWindowLong(hwnd, 0, (LONG) pButtonInfo);
}

case WM_DESTROY:
{
BUTTONINFO* pButtonInfo = (BUTTONINFO*) ::GetWindowLong(hwnd, 0);
delete[] pButtonInfo->szText;
delete pButtonInfo;
//..
}
break;
Captain Price 3-Jul-12 0:11am    
Your code is fine ! But the following Line has a mistake ! Compiler return some errors :

BUTTONINFO* pButtonInfo(new BUTTONINFO);
Here's the answer that i've been looking for :.

Read the article in the following link;
http://www.catch22.net/tuts/custom-controls[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900