MFC如何将窗口中所有控件大小随父窗口大小改变

MFC如何将窗口中所有控件大小随父窗口大小改变,第1张

对话框属性->样式,勾上最大化框。

再在类向导里,选择WM_SIZE,然后Edit Code

VC++全屏时,根据对话框大小调整控件大小

1、在对话框类中加入成员变量CRect m_rect;用于保存对话框大小变化前的大小;

2、在对话框的OnInitDialog()函数中获取对话框创建时的大小:GetClientRect(&m_rect);

3、在WM_SIZE的响应函数OnSize()中加入以下代码:

C/C++ code

CWnd pWnd;

pWnd = GetDlgItem(IDC_LIST); //获取控件句柄

if(pWnd && nType!=1)//判断是否为空,因为对话框创建时会调用此函数,而当时控件还未创建

{

CRect rect; //获取控件变化前大小

pWnd->GetWindowRect(&rect);

ScreenToClient(&rect);//将控件大小转换为在对话框中的区域坐标

// cx/m_rectWidth()为对话框在横向的变化比例

rectleft=rectleftcx/m_rectWidth();/////调整控件大小

rectright=rectrightcx/m_rectWidth();

recttop=recttopcy/m_rectHeight();

rectbottom=rectbottomcy/m_rectHeight();

pWnd->MoveWindow(rect);//设置控件大小

}

GetClientRect(&m_rect);//将变化后的对话框大小设为旧大小

楼上的都在扯淡,LPRECT是指向RECT结构体的指针,RECT结构体的定义如下:

typedef struct tagRECT {

LONG left;

LONG top;

LONG right;

LONG bottom;

} RECT;

其与MFC中的类CRect完全不是一码事。所以按道理,lpRect的数据类型是RECT。之所以你可以用CRect的对象或者其指针来作为上面函数的参数,是因为VC的编译器自动为你作了类型转换。CRect有一个 *** 作符为LPRECT,所以正规的写法应该是

CRect rect;

GetWindowRect( LPRECT(rect) );

ScreenToClient( LPRECT(rect) );

虽然你在两个函数中都可以直接用rect或者&rect来作为参数传进去,但是这样是不安全的(大多数情况这种写法完全没有问题),但我建议你用正规的写法。

GDI是Graphics Device Interface的缩写,含义是图形设备接口,它的主要任务是负责系统与绘图程序之间的信息交换,处理所有Windows程序的图形输出。

GDI具有如下特点:

GDI函数大致可分类为:

设备上下文函数(如GetDC、CreateDC、DeleteDC)、 画线函数(如LineTo、Polyline、Arc)、填充画图函数(如Ellipse、FillRect、Pie)、画图属性函数(如SetBkColor、SetBkMode、SetTextColor)、文本、字体函数(如TextOut、GetFontData)、位图函数(如SetPixel、BitBlt、StretchBlt)、坐标函数(如DPtoLP、LPtoDP、ScreenToClient、ClientToScreen)、映射函数(如SetMapMode、SetWindowExtEx、SetViewportExtEx)、元文件函数(如PlayMetaFile、SetWinMetaFileBits)、区域函数(如FillRgn、FrameRgn、InvertRgn)、路径函数(如BeginPath、EndPath、StrokeAndFillPath)、裁剪函数(如SelectClipRgn、SelectClipPath)等

设备描述表(Device Context, 缩写为DC)也称设备上下文或设备环境。简单来说,就是一种包含各种绘图属性(字体,颜色)和方法(绘图函数)的数据结构。设备描述表定义了设备,绘图工具和画图信息,其不仅可以绘制各种图形,还可以确定在应用窗口中绘制图形的方式和图形的样式。

Windows所有的绘制 *** 作及图形输出都必须通过设备描述表这个虚拟用户工作区来进行。用户在绘制之前,必须获取绘制窗口区域的一个设备环境DC,接着才能进行GDI函数的调用,执行适合与设备环境的命令。

该函数用于获取窗口工作区的显示器设备描述表,其不带任何参数。如果函数条用成功,则返回标识CWnd客户区的设备环境,否则返回NULL。

说明:在完成绘图之后,用GetDC函数获取的设备描述表必须通过ReleaseDC函数来释放。

Windows应用程序响应WM_PAINT消息进行图形刷新时,会通过调用BeginPaint()函数来获取DC。

参考: 设备描述表概念及使用

