Duilib parent window invalidation and message passing

1. Parent window invalidation and message passing

When using the duillib interface library, we often need to create multiple windows. There are certain logic requirements between the child window and the parent window. For example, when the child window pops up, the parent window cannot process the messages of the controls in the parent window, that is, the parent window is invalidated until the child window is closed and then restored.

To create multiple windows, we can define multiple window classes for one-to-one correspondence. When I created the window class, I inherited the window class WindowImpBase encapsulated in the duilib library . By overloading the message processing function in the parent class, you can filter the message.

     virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);//消息处理函数

Invalidate the parent window before creating the child window, and then restore the parent window and get the focus in the WM_CLOSE message processing of the child window. code show as below:

void MainFrameWnd::Notify(TNotifyUI& msg){
    
    
	if (msg.sType == DUI_MSGTYPE_CLICK){
    
    
		if (msg.pSender == close_btn_){
    
    
			SendMessage(WM_SYSCOMMAND, SC_CLOSE, 0);
			return;
		}
		else if (msg.pSender == min_btn_){
    
    
			SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
			return;
		}
		else if (msg.pSender == max_btn_){
    
    
			SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
			return;
		}
		else if (msg.pSender == restore_btn_){
    
    
			SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
			return;
		}
		else if (msg.pSender == mul_btn_){
    
    
			::EnableWindow(this->GetHWND(), false);    //父窗口无效化
			if (second_frame_wnd_ == nullptr){
    
    
				second_frame_wnd_ = new SecondFrameWnd();
				second_frame_wnd_->Create(this->GetHWND(), SecondFrameWnd::kClassName_, UI_WNDSTYLE_FRAME, 0);
			}
			second_frame_wnd_->CenterWindow();
			second_frame_wnd_->ShowWindow();
		}
	}
	__super::Notify(msg);
}

LRESULT MainFrameWnd::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){
    
    
	if (uMsg == WM_CLOSE){
    
    
		PostQuitMessage(0L);
	}
	return __super::OnClose(uMsg, wParam, lParam, bHandled);
}

LRESULT MainFrameWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam){
    
    
	//switch (uMsg)
	//{
    
    
	//case WM_CREATE:
	//	//其他初始化操作
	//	break;
	//case WM_KEYDOWN:{
    
    
	//	//处理键盘按下操作
	//	//VK_ESCAPE Esc键
	//	if (wParam == VK_ESCAPE){
    
    
	//		return 0;
	//	}
	//	break;
	//}
	//case WM_CLOSE:
	//	//处理窗口关闭事件
	//	break;
	//default:
	//	break;
	//}
	//return __super::HandleMessage(uMsg, wParam, lParam);                             //调用父类的消息处理函数

	if (uMsg == WM_CLOSE)
	{
    
    
		::EnableWindow(::GetParent(this->GetHWND()), TRUE);  // 父窗口还原 m_hwnd为子窗口句柄
		::SetFocus(::GetParent(this->GetHWND()));//设置焦点到父窗口
	}
	return __super::HandleMessage(uMsg, wParam, lParam);

}

In this way, the invalidation of the parent window can be realized, and a little feedback can be given to the user.

What I design here is to add a Control control in the general layout of the parent window, let it cover the entire parent window, initialize it as invisible, and set the background color to translucent black. When the parent window is invalidated, set the Control control to be visible, so that the parent window becomes gray, telling the user that the parent window is unavailable, and then make it invisible after restoring, thus realizing a feedback of invalidating the parent window. There is no code to write here, because it is very simple to implement.
insert image description here

2. The meaning and usage of EnableWindow() and SetFocus()

The EnableWindow() function is used to enable or disable a window or control. When the incoming parameter is TRUE , the window or control will be enabled and can respond to user input; when the incoming parameter is FALSE , the window or control will be disabled and no longer respond to user input.

  • The calling format of this function is: EnableWindow(hWnd, bEnable) , where hWnd is the handle of the window or control, and bEnable is a BOOL value.
::EnableWindow(::GetParent(this->GetHWND()), TRUE);  // 父窗口还原 m_hwnd为子窗口句柄

The SetFocus() function is used to set the focus to the specified window or control. When this function is called, the specified window or control becomes the currently active window and receives user input. The calling format of this function is: SetFocus(hWnd) , where hWnd is the handle of the window or control.

::SetFocus(::GetParent(this->GetHWND()));//设置焦点到父窗口

These functions can also be used in DuiLib, by obtaining the handle of the control (you can use methods such as **FindControl()** provided by DuiLib ) to operate the enabled state and focus of the window or control.

Guess you like

Origin blog.csdn.net/qq_44918090/article/details/131723739