How to set focus to a control in the dialog startup
Setting focus to a dialog control is more than just call SetFocus.
Normally, the focus is set to a control which was higher in z-order. But we can set focus in two ways.
- Call SetFocus() function to desired control in OnInitDialog and return FALSE.
- Call ::PostMessage( this->m_hWnd, WM_NEXTDLGCTL, (WPARAM)( GetDlgItem( IDC_BUTTON1 )->m_hWnd ), TRUE ); in OnInitDialog() and return TRUE. Don’t call SendMessage instead of PostMessage.
See Assembly code generated for your source code
To study internals, we often need to refer assembly code.
Method to generate assembly code
1. Take “Project settings” by Alt+F7
2. Go to “C/C++” tab.
3. Select “Listing Files” from Category combo box.
4. Select “Assembly with source Code” form Listing file type combo box.
5. Recompile the project.
6. Take the debug or release directory, you can see asm files
This can be done in another way, Just add /FAs to the “Project Options” edit box in the “C/C++” tab.
Difference between ::PostMessage and ::SendMessage APIs
The difference between these two APIs is that the way they return control to the calling application. With SendMessage, the control doesn’t return to the calling application until the window process the message. However, PostMessage returns the control immediately after the message push to the windows message queue regardless whether the sent message has been processed or not.
Windows API reference
Windows API reference Windows API reference
What is the difference between UNICODE and _UNICODE
The purpose of the both preprocessor macros are same.
UNICODE is used by windows headers and _UNICODE is used by C-Runtime/MFC headers.
CDC::DrawText
The syntax
int DrawText( const CString& str, LPRECT lpRect, UINT nFormat );
We dont know the width and height of the rectangle where the text to display, so that the lpRect is empty
Then how calculate the text area ?
It is simple,
Eg:
CRect rcText( 0, 0, 0 ,0 );
dc.DrawText( csText, &rcText, DT_CALCRECT );
dc.DrawText( csText, &rcText, DT_CENTER );
The first DrawText fills rcText with required value, ie the hight of the text, width of the text.
Call next DrawText, then the text will display.
The return of the DrawText is the height of the text. But we also have to calculate the width of the text. So the above method is easy to use.
Get Icon of a process
Use ExtractIconEx function to get icon of a process. It will return both small and large icon of the specified process.
::ExtractIconEx( szProcessName, 0, &hSmallIcon, &hLargeIcon, 1 ) ;
You must destroy all icons extracted by ExtractIconEx by calling the DestroyIcon function.
Why this program is not crashing up?
class Test
{
private:
int m_Data;
void Disp( int n)
{
int i = n + 100;
cout << “Data = ” << i;
}
};
int main()
{
Test* t = 0;
t->Disp( 100 );
}
What is the output of this function? Will it crash?
This program outputs Data = 200 Why?
Normally we think program will crash if function call with a null pointer. But if you know the internal working of member function call, it will easy to catch my words.
As you all know, there is only one definition of member function in memory. All the instance of the class uses this memory.
t->Disp( 100 ) will replaced by compiler as Test::Disp( Test* this, int n ) ( Note that every non static member function will have “this” pointer to refer the calling object itself ).
Then this is null pointer and n is 100. Inside the Disp function we just manipulate data that don’t depends the class. So there is no chance for crash.
But if we call member data inside the Disp function, the scene will change, this->m_Data surely crash the program.
Windows Handle
In windows OS, every resource is an “object” identified and referenced by handle of type HANDLE.
In 32 bit machine, it is a 32 bit value. HANDLE is just a typedef of void pointer.
This type is declared in WinNT.h as typedef PVOID HANDLE;
Usually, the HANDLE is wrapped in an instance of a class. CWnd is a good example; it contains HWND which is a handle to a window. Here CWnd brings the object oriented concept. For exapmle to show a window,
pWnd->ShowWindow( SW_SHOW ); this is exactly same as ::ShowWindow( pWnd->m_hWnd, SW_SHoW );
Kernal objects must be manipulated by HANDLES contains:
files, thread, memory,events,mutexes,semaphores,pipes,processes
Hello world!
Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!
-
Archives
- September 2008 (1)
- August 2008 (4)
- July 2008 (5)
-
Categories
-
RSS
Entries RSS
Comments RSS