检索类名称和窗口名称与指定字符串匹配的窗口的句柄。该函数搜索子窗口,从指定子窗口后面的那个开始。此函数不执行区分大小写的搜索。

返回与指定窗口有特定关系(如Z序或所有者)的窗口句柄。

参数:

hWnd:窗口句柄。要获得的窗口句柄是依据nCmd参数值相对于这个窗口的句柄。

nCmd:说明指定窗口与要获得句柄的窗口之间的关系。

打开一个已存在的进程对象,并返回进程的句柄。

一般应用程序当中的鼠标事件只能争对应用程序窗口内部有效,如果点击应用程序以外的窗口,例如点击其它应用程序窗口等,这个时候,鼠标消息是不会发送给我们的应用程序窗口,更不会激发事件。我们可以利用mfc窗口中的windows消息处理函数,来处理我们的鼠标或键盘事件 *** 作如下:

首先在我们需要开启捕获的事件中加入鼠标捕获

SetCapture();

然后在WindowProc中加入如下代码:

LRESULT CMyxgsdlgmainbg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)

{

// TODO: 在此添加专用代码和/或调用基类

POINT point;

CRect rect;

switch (message)

{

case WM_LBUTTONDOWN:

;

case WM_LBUTTONDBLCLK:

;

case WM_RBUTTONDOWN:

;

case WM_RBUTTONDBLCLK:

;

case WM_MBUTTONDOWN:

;

case WM_MBUTTONDBLCLK:

GetCursorPos(&point);

ScreenToClient(&point);

CRect rect;

AfxGetMainWnd()-> GetWindowRect(&rect);

ScreenToClient(&rect);

if(!rectPtInRect(point)) //判断是否在本应用程序窗口内

{

isshowmain=false;

}

KillTimer(8);

issettimer8=false;

ReleaseCapture();

break;

}

return CDialog::WindowProc(message, wParam, lParam);

}

ReleaseCapture();//该函数从当前线程中的窗口释放鼠标捕获,并恢复通常的鼠标输入处理。捕获鼠标的窗口接收所有的鼠标输入(无论光标的位置在哪里),除非点击鼠标键时,光标热点在另一个线程的窗口中

我写过一个VB写的截图软件,自定义矩形截取特定区域,需要的话把你的邮箱告诉我,我发源码给你

主要的API:

Public Const PM_REMOVE = &H1

' 鼠标相关消息

Public Const WM_MOUSEFIRST = &H200

Public Const WM_MOUSELAST = &H209

Public Const WM_RBUTTONDOWN = &H204

Public Const WM_LBUTTONDOWN = &H201

Public Const WM_RBUTTONUP = &H205

Public Const WM_LBUTTONUP = &H202

Public Const WM_MOUSEMOVE = &H200

' BitBlt 函数常量

Public Const SRCCOPY = &HCC0020

Public Const SRCINVERT = &H660046

' PatBlt 函数常量

Public Const DINV = 3

Public Const DSTINVERT = &H550009

Type RECT

Left As Long

Top As Long

Right As Long

Bottom As Long

End Type

Type POINTAPI

x As Long

y As Long

End Type

Type Size

cx As Long

cy As Long

End Type

Type msg

hwnd As Long

message As Long

wParam As Long

lParam As Long

time As Long

pt As POINTAPI

End Type

'Windows 关于消息的函数

Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" _

(lpMsg As msg, ByVal hwnd As Long, _

ByVal wMsgFilterMin As Long, _

ByVal wMsgFilterMax As Long, _

ByVal wRemoveMsg As Long _

) As Long

Declare Function WaitMessage Lib "user32" () As Long

Declare Function TranslateMessage Lib "user32" (lpMsg As msg) As Long

Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" _

(lpMsg As msg) As Long

Declare Function PostMessage Lib "user32" Alias "PostMessageA" _

(ByVal hwnd As Long, _

ByVal wMsg As Long, _

ByVal wParam As Long, _

ByVal lParam As Long _

) As Long

'鼠标捕捉函数

Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long

Declare Function ReleaseCapture Lib "user32" () As Long

Declare Function GetCapture Lib "user32" () As Long

Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long

Declare Function DeleteObject Lib "gdi32" _

(ByVal hObject As Long) As Long

Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" _

(ByVal hdc As Long, _

ByVal lpszString As String, _

ByVal cbString As Long, _

lpSize As Size _

) As Long

