MFC全屏 控件随之全屏

一只正在啃的蜗牛 2018-09-27 05:15:15
#define NUM_OF_LINK_CTRL 13 void CSerlink::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if(!m_bIsInit)return;
CRect recNow;
GetWindowRect(&recNow);
RectToRect(mDlgRect,recNow,mRectArray[0],false);
RectToRect(mDlgRect,recNow,mRectArray[1],false);
RectToRect(mDlgRect,recNow,mRectArray[2],false);
RectToRect(mDlgRect,recNow,mRectArray[3],false);
RectToRect(mDlgRect,recNow,mRectArray[4],false);
RectToRect(mDlgRect,recNow,mRectArray[5],false);
RectToRect(mDlgRect,recNow,mRectArray[6],false);
RectToRect(mDlgRect,recNow,mRectArray[7],false);
RectToRect(mDlgRect,recNow,mRectArray[8],false);
RectToRect(mDlgRect,recNow,mRectArray[9],false);
RectToRect(mDlgRect,recNow,mRectArray[10],false);
RectToRect(mDlgRect,recNow,mRectArray[11]);
RectToRect(mDlgRect,recNow,mRectArray[12]);
SetnIDRect(IDC_STATIC_COMM_PORT,mRectArray[0]);
SetnIDRect(IDC_EDIT_COMM_PORT,mRectArray[1]);
SetnIDRect(IDC_STATIC_SLAVE_ADDRESS,mRectArray[2]);
SetnIDRect(IDC_EDIT_SLAVE_ADDR,mRectArray[3]);
SetnIDRect(IDC_STATIC_SLAVE_ADDRESS2,mRectArray[4]);

SetnIDRect(IDC_EDIT_SLAVE_ADDR2,mRectArray[5]);
SetnIDRect(IDC_BUTTON_FIND_FIRST,mRectArray[6]);
SetnIDRect(IDC_BUTTON_FIND_NEXT,mRectArray[7]);
SetnIDRect(IDC_BUTTON_STOP,mRectArray[8]);
SetnIDRect(IDC_BUTTON_APPLY,mRectArray[9]);
SetnIDRect(IDC_BUTTON_OK_SER,mRectArray[10]);
SetnIDRect(IDC_STATIC,mRectArray[11]);
SetnIDRect(IDC_STATIC_GR_BX,mRectArray[12]);
没有全屏之前已经有上面代码固定了,请问怎么编辑让控件全屏时移到相应位置?
...全文
376 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lx624909677 2018-10-15
  • 打赏
  • 举报
回复
控件的位置设置成绝对的,然后在OnSize消息处理函数中,去根据当前窗口的大小来改变控件的大小和位置
workaways 2018-10-13
  • 打赏
  • 举报
回复
要在消息函数On_size里添加相应的代码才会在尺寸改变时,随之变化
  • 打赏
  • 举报
回复
好的,晚点我试一下
zgl7903 2018-10-12
  • 打赏
  • 举报
回复
子窗口一样可以使用, WM_SIZE 中移动子窗口, 将会触发子窗口的 WM_SIZE消息

class CDlg6Dlg : public CDialog
{
public:
CWindowSizeMange m_WinsizeManage;

CSubPagDlg m_Page;
};

class CSubPagDlg : public CDialog
{
public:
CWindowSizeMange m_WinsizeManage;
}
  • 打赏
  • 举报
回复
子窗口如何操作
zgl7903 2018-10-11
  • 打赏
  • 举报
回复

因为记录了子窗口原始相位位置,因此不论缩放多少次,相对位置都不会发生变化

#include <Afxtempl.h>

class CWindowSizeMange
{
protected:
typedef struct _tagSizeData_t
{
HWND hWndCtrl; //窗口句柄
double fLeft, fTop, fRight, fBottom; //位置相对比例

_tagSizeData_t()
{
memset(this, 0, sizeof(*this));
}

//初始化 (记录窗口原始坐标位置)
void Init(HWND hParent, RECT rcParent, HWND hCtrl)
{
ASSERT(hParent && hCtrl);

hWndCtrl = hCtrl;

//获取控件的坐标
RECT rcCtrl;
GetWindowRect(hCtrl, &rcCtrl);
POINT ptLT = { rcCtrl.left, rcCtrl.top };
POINT ptRB = { rcCtrl.right, rcCtrl.bottom };
ScreenToClient(hParent, &ptLT);
ScreenToClient(hParent, &ptRB);

//计算相对位置
double cx = rcParent.right - rcParent.top;
double cy = rcParent.bottom - rcParent.top;
fLeft = ptLT.x / cx;
fTop = ptLT.y / cy;
fRight = ptRB.x / cx;
fBottom = ptRB.y / cy;
}

//获取窗口坐标
RECT GetRect(RECT rcParent)
{
RECT rcCtrl = {0,0,0,0};
if(hWndCtrl)
{
int cx = rcParent.right - rcParent.left;
int cy = rcParent.bottom - rcParent.top;
rcCtrl.left = (LONG)(cx * fLeft);
rcCtrl.top = (LONG)(cy * fTop);
rcCtrl.right = (LONG)(cx * fRight);
rcCtrl.bottom = (LONG)(cy * fBottom);
}

return rcCtrl;
}

}SIZEDATA, *LPSIZEDATA;

protected:
HWND m_hParentWnd;
CArray <SIZEDATA, const SIZEDATA &>m_SizeArray;
CRect m_rcOrgRect;

public:
//初始化
INT_PTR Init(HWND hParent)
{
ASSERT(hParent && IsWindow(hParent));

m_hParentWnd = hParent;
GetClientRect(hParent, &m_rcOrgRect);

m_SizeArray.RemoveAll();
HWND hWndT = NULL;
while(1)
{
hWndT = FindWindowEx(hParent, hWndT, NULL, NULL);
if(hWndT)
{
SIZEDATA m;
m.Init(hParent, m_rcOrgRect, hWndT);
m_SizeArray.Add(m);
}
else
{
break;
}
}

return m_SizeArray.GetSize();
}

//缩放窗口控件
void ResizeWindow()
{
CRect rcClient;
GetClientRect(m_hParentWnd, &rcClient);
if(m_hParentWnd && m_SizeArray.GetSize() > 0
&& rcClient != m_rcOrgRect)
{
for(INT_PTR i=0; i<m_SizeArray.GetSize(); i++)
{
SIZEDATA *pm = &m_SizeArray[i];

CRect rcCtrl = pm->GetRect(rcClient);
MoveWindow(pm->hWndCtrl,
rcCtrl.left, rcCtrl.top, rcCtrl.Width(), rcCtrl.Height(), TRUE);
}

m_rcOrgRect = rcClient;
}
}
};
//应用实例

//Dlg6Dlg.h

class CDlg6Dlg : public CDialog
{
public:
CWindowSizeMange m_WinsizeManage;
protected:
virtual BOOL OnInitDialog();
afx_msg void OnSize(UINT nType, int cx, int cy);

//Dlg6Dlg.cpp

BEGIN_MESSAGE_MAP(CDlg6Dlg, CDialog)
//{{AFX_MSG_MAP(CDlg6Dlg)
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CDlg6Dlg::OnInitDialog()
{
CDialog::OnInitDialog();

//记录控件位置
m_WinsizeManage.Init(m_hWnd);

return TRUE;
}

void CDlg6Dlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
m_WinsizeManage.ResizeWindow();
}
---------------------
作者:zgl7903
来源:CSDN
原文:https://blog.csdn.net/zgl7903/article/details/79316100?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!
haha0909 2018-10-11
  • 打赏
  • 举报
回复
在OnSize里调用ReSize(),并定义 POINT m_old;
haha0909 2018-10-11
  • 打赏
  • 举报
回复
在OnSize里调用,POINT m_old;

void CFMDTouchWorkshopView::ReSize()
{

float fsp[2];
POINT Newp; //获取现在对话框的大小
CRect recta;
GetClientRect(&recta); //取客户区大小
Newp.x=recta.right-recta.left;
Newp.y=recta.bottom-recta.top;
if((Newp.x == 0)&&(Newp.y == 0))
return;
fsp[0]=(float)Newp.x/m_old.x;
fsp[1]=(float)Newp.y/m_old.y;
CRect Rect;
int woc;
CPoint OldTLPoint,TLPoint; //左上角
CPoint OldBRPoint,BRPoint; //右下角
HWND hwndChild=::GetWindow(m_hWnd,GW_CHILD); //列出所有控件
while(hwndChild)
{
woc=::GetDlgCtrlID(hwndChild);//取得ID
GetDlgItem(woc)->GetWindowRect(Rect);
ScreenToClient(Rect);
OldTLPoint = Rect.TopLeft();
TLPoint.x = long(OldTLPoint.x*fsp[0]);
TLPoint.y = long(OldTLPoint.y*fsp[1]);
OldBRPoint = Rect.BottomRight();
BRPoint.x = long(OldBRPoint.x *fsp[0]);
BRPoint.y = long(OldBRPoint.y *fsp[1]);
Rect.SetRect(TLPoint,BRPoint);
GetDlgItem(woc)->MoveWindow(Rect,TRUE);
hwndChild=::GetWindow(hwndChild, GW_HWNDNEXT);
}
m_old=Newp;
}
  • 打赏
  • 举报
回复
子对话框如何全屏?
sevancheng 2018-10-10
  • 打赏
  • 举报
回复
WM_SIZE 消息函数里面去MoveWindow
  • 打赏
  • 举报
回复
CWnd *pWnd;
pWnd = GetDlgItem(IDC_LIST); //获取控件句柄
if(pWnd)//判断是否为空,因为对话框创建时会调用此函数,而当时控件还未创建
{
CRect rect; //获取控件变化前大小
pWnd->GetWindowRect(&rect);
ScreenToClient(&rect);//将控件大小转换为在对话框中的区域坐标
rect.left=rect.left*cx/m_rect.Width();/////调整控件大小
rect.right=rect.right*cx/m_rect.Width();
rect.top=rect.top*cy/m_rect.Height();
rect.bottom=rect.bottom*cy/m_rect.Height();
pWnd->MoveWindow(rect);//设置控件大小
}
// ....其他控件依次操作
GetClientRect(&m_rect);//将变化后的对话框大小保存
后面我加了类似这种,但不行,运行时还没全屏就出现了移动,

15,978

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