MFC create controls in code, create custom registration window

Manually create the control flow

  1. Create a class member variable control type
    figure are controls in addition to m_hicn
    Here Insert Picture Description
  2. Each type of control, create the following code:
	CFont* pFont = GetFont();
	m_list2.Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT, CRect(11, 64, 429, 290), this, 12340);
	m_list2.SetFont(pFont);
	
	m_edit2.Create(WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL | ES_AUTOHSCROLL | WS_BORDER, CRect(57, 11, 137, 31), this, 12350);
	m_edit2.SetFont(pFont);

	m_button2.Create(L"button2", WS_CHILD | WS_VISIBLE | WS_TABSTOP, CRect(98, 36, 173, 58), this, 12341);
	m_button2.SetFont(pFont);

	m_combobox2.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | CBS_DROPDOWNLIST, CRect(11, 36, 86, 58), this, 12345);
	m_combobox2.SetFont(pFont);

	m_static2.Create(L"static2", WS_CHILD | WS_VISIBLE, CRect(146, 11, 183, 23), this, 12346);
	m_static2.SetFont(pFont);

Each type of control is not difficult to see the Create function both interrelated and distinguished from each other. They have, window size and position, style, the parent window of the definition of their own ID, but some controls to create the content of this text parameters, some not, and each of them has its own unique control style, CComboBox have CBS_DROPDOWNLIST , CEdit have ES_AUTOHSCROLL, CListCtrl have LVS_REPORT and so on. Careful excavation, we know that these are overridden Create parent CWnd Create function.

Sign up to create custom window

In MSDN

CWnd::Create

virtual BOOL Create( LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);

Return Value

Nonzero if successful; otherwise 0.

Parameters

lpszClassName

Points to a null-terminated character string that names the Windows class (a WNDCLASS structure). The class name can be any name registered with the global AfxRegisterWndClass function or any of the predefined control-class names. If NULL, uses the default CWnd attributes.

lpszWindowName

Points to a null-terminated character string that contains the window name.

dwStyle

Specifies the window style attributes. WS_POPUP cannot be used. If you wish to create a pop-up window, use CWnd::CreateEx instead.

rect

The size and position of the window, in client coordinates of pParentWnd.

pParentWnd

The parent window.

nID

The ID of the child window.

pContext

The create context of the window.

Remarks

Creates a Windows child window and attaches it to the CWnd object.

You construct a child window in two steps. First, call the constructor, which constructs the CWnd object. Then call Create, which creates the Windows child window and attaches it to CWnd. Create initializes the window’s class name and window name and registers values for its style, parent, and ID.

Example

// Dynamically create static control using CWnd::Create,
// instead of with CStatic::Create, which doesn’t
// need the “STATIC” class name.

void CMyDlg::OnCreateStatic()
{
CWnd* pWnd = new CWnd;
pWnd->Create(_T(“STATIC”), “Hi”, WS_CHILD | WS_VISIBLE,
CRect(0, 0, 20, 20), this, 1234);
}

CWnd :: Create () function to create a child window is limited to include all the controls, including generally not used to create the main window. Creates the main window of the task handed over to CWnd :: CreateEx ()

CWnd::CreateEx

BOOL CreateEx( DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hwndParent, HMENU nIDorHMenu, LPVOID lpParam = NULL );

BOOL CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, LPVOID lpParam = NULL);

Return Value

Nonzero if successful; otherwise 0.

Parameters

dwExStyle

Specifies the extended style of the CWnd being created. Apply any of the extended window styles to the window.

lpszClassName

Points to a null-terminated character string that names the Windows class (aWNDCLASS structure). The class name can be any name registered with the global AfxRegisterWndClass function or any of the predefined control-class names. It must not be NULL.

lpszWindowName

Points to a null-terminated character string that contains the window name.

dwStyle

Specifies the window style attributes. See Window Styles and CWnd::Create for a description of the possible values.

x

Specifies the initial x-position of the CWnd window.

Y

Specifies the initial top position of the CWnd window.

nWidth

Specifies the width (in device units) of the CWnd window.

nHeight

Specifies the height (in device units) of the CWnd window.

hwndParent

Identifies the parent or owner window of the CWnd window being created. Use NULL for top-level windows.

nIDorHMenu

Identifies a menu or a child-window identifier. The meaning depends on the style of the window.

lpParam

Points to the data referenced by the lpCreateParams field of the CREATESTRUCT structure.

rect

The size and position of the window, in client coordinates of pParentWnd.

pParentWnd

The parent window.

nID

The ID of the child window.

Remarks

Creates an overlapped, pop-up, or child window with the extended style specified in dwExStyle.

The CreateEx parameters specify the WNDCLASS, window title, window style, and (optionally) initial position and size of the window. CreateEx also specifies the window’s parent (if any) and ID.

When CreateEx executes, Windows sends the WM_GETMINMAXINFO, WM_NCCREATE, WM_NCCALCSIZE, and WM_CREATE messages to the window.

To extend the default message handling, derive a class from CWnd, add a message map to the new class, and provide member functions for the above messages. Override OnCreate, for example, to perform needed initialization for a new class.

Override further OnMessage message handlers to add further functionality to your derived class.

If the WS_VISIBLE style is given, Windows sends the window all the messages required to activate and show the window. If the window style specifies a title bar, the window title pointed to by the lpszWindowName parameter is displayed in the title bar.

The dwStyle parameter can be any combination of window styles.

Example

void CMyDlg::OnCreateExtendedCtrl()
{
CWnd* pWnd = new CStatic;
pWnd->CreateEx(WS_EX_CLIENTEDGE, // Make a client edge label.
_T(“STATIC”), “Hi”,
WS_CHILD | WS_TABSTOP | WS_VISIBLE,
5, 5, 30, 30, m_hWnd, (HMENU)1234);
}

English documents also taken over MSDN, there are some noteworthy point, I would like to remind you:

  • CreateEx () is mainly used to create the main window, Create () is used for the child window (including controls).
  • Create () in nID is not NULL, who should have a child window ID is used to identify the identity of the
  • Create () in the parent window is not NULL, it may be a desktop
  • CreateEx () must be NULL when the primary window nID
  • Create () is not available in the style pop, Create () function is in fact an internal call CreateEx ()

Vs created by mfc dialog-based application, the following functions will be converted as shown in the following:

BOOL CMFCApplication1App::InitInstance()
{
	
	CWnd* wnd = new CWnd;
	m_pMainWnd = wnd;#程序需要指定主窗口
	CString szClass;

	HBRUSH Hbr = (HBRUSH)::GetStockObject(BLACK_BRUSH);

	HCURSOR Hcr = (HCURSOR)LoadStandardCursor(IDC_CROSS);

	HICON hico = LoadIcon(IDR_MAINFRAME);
	szClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,Hcr,Hbr,hico);
	
	wnd->CreateEx(0, szClass, L"First CreateEx window", WS_OVERLAPPEDWINDOW|WS_VISIBLE, CRect(0, 300, 600,900), NULL,0);
	
	
	/*wnd->Create(szClass, L"First Create window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CRect(0, 300, 600, 900), CWnd::FromHandle(GetDesktopWindow()), 12386);*/
	return TRUE;#此处必须为TRUE不然不启动消息泵

}

Published 15 original articles · won praise 21 · views 1672

Guess you like

Origin blog.csdn.net/qzonelaji/article/details/104088357