Declare Function IsRectEmpty Lib "user32" (lpRect As RECT) As Long

Declare Function PatBlt Lib "gdi32" _

(ByVal hdc As Long, _

ByVal x As Long, ByVal y As Long, _

ByVal nWidth As Long, _

ByVal nHeight As Long, _

ByVal dwRop As Long _

) As Long

Declare Function Rectangle Lib "gdi32" _

(ByVal hdc As Long, _

ByVal X1 As Long, ByVal Y1 As Long, _

ByVal X2 As Long, ByVal Y2 As Long _

) As Long

Declare Function ScreenToClient Lib "user32" _

(ByVal hwnd As Long, _

lpPoint As POINTAPI _

) As Long

Declare Function SelectObject Lib "gdi32" _

(ByVal hdc As Long, _

ByVal hObject As Long _

) As Long

Declare Function TextOut Lib "gdi32" Alias "TextOutA" _

(ByVal hdc As Long, _

ByVal x As Long, ByVal y As Long, _

ByVal lpString As String, _

ByVal nCount As Long _

) As Long

Declare Function SetTextColor Lib "gdi32" _

(ByVal hdc As Long, _

ByVal crColor As Long _

) As Long

Declare Function SetBkColor Lib "gdi32" _

(ByVal hdc As Long, _

ByVal crColor As Long _

) As Long

Declare Function SetForegroundWindow Lib "user32" _

(ByVal hwnd As Long) As Long

Declare Function UpdateWindow Lib "user32" (ByVal hwnd As Long) As Long

Declare Function WindowFromPoint Lib "user32" _

(ByVal xPoint As Long, _

ByVal yPoint As Long _

) As Long

Declare Function BitBlt Lib "gdi32" _

(ByVal hDestDC As Long, _

ByVal x As Long, _

ByVal y As Long, _

ByVal nWidth As Long, _

ByVal nHeight As Long, _

ByVal hSrcDC As Long, _

ByVal xSrc As Long, _

ByVal ySrc As Long, _

ByVal dwRop As Long _

) As Long

Declare Function CreateDCNull Lib "gdi32" Alias "CreateDCA" _

(ByVal lpDriverName As String, _

ByVal lpDeviceName As String, _

ByVal lpOutput As String, _

ByVal lpInitData As Long _

) As Long

Declare Function ReleaseDC Lib "user32" _

(ByVal hwnd As Long, _

ByVal hdc As Long _

) As Long

Declare Function GetDeviceCaps Lib "gdi32" _

(ByVal hdc As Long, _

ByVal nIndex As Long _

) As Long

Declare Function CreateBitmap Lib "gdi32" _

(ByVal nWidth As Long, _

ByVal nHeight As Long, _

ByVal nPlanes As Long, _

ByVal nBitCount As Long, _

lpBits As Any _

) As Long

Declare Function ClientToScreen Lib "user32" _

(ByVal hwnd As Long, _

lpPoint As POINTAPI _

) As Long

Declare Function CreateCompatibleDC Lib "gdi32" _

(ByVal hdc As Long) As Long

Declare Function OpenClipboard Lib "user32" _

(ByVal hwnd As Long) As Long

Declare Function EmptyClipboard Lib "user32" () As Long

Declare Function SetClipboardData Lib "user32" _

(ByVal wFormat As Long, _

ByVal hMem As Long _

) As Long

Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" _

(ByVal lpDriverName As String, _

ByVal lpDeviceName As String, _

ByVal lpOutput As String, _

lpInitData As Long _

) As Long

Declare Function CreateCompatibleBitmap Lib "gdi32" _

(ByVal hdc As Long, _

ByVal nWidth As Long, _

ByVal nHeight As Long _

) As Long

Declare Function CloseClipboard Lib "user32" () As Long

为什么在OnCtlColor里面调用InvalidateRect,InvalidateRect会使得OnCtlColor重复被调用,就是说只要调用一个对话框类对象的InvalidateRect函数,则对话框的OnCtlColor函数被调用,就比如在OnPaint函数里面调用InvalidateRect函数一样,建议不要这么做,非要这么做一定要注意不要引起无限循环调用。文本框多次输入的时候怎么会造成文字重叠呢,比较少见哈,如果内要强迫文本框刷新,可以在输入字符消息函数里面调用InvalidateRect函数。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/11672029.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存