在对话框中存在控件时,缺少单个(相同)控件 [英] Missing Single(Same) control while enmurating controls present in a dialog

查看:50
本文介绍了在对话框中存在控件时,缺少单个(相同)控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将根据对话框的高度和宽度将所有控件替换为对话框的中心。我这样做是通过使用:

 if(:: IsWindow(this-> GetSafeHwnd())&& :: IsWindow(GetDlgItem(nAnchorID) - > GetSafeHwnd()))
{
CRect wndRect;
CRect ctrlRect;
//获取锚帧的矩形和此窗口
this-> GetWindowRect(wndRect);
GetDlgItem(nAnchorID) - > GetWindowRect(& ctrlRect);
//将它的'coords转换为基于客户端的coords
ScreenToClient(& ctrlRect);
ScreenToClient(& wndRect);

int nNewX = 0;
int nNewY = 0;
int nXDiff = 0;
int nYDiff = 0;
int nWndWidth = wndRect.Width();
int nWndHeight = wndRect.Height();
int nCtrlWidth = ctrlRect.Width();
int nCtrlHeight = ctrlRect.Height();
bool bWndIsSmallerX = nWndWidth< nCtrlWidth;
bool bWndIsSmallerY = nWndHeight< nCtrlHeight;

nNewX =(bHorizo​​ntal&&!bWndIsSmallerX)? (int)((nWndWidth - nCtrlWidth)* 0.45):0;
nNewY =(bVertical&&!bWndIsSmallerY)? (int)((nWndHeight - nCtrlHeight)* 0.5):0;

nXDiff = nNewX - ctrlRect.left;
nYDiff = nNewY - ctrlRect.top;

HWND hwnd = :: GetTopWindow(this-> GetSafeHwnd());
while(hwnd)
{
UINT nID = :: GetDlgCtrlID(hwnd);
if(nID == nAnchorID)
GetDlgItem(nID) - > ShowWindow(GetDlgItem(nID) - > IsWindowVisible()?SW_HIDE:SW_SHOW);
else if(nID == IDC_UPLOW_CASE){
CString sText;
GetDlgItem(nID) - > GetWindowText(sText);
sText =(sText ==全部小写)? ALL UPPERCASE:全部小写;
SetDlgItemText(nID,(LPCTSTR)sText);
}

CRect rect;
:: GetWindowRect(hwnd,& rect);
ScreenToClient(& rect);
:: MoveWindow(hwnd,rect.left + nXDiff,rect.top + nYDiff,rect.Width(),rect.Height(),TRUE);
hwnd = :: GetNextWindow(hwnd,GW_HWNDNEXT);
}
}



但是我缺少一个控件。

任何人都可以帮我解决这个问题。< br $> b $ b



我只从Code Projects获得此代码...................

但是我在SDI MFC应用程序开发的自己的应用程序中使用此代码

解决方案

其实我(发布此问题的人)我犯了一个愚蠢的错误,我已经通过使用#define关键字声明了ANCHOR ID,并为此分配了一个值。我分配给ANCHOR ID的ID值与对话框中存在的CONTROL ID之一匹配,这样我的代码就运行了,我错过了ID值与ANCHOR ID相同的控件。

ANCHOR ID是我们的对话框中显示的一个框架,它覆盖了我们的对话框大小,以便我们可以使用该控件并计算我们需要移动的空间量。这样做的一个优点是控件的重叠不会发生。

你可以找到一个漂亮的例子,使用ANCHOR ID并枚举所有控件并将它们放在中心:

在对话框或FormView中枚举控件 [ ^ ]

I am replacing all controls to the center of the dialog based on the height and width of the dialog. I am doing this by using :

if (::IsWindow(this->GetSafeHwnd()) && ::IsWindow(GetDlgItem(nAnchorID)->GetSafeHwnd()))
	{
		CRect wndRect;
		CRect ctrlRect;
		// get the rect of the anchor frame and the this window
		this->GetWindowRect(wndRect);
		GetDlgItem(nAnchorID)->GetWindowRect(&ctrlRect);
		// convert it''s coords to client-based coords
		ScreenToClient(&ctrlRect);
		ScreenToClient(&wndRect);

		int nNewX  = 0;
		int nNewY  = 0;
		int nXDiff = 0;
		int nYDiff = 0;
		int nWndWidth   = wndRect.Width();
		int nWndHeight  = wndRect.Height();
		int nCtrlWidth  = ctrlRect.Width(); 
		int nCtrlHeight = ctrlRect.Height();
		bool bWndIsSmallerX = nWndWidth  < nCtrlWidth;
		bool bWndIsSmallerY = nWndHeight < nCtrlHeight;

		nNewX  = (bHorizontal && !bWndIsSmallerX) ? (int)((nWndWidth  - nCtrlWidth)  * 0.45) : 0;
		nNewY  = (bVertical   && !bWndIsSmallerY) ? (int)((nWndHeight - nCtrlHeight) * 0.5) : 0;

		nXDiff = nNewX - ctrlRect.left;
		nYDiff = nNewY - ctrlRect.top;

		HWND hwnd = ::GetTopWindow(this->GetSafeHwnd());
		while (hwnd)
		{
			UINT nID = ::GetDlgCtrlID(hwnd);
			if (nID == nAnchorID)
				GetDlgItem(nID)->ShowWindow(GetDlgItem(nID)->IsWindowVisible()?SW_HIDE:SW_SHOW);
			else if (nID == IDC_UPLOW_CASE){
				CString sText;
				GetDlgItem(nID)->GetWindowText(sText);
				sText = (sText == "all lowercase") ? "ALL UPPERCASE" : "all lowercase";
				SetDlgItemText(nID, (LPCTSTR)sText);
			}

			CRect rect;
			::GetWindowRect(hwnd, &rect);
			ScreenToClient(&rect);
			::MoveWindow(hwnd, rect.left + nXDiff, rect.top + nYDiff, rect.Width(), rect.Height(), TRUE);
			hwnd = ::GetNextWindow(hwnd, GW_HWNDNEXT);
		}
	}


But i am missing one control.
Can any body help me in solving this.


I got this code from Code Projects only...................
but i am using this code in my own application developed in SDI MFC app

解决方案

Actually I (The person who posted this question) did a silly mistake that I have declared ANCHOR ID by using "#define" key word and I have assigned one value to it. The ID value that I have assigned to ANCHOR ID was matching with one of the CONTROL’s ID present in the dialog so that my code was running and I was missing the control that is having the ID value same as ANCHOR ID.
ANCHOR ID is the one frame that is presented in our dialog that covers our dialog size so that we can make use of that control and calculating the amount of space that we need to move. One advantage of this is overlapping of controls won’t happen.
You can find a beautiful example using ANCHOR ID and enumerating all controls and placing them in center:
Enumerate Controls In a Dialog Box or FormView[^].


这篇关于在对话框中存在控件时,缺少单个(相同)控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