C # How to handle the parent window and Its subwindow title

Source: Internet
Author: User

If you use MFC to write too many document interfaces (MDI) Windows programs, you must know that if the parent window is titled "PCaption" and the Child Window is titled "CCaption ", when the sub-window is maximized and activated, the sub-window title is usually combined with the parent window title to "PCaption-[CCaption]". This is a default behavior of MDI. Writing multi-Document Interface programs with C # is no exception. Many users do not like this default feature and often want to replace it with custom window titles. This article will demonstrate how to customize and modify the window title of the MDI Application in the C # program.
If you use MFC for programming, you only need to rewrite the virtual function CFrameWnd: OnUpdateFrameTitle of the framework window class. So how can C # be used to implement the same functions in Microsoft's. NET Framework? First, we must understand how MDI implements its own behavior characteristics through the core Windows API. In fact, this has nothing to do with the Common Language Runtime (CLR) of MFC or. NET. When creating an MDI application, the framework and its subwindows have their own special window processes, DefFrameProc and DefMDIChildProc. One processes various WM_MDIXXX messages and other messages similar to WM_SIZE and WM_SYSCOMMAND, And the other implements MDI behavior.

If you use pure C code, you must use DefFrameProc and DefMDIChildProc to create windows. In MFC, use CMDIFrameWnd/CMDIChildWnd ;. in the. NET Framework Platform, set Form. isMdiContainer and Form. no matter which method is used, the core of MdiParent is user kernel, especially DefFrameProc. When the MDI subwindow is maximized, it is connected to the title Text of the Parent and Child windows to generate the title string of the main window. After understanding this, I will demonstrate how to rewrite the MDI. The original version of this example is from the Scribble MDI written in C # In the MSDN Library (you can find it by searching "scribble sample ). The basic idea is to first rewrite the WM_GETTEXT message processing routine in the MainWindow of the Scribble example. You must add two data members: NormalText and MaximizedText, and use them to save the title of the normal and maximized status:

// In Scribble. cs, MainWindow class
Private String NormalText = "Scribble2 ";
Private String MaximizedText = "Window is now maximized ";

If you want other classes to access these two members, you can use the attribute mechanism to replace the data members, such:

Private String normaltext;
Public String NormalText
{
Get {return normaltext ;}
Set {normaltext = value ;}
}

In the example program, MainWindow is the only class to access this string, so there is no need to use the attribute mechanism. With these two new data members, all you have to do is rewrite the WM_GETTEXT processing routine and return the subwindow maximization status and the title text in normal conditions. So how can we rewrite the WM_GETTEXT processing routine?



Windows. Forms provides some virtual functions for processing WM_XXX messages, such as OnResize/WM_SIZE, but does not have something related to WM_GETTEXT (OnGetText/WM_GETTEXT ). Don't worry. Without virtual functions, we can always rewrite all-embracing WndProc processing routines. Therefore, you must know the ID of the message to be processed, that is, the Message ID of WM_GETTEXT = 0x000D. Someone may ask how do you know that the Message ID is 0x000D, which is very simple, one method is to run SPY to obtain the data, and the other method is to directly find winuser in Windows SDK. h header file. Once you can write code at the WndProc level, you can basically write a program in the C language, because the Win32 API and all other languages are passed through the WPARAMs and LPARAMs parameters, including strings. For WM_GETTEXT, Message. LParam is a pointer to char *, and Message. WParam is the length of the pointer. That is to say, you must copy the text string to the caller's buffer. Fortunately, this is not too difficult. The following is the program code:

Public class MainWindow: System. Windows. Forms. Form
{
Private String NormalText = "Scribble2 ";
Private String MaximizedText = "Window is now maximized ";

// Handle WM_GETTEXT: Return maximized or
// Normal text, depending on
// State of active MDI child window.
Protected override void WndProc (ref Message m)
{
Const int WM_GETTEXT = 0x000D;
If (m. Msg = WM_GETTEXT ){
Form active = this. ActiveMdiChild;
String s = active! = Null &&
Active. WindowState = FormWindowState. Maximized? MaximizedText:
NormalText;
Char [] c = s. ToCharArray ();
IntPtr buf = m. LParam;
Int len = c. Length;
Marshal. Copy (c, 0, buf, Math. Min (int) m. WParam, len ));
M. Result = (IntPtr) len;
Return;
}
Base. WndProc (ref m );
}

... // Rest of MainWindow unchanged from Scribble sample

}

After the above changes, the program is now running. When the MDI sub-Window is maximized, the text displayed in the title of the main Window is "Window is now maximized", as shown in 1,


Figure 1 title of the main window when the sub-window is maximized

When the two windows are in normal state, the following figure is displayed:


Figure 2 title of two subwindows in normal conditions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.