About us Principles Process Team Testimonials Partnership
PDA&Mobile Web SEO Java .NET Embedded Software testing Free Pilot Project Design Dedicated Teams
News Articles
Solutions Technologies Case Studies Knowledge Base
Custom software development company
Home > Expertise > Knowledge Base > Use of superclassing for restoration of entry focus in custom window

 Offshore custom software development services

Use of superclassing for restoration of entry focus in custom window

by Sergey Krotkikh
Senior Software Developer
QArea
We propose one of the ways to restore entry focus in a custom window after its activation.
Suppose, that only standard controls are used in a window.
They are WC_BUTTON, WC_COMBOBOX, WC_EDIT, WC_LISTBOX, WC_SCROLLBAR. After getting the idea of it, the method can be extended to any class of a child window that can be in entry focus.
For each of the controls stated above we create a superclass with the same name and a new window function. Window function will be uniform for all classes. It processes the message WM_SETFOCUS, at this stores the parameter of hWnd window and retrieves the control of parent class function.
Then the window function of a custom window, for which the focus is to be restored, in WM_ACTIVATE handler (or in WM_SETFOCUS) activates a function that restores focus on its element that was the last to receive message from WM_SETFOCUS.
Considering, that standard controls with WS_TABSTOP feature are compulsorily to receive WM_SETFOCUS message at Tab pressing, the method works perfectly.
Well, let us start with creating several structures:
typedef struct superclassdata
{    TCHAR ClassName[100];    /* Name of the class to be superclassed*/
     WNDPROC fnWndProc;    /* Window procedure of the parent class*/
}SCD;
SCD aSCD[MAX_SCD];    /* Array where we will store access points of window procedures*/

typedef struct parentwnd
{    HWND hWnd;    /* Custom window for which we will restore focus*/
     HWND hFocus;    /* window element that is in focus*/
}PWND;
PWND aPWND[MAX_PWND];    /* Array where the element in focus for each custom window is stored.*/
BOOL CreateSuperClass(TCHAR* lpClassName);
 /* Function which creates a superclass for the specified class (with name lpClassName)*/
long CALLBACK SuperClassWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
 /* Window function of superclasses (uniform for all)*/
VOID StartRestoreFocus(HWND hWnd, HWND hFocus);
 /*Launch the process. Function must be activated from WM_CREATE, or from another place, afterwards the procedure of focus restoration for hWnd window sets in. Parameter hFocus specifies an element which is the first to receive the focus. Parameter may not be specified, then function sets focus on the first child element that has WS_TABSTOP feature.*/
VOID StopRestoreFocus(HWND hWnd);
 /* Close up the process. Array element is discharged and the process of focus restoration for hWnd window is aborted.*/
 /* Function should be activated at processing of WM_DESTROY or similar location in a custom window message processing function.*/
 // Restore focus.
VOID RestoreFocus(HWND hWnd);
 /* Function should be activated in WM_SETFOCUS handler or in WM_ACTIVATE handler of hWnd custom window.
 Function restores focus on the element that was the last to receive it when the window was active.*/

First of all, it is necessary to register new classes of windows in the program:
 CreateSuperClass(WC_BUTTON);
 CreateSuperClass(WC_COMBOBOX);
 CreateSuperClass(WC_EDIT);
 CreateSuperClass(WC_LISTBOX);
 CreateSuperClass(WC_SCROLLBAR);

Let us put down the code in application initialization part, in WinMain.
Next, for a new window we add the code to its function:
...
switch (message)
{
case WM_CREATE:
{
...
StartRestoreFocus(hWnd,(HWND)NULL);
...
}
return 0;
...
case WM_SETFOCUS: // Possible in WM_ACTIVATE
{
RestoreFocus(hWnd);
break;
}
...
case WM_DESTROY: // Possible in WM_ACTIVATE
{
...
StopRestoreFocus(hWnd);
...
}
return 0;
...
}

Finally, the functions themselves.
BOOL CreateSuperClass(TCHAR* lpClassName)
{
int i;
WNDCLASS WndClass;
WndClass.style=(UINT)0;
if(GetClassInfo(hInstGlobal,(LPSTR) lpClassName,&WndClass))
{
if(bSCDInit)
{
for(i = 0;i < MAX_SCD; i++)
{
aSCD[i].ClassName[0]=_T('\0');
}
for(i = 0;i < MAX_PWND; i++)
{
aPWND[i].hWnd=(HWND)NULL;
aPWND[i].hFocus=(HWND)NULL;
}
bSCDInit=FALSE;
}
for(i = 0; i < MAX_SCD; i++)
if(aSCD[i].ClassName[0]==_T('\0'))
{
WndClass.hInstance=hInstGlobal;
WndClass.lpszClassName=lpClassName;
_tcscpy(aSCD[i].ClassName,lpClassName);
aSCD[i].fnWndProc=WndClass.lpfnWndProc;
WndClass.lpfnWndProc=(WNDPROC)SuperClassWndProc;
RegisterClass(&WndClass);
return TRUE;
}
}
return FALSE;
}

VOID StartRestoreFocus(HWND hWnd, HWND hFocus)
{
int i;
for(i=0;i<MAX_PWND;i++)
{
if (aPWND[i].hWnd==(HWND)NULL)
{
aPWND[i].hWnd=hWnd;
if (hFocus)
aPWND[i].hFocus=hFocus;
else
aPWND[i].hFocus=GetNextDlgTabItem(hWnd,(HWND)NULL,FALSE);
break;
}
}
}

VOID StopRestoreFocus(HWND hWnd)
{
int i;
for(i=0;i<MAX_PWND;i++)
{
if (aPWND[i].hWnd==hWnd)
{
aPWND[i].hWnd=(HWND)NULL;
aPWND[i].hFocus=(HWND)NULL;
break;
}
}
}

VOID RestoreFocus(HWND hWnd)
{
int i;
for(i=0;i<MAX_PWND;i++)
{
if(aPWND[i].hWnd==hWnd)
{
SetFocus(aPWND[i].hFocus);
break;
}
}
return;
}

long CALLBACK SuperClassWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i,j;
TCHAR t[MAX_PATH];
GetClassName(hWnd, t, MAX_PATH);
for(i=0;i<MAX_SCD;i++)
{
if(_tcscmp(aSCD[i].ClassName,t)==0)
{
switch(message)
{
case WM_SETFOCUS:
{
HWND hParent=hWnd;
BOOL bF=FALSE;
while (!bF && IsWindow(hParent))
{
for(j=0;j<MAX_PWND;j++)
{
if(aPWND[j].hWnd==hParent)
{
aPWND[j].hFocus=hWnd;
bF=TRUE;
break;
}
}
hParent=GetParent(hParent);
}
}
}
return CallWindowProc(aSCD[i].fnWndProc, hWnd, message, wParam, lParam);
}
}
return 0;
}
> QArea Newsletter
 
>NEWS
19-Oct-2009

QArea Offers 50 Hours Free Trial Testing Project to qualified and serious companies without any obligations and hidden costs.

19-Jan-2009

Skyhook Wireless appreciates  the high level of professionalism and quality of work provided by QArea team.

26-Nov-2008

Max Garkavtsev, the Founder of QArea, made some connections to business leaders and entrepreneurs at the Startup Networking Group Meeting.

05-Sep-2008
QArea became a Microsoft Certified Partner
14-Aug-2008

QArea to be a Vendor at the 9-th SoTeC showcase. This year's theme is "Capturing Summit - Expanding Knowledge - Achieving Goals".

24-Jul-2008

Max Garkavtsev, the Founder of QArea, met LA Angels and Ventures at the one of the largest Angels & Ventures Networking events.

21-Jul-2008

Max Garkavtsev, the Founder of QArea, attended the LAEDC Economic Forecast and Industry Outlook of Southern California.

09-Jul-2008

The Huffington Post plans on continuing to grow their business with QArea.

 
> Get in Touch
Offshore custom software development servicesRequest a Quote
Offshore custom software development servicesRequest a Call
Close window
* Indicates a required field
First Name*:
Last Name*:
E-mail*:
Position:
Company:
Website*:
City:
State:
Country:
Phone Number*:
+
What is your time zone?:
Suitable time for a phone call*:
What kind of information are you interested in?:
You may upload any relevant files if you wish.
The file size must not exceed 1 MB.
  
Close window
* Indicates a required field
First Name*:
Last Name*:
Position:
Company:
Website*:
City:
State:
Country:
E-mail*:
Phone Number*:
+
Project description*:
You may upload any relevant files if you wish.
The file size must not exceed 1 MB.
  


50 Hours Pilot Testing Project
Evaluate our capabilities, process and quality of work without any obligations and hidden costs. more













Custom software development company Offshore software development company  
Offshore custom software development services Software outsourcing company