How to pass arguments from window to the windowProc function :
CREATESTRUCT str;
int*i = new int();
*i = 40;
str->lpCreateParams = i;
WindowProc()
{
switch(Msg)
{
int* j = (int*) ((LPCREATESTRUCT(lParam)) ->lpCreateParams)
}
}
Another one way is we can also attach the Window parameter at the time of
WNDcLASS registration.
wc.cbWndExtra = i;
we can get this value using GetWindowLong()
int* j = (int*) GetWindowLong(hWnd,GWL_USERDATA);
SetWindowLong() fn is also available
we can also use GetWindowLongPtr() or SetWindowLongPtr() for this purpose.
Showing posts with label SDK. Show all posts
Showing posts with label SDK. Show all posts
Thursday, December 20, 2007
Window Creation problem with WM_NCCREATE message
I registered the class in win32 SDK application.
I created the window using CreateWindow() fn.CreateWindow() fn returns NULL.
if I checked the GetLastError(), it returns 0( SUCCESS).window creation is failed but
GetLastError() also not returns valid Error information.
I handled the WM_NCCREATE message within a Window Procedure.
solution :
within the WindowProc() fn , I handled the WM_NCCREATE fn as follows :
case WM_NCCREATE :
break;
This is the reason for window creation failure. if we handled this WM_NCCREATE fn,
we must return TRUE. then only window creation will succeeds.
I created the window using CreateWindow() fn.CreateWindow() fn returns NULL.
if I checked the GetLastError(), it returns 0( SUCCESS).window creation is failed but
GetLastError() also not returns valid Error information.
I handled the WM_NCCREATE message within a Window Procedure.
solution :
within the WindowProc() fn , I handled the WM_NCCREATE fn as follows :
case WM_NCCREATE :
break;
This is the reason for window creation failure. if we handled this WM_NCCREATE fn,
we must return TRUE. then only window creation will succeeds.
Tuesday, June 26, 2007
VC++ Win32 Dialog box
VC++ Win32 Dialog box :
-------------------------------------------
Dialog Box :
1.A dialog box generally takes the form of a popup window containing various child window controls.
2.The size and placement of these controls are specified in a "dialog box template" in the program's resource script file. (.rc)
3.window procedure for processing the dialog box messages is referred as the "dialog Procedure."
4.Dialog procedures generally do not process WM_PAINT messages.
5.dialog box can be considered as an input/output "black box" meaning that you don't have to know how a dialog box works internally in order to be able to use it, you only have to know how to interact with it.That's a principle of object oriented programming (OOP) called information hiding.
6.A dialog box is defined as a resource much the same way as a menu. You write a dialog box template describing the characteristics of the dialog box and its controls and then compile the resource script with a resource editor.
7.all resources are put together in the same resource script file.
two main types of dialog box:
1.modal
2.modeless.
Modeless dialog box :
A modeless dialog box lets you change input focus to other window. The example is the Find dialog of MS Word.
Modal dialog box :
two subtypes of modal dialog box:
1.application modal
2.system modal
An application modal dialog box doesn't let you change input focus to other window in the same application but you can change the input focus to the window of OTHER application.
A system modal dialog box doesn't allow you to change input focus to any other window until you respond to it first.
A modeless dialog box is created by calling CreateDialogParam API function.
A modal dialog box is created by calling DialogBoxParam API function.
The only distinction between an application modal dialog box and a system modal one is the DS_SYSMODAL style. If you include DS_SYSMODAL style in a dialog box template, that dialog box will be a system modal one.
-------------------------------------------
Dialog Box :
1.A dialog box generally takes the form of a popup window containing various child window controls.
2.The size and placement of these controls are specified in a "dialog box template" in the program's resource script file. (.rc)
3.window procedure for processing the dialog box messages is referred as the "dialog Procedure."
4.Dialog procedures generally do not process WM_PAINT messages.
5.dialog box can be considered as an input/output "black box" meaning that you don't have to know how a dialog box works internally in order to be able to use it, you only have to know how to interact with it.That's a principle of object oriented programming (OOP) called information hiding.
6.A dialog box is defined as a resource much the same way as a menu. You write a dialog box template describing the characteristics of the dialog box and its controls and then compile the resource script with a resource editor.
7.all resources are put together in the same resource script file.
two main types of dialog box:
1.modal
2.modeless.
Modeless dialog box :
A modeless dialog box lets you change input focus to other window. The example is the Find dialog of MS Word.
Modal dialog box :
two subtypes of modal dialog box:
1.application modal
2.system modal
An application modal dialog box doesn't let you change input focus to other window in the same application but you can change the input focus to the window of OTHER application.
A system modal dialog box doesn't allow you to change input focus to any other window until you respond to it first.
A modeless dialog box is created by calling CreateDialogParam API function.
A modal dialog box is created by calling DialogBoxParam API function.
The only distinction between an application modal dialog box and a system modal one is the DS_SYSMODAL style. If you include DS_SYSMODAL style in a dialog box template, that dialog box will be a system modal one.
Thursday, April 19, 2007
cbClsExtra parameter in WNDCLASS structure
I have created a custom control with win32. It is a fill bar control.
I want to set the color of the fill color at design time. I put the
color value in the WNDCLASS in the cbClsExtra or cbWndExtra parameter.
How can the custom control get the values of these 2 parameters?
I know i can also send the color value with a message to the custom
control, but that is not what i want. I have found a function named :
GetWindowLong , but i don't know how to use this function and if it
is the right one to get those 2 parameters.
Answers :
Get/SetClassLong and Get/SetWindowLong are the right functions to access
this data. Using them is pretty straigtforward - the second parameter
is an offset to where you want to start reading data. If you allocate
8 extra bytes in cbWndExtra you would access them like this:
lValue1 = GetWindowLong(hwnd, 0);
lValue2 = GetWindowLong(hwnd, 4);
and the same w/ cbClsExtra and GetClassLong. Win32 also provides a bunch
of predefined values like GCL_STYLE and GCL_WNDPROC which give you access
to default data that is available for all windows and window classes. All
of the default values are negative numbers so as not to clash with accessing
extra user data bytes.
Answer2
You're not using the Extra parameters correctly. Those
values are used to reserve extra bytes in the block of
memory associated with the class or window. You can
then store things later in that location with SetWindowLong()
or SetClassLong() and retrieve with GetWindowLong()
or GetClassLong().
So if you say:
WNDCLASS wc;
wc.cbWndExtra=4;
then it reserves an extra 4 bytes for you. That's big enough
to hold a pointer to a block of memory if you so desire.
If all you have is a COLORREF, then 4 bytes will handle
that nicely. For clarity in your code, you should use:
wc.cbWndExtra=sizeof(COLORREF);
When you call CreateWindow(), you can pass a 4-byte
value as the last parameter, i.e.:
CreateWindow("mycustomcontrol",.....,RGB(50,100,150));
At WM_CREATE, you pick up the COLORREF and store it:
case WM_CREATE:
{LPCREATESTRUCT cs=(LPCREATESTRUCT)lparam;
COLORREF colour=cs->lpCreateParams;
SetWindowLong(hwnd,0,colour); //offset 0
}
break;
at any given time, you can retrieve it with:
case WM_someothermessage:
COLORREF colour=GetWindowLong(hwnd,0); //offset 0
break;
The classic book for learning Windows programming is
"Programming Windows 95" by Charles Petzold (Microsoft
Press). Almost everyone recommends this book, as do I.
It has excellent examples - source & .exe included.
I want to set the color of the fill color at design time. I put the
color value in the WNDCLASS in the cbClsExtra or cbWndExtra parameter.
How can the custom control get the values of these 2 parameters?
I know i can also send the color value with a message to the custom
control, but that is not what i want. I have found a function named :
GetWindowLong , but i don't know how to use this function and if it
is the right one to get those 2 parameters.
Answers :
Get/SetClassLong and Get/SetWindowLong are the right functions to access
this data. Using them is pretty straigtforward - the second parameter
is an offset to where you want to start reading data. If you allocate
8 extra bytes in cbWndExtra you would access them like this:
lValue1 = GetWindowLong(hwnd, 0);
lValue2 = GetWindowLong(hwnd, 4);
and the same w/ cbClsExtra and GetClassLong. Win32 also provides a bunch
of predefined values like GCL_STYLE and GCL_WNDPROC which give you access
to default data that is available for all windows and window classes. All
of the default values are negative numbers so as not to clash with accessing
extra user data bytes.
Answer2
You're not using the Extra parameters correctly. Those
values are used to reserve extra bytes in the block of
memory associated with the class or window. You can
then store things later in that location with SetWindowLong()
or SetClassLong() and retrieve with GetWindowLong()
or GetClassLong().
So if you say:
WNDCLASS wc;
wc.cbWndExtra=4;
then it reserves an extra 4 bytes for you. That's big enough
to hold a pointer to a block of memory if you so desire.
If all you have is a COLORREF, then 4 bytes will handle
that nicely. For clarity in your code, you should use:
wc.cbWndExtra=sizeof(COLORREF);
When you call CreateWindow(), you can pass a 4-byte
value as the last parameter, i.e.:
CreateWindow("mycustomcontrol",.....,RGB(50,100,150));
At WM_CREATE, you pick up the COLORREF and store it:
case WM_CREATE:
{LPCREATESTRUCT cs=(LPCREATESTRUCT)lparam;
COLORREF colour=cs->lpCreateParams;
SetWindowLong(hwnd,0,colour); //offset 0
}
break;
at any given time, you can retrieve it with:
case WM_someothermessage:
COLORREF colour=GetWindowLong(hwnd,0); //offset 0
break;
The classic book for learning Windows programming is
"Programming Windows 95" by Charles Petzold (Microsoft
Press). Almost everyone recommends this book, as do I.
It has excellent examples - source & .exe included.
Client rectangle size and Meta files
GetClientRect()
function for determining the dimensions of the client area. There's nothing really wrong with this function, but it's a bit inefficient to call it every time you need to use this information. A much better method for determining the size of a window's client is to process the WM_SIZE message within your window procedure. Windows sends a WM_SIZE message to a window procedure whenever the size of the window changes. The lParam variable passed to the window procedure contains the width of the client area in the low word and the height in the high word. To save these dimensions, you'll want to define two static variables in your window procedure:
static int cxClient, cyClient ;
Like cxChar and cyChar, these variables are defined as static because they are set while processing one message and used while processing another message. You handle the WM_SIZE method like so:
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
How can we use meta files in VC++ SDK programming :
Old Meta file usage :
-----------------------------
1.
HDC hdcMeta;
HMETAFILE hmf;
hdcMeta = CreateMetaFile (NULL);
MoveToEx (hdcMeta, 0, 0, NULL) ;
LineTo (hdcMeta, 100, 100) ;
hmf = CloseMetaFile(hdcMeta);
2. For Displaying Meta file :
--------------------------------------
PlayMetaFile(hdc, hmf);
Enhanced Meta files :
----------------------------------
static HENHMETAFILE hemf ;
HDC hdc, hdcEMF ;
hdcEMF = CreateEnhMetaFile (NULL, NULL, NULL, NULL) ; or
hdcEMF = CreateEnhMetaFile (NULL, TEXT ("emf2.emf"), NULL,
TEXT ("EMF2\0EMF Demo #2\0")) ;
Rectangle (hdcEMF, 100, 100, 200, 200) ;
hemf = CloseEnhMetaFile (hdcEMF) ;
Displaying Meta File :
------------------------------
GetClientRect (hwnd, &rect) ;
rect.left = rect.right / 4 ;
rect.right = 3 * rect.right / 4 ;
rect.top = rect.bottom / 4 ;
rect.bottom = 3 * rect.bottom / 4 ;
PlayEnhMetaFile (hdc, hemf, &rect) ;
function for determining the dimensions of the client area. There's nothing really wrong with this function, but it's a bit inefficient to call it every time you need to use this information. A much better method for determining the size of a window's client is to process the WM_SIZE message within your window procedure. Windows sends a WM_SIZE message to a window procedure whenever the size of the window changes. The lParam variable passed to the window procedure contains the width of the client area in the low word and the height in the high word. To save these dimensions, you'll want to define two static variables in your window procedure:
static int cxClient, cyClient ;
Like cxChar and cyChar, these variables are defined as static because they are set while processing one message and used while processing another message. You handle the WM_SIZE method like so:
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
How can we use meta files in VC++ SDK programming :
Old Meta file usage :
-----------------------------
1.
HDC hdcMeta;
HMETAFILE hmf;
hdcMeta = CreateMetaFile (NULL);
MoveToEx (hdcMeta, 0, 0, NULL) ;
LineTo (hdcMeta, 100, 100) ;
hmf = CloseMetaFile(hdcMeta);
2. For Displaying Meta file :
--------------------------------------
PlayMetaFile(hdc, hmf);
Enhanced Meta files :
----------------------------------
static HENHMETAFILE hemf ;
HDC hdc, hdcEMF ;
hdcEMF = CreateEnhMetaFile (NULL, NULL, NULL, NULL) ; or
hdcEMF = CreateEnhMetaFile (NULL, TEXT ("emf2.emf"), NULL,
TEXT ("EMF2\0EMF Demo #2\0")) ;
Rectangle (hdcEMF, 100, 100, 200, 200) ;
hemf = CloseEnhMetaFile (hdcEMF) ;
Displaying Meta File :
------------------------------
GetClientRect (hwnd, &rect) ;
rect.left = rect.right / 4 ;
rect.right = 3 * rect.right / 4 ;
rect.top = rect.bottom / 4 ;
rect.bottom = 3 * rect.bottom / 4 ;
PlayEnhMetaFile (hdc, hemf, &rect) ;
Thursday, April 05, 2007
Adding file Dialog to the file source filter
Win32 API :
ChooseFont() - Display Font Dialog
ChooseColor() - Display color dialog
GetOpenFileName() -Display File dialog
Learnt today...:
1.Added the file dialog to the Image Source Filter
1.Before creating an instance of the filter, we must prompt the user for selecting the file from File Dialog...
2.So i displayed the File Dialog before the filter's CreateInstance() fn...
3.Initialize the filter intsance in constructor initialization like
CMySourceStream::CMySourceStream(HRESULT* phr,CSourceFilter* pFilter,LPCWSTR pPinName):CSourceStream(NAME("SourceStream"),phr,pFilter,pPinName),
m_pFilter((CSourceFilter*)pFilter)
{
}
if we initialized the filter instance like this, I faced the problem that the filter instance is already locked ...
So use the above approach...
CMySourceStream::CMySourceStream(HRESULT* phr,CSourceFilter* pFilter,LPCWSTR pPinName):CSourceStream(NAME("SourceStream"),phr,pFilter,pPinName)
{
m_pFilter = (CSourceFilter*)pFilter;)
}
4.Display the file dialog...
OPENFILENAME ofn;
char szFileName[MAX_PATH]="";
char *szFile;
szFile = new char[260];
int i =0,j = 0;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
ofn.hwndOwner = ::GetActiveWindow();
ofn.lpstrFilter = "Bitmap(*.bmp)\0*.bmp\0jpg Files (*.jpg)\0*.jpg\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "bmp";
if(GetOpenFileName(&ofn)) //Displays file dialog...
{
// Do something useful with the filename stored in szFileName
//if(szFileName))
//OutputDebugString(szFileName);
while(szFileName[i] != NULL)
{
szFile[j] = szFileName[i];
if (szFileName[i] == '\\')
{
j= j+1;
szFile[j] = '\\';
}
i = i + 1;
j = j + 1;
}
szFile[i + 1] = NULL;
OutputDebugString(szFile);
return szFile;
}
else
{
return NULL;
}
}
if we are not assigned the data
char szFileName[MAX_PATH]="";
ofn.lpstrFile = szFileName;
the szFileName must be assigned some blank values otherwise the file dialog will not be shown
on the screen...
ChooseFont() - Display Font Dialog
ChooseColor() - Display color dialog
GetOpenFileName() -Display File dialog
Learnt today...:
1.Added the file dialog to the Image Source Filter
1.Before creating an instance of the filter, we must prompt the user for selecting the file from File Dialog...
2.So i displayed the File Dialog before the filter's CreateInstance() fn...
3.Initialize the filter intsance in constructor initialization like
CMySourceStream::CMySourceStream(HRESULT* phr,CSourceFilter* pFilter,LPCWSTR pPinName):CSourceStream(NAME("SourceStream"),phr,pFilter,pPinName),
m_pFilter((CSourceFilter*)pFilter)
{
}
if we initialized the filter instance like this, I faced the problem that the filter instance is already locked ...
So use the above approach...
CMySourceStream::CMySourceStream(HRESULT* phr,CSourceFilter* pFilter,LPCWSTR pPinName):CSourceStream(NAME("SourceStream"),phr,pFilter,pPinName)
{
m_pFilter = (CSourceFilter*)pFilter;)
}
4.Display the file dialog...
OPENFILENAME ofn;
char szFileName[MAX_PATH]="";
char *szFile;
szFile = new char[260];
int i =0,j = 0;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
ofn.hwndOwner = ::GetActiveWindow();
ofn.lpstrFilter = "Bitmap(*.bmp)\0*.bmp\0jpg Files (*.jpg)\0*.jpg\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "bmp";
if(GetOpenFileName(&ofn)) //Displays file dialog...
{
// Do something useful with the filename stored in szFileName
//if(szFileName))
//OutputDebugString(szFileName);
while(szFileName[i] != NULL)
{
szFile[j] = szFileName[i];
if (szFileName[i] == '\\')
{
j= j+1;
szFile[j] = '\\';
}
i = i + 1;
j = j + 1;
}
szFile[i + 1] = NULL;
OutputDebugString(szFile);
return szFile;
}
else
{
return NULL;
}
}
if we are not assigned the data
char szFileName[MAX_PATH]="";
ofn.lpstrFile = szFileName;
the szFileName must be assigned some blank values otherwise the file dialog will not be shown
on the screen...
Subscribe to:
Posts (Atom)