如何设置窗口的最小的宽度和高度
使窗口边框在调整时有最小的范围 零度C 发表于 2015-3-13 22:21路过路过。。。
明显上面的是有问题的
(修改版)
#include <windows.h>
#include "strsafe.h"
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow)
{
TCHAR szAppName[] = TEXT("Windows程序设计");
WNDCLASSEX wndClass;
MSG msg;
HWND hwnd;
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hIconSm = NULL;
wndClass.hInstance = hInstance;
wndClass.lpszClassName = TEXT("AppTest0");
wndClass.lpszMenuName = NULL;
wndClass.lpfnWndProc = WndProc;
wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_IME;
RegisterClassEx(&wndClass);
hwnd = CreateWindowEx(WS_EX_TOPMOST, TEXT("AppTest0"), szAppName, WS_OVERLAPPED |
WS_CAPTION |
WS_SYSMENU |
WS_MINIMIZEBOX | WS_SIZEBOX
, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
int a = ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static int cxScr = 0, cyScr = 0, cxClient = 0, cyClient = 0, time;
TCHAR Buffer;
PAINTSTRUCT ps;
static int n = 0;
switch (msg)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_SIZING:
wsprintf(Buffer, TEXT("窗口分辨率:%d, %d px"), cxClient, cyClient);
TextOut(GetDC(hwnd), 0, 0, Buffer, lstrlen(Buffer));
if (((LPRECT)lParam)->bottom - ((LPRECT)lParam)->top < 200)
{
if (wParam == WMSZ_BOTTOM)
((LPRECT)lParam)->bottom = ((LPRECT)lParam)->top + 200;
if (wParam == WMSZ_TOP)
((LPRECT)lParam)->top = ((LPRECT)lParam)->bottom - 200;
if (wParam == WMSZ_BOTTOMLEFT)
{
((LPRECT)lParam)->bottom = ((LPRECT)lParam)->top + 200;
if (((LPRECT)lParam)->right - ((LPRECT)lParam)->left < 200)
((LPRECT)lParam)->left = ((LPRECT)lParam)->right - 200;
}
if (wParam == WMSZ_BOTTOMRIGHT)
{
((LPRECT)lParam)->bottom = ((LPRECT)lParam)->top + 200;
if (((LPRECT)lParam)->right - ((LPRECT)lParam)->left < 200)
((LPRECT)lParam)->right = ((LPRECT)lParam)->left + 200;
}
if (wParam == WMSZ_TOPLEFT)
{
((LPRECT)lParam)->top = ((LPRECT)lParam)->bottom - 200;
if (((LPRECT)lParam)->right - ((LPRECT)lParam)->left < 200)
((LPRECT)lParam)->left = ((LPRECT)lParam)->right - 200;
}
if (wParam == WMSZ_TOPRIGHT)
{
((LPRECT)lParam)->top = ((LPRECT)lParam)->bottom - 200;
if (((LPRECT)lParam)->right - ((LPRECT)lParam)->left < 200)
((LPRECT)lParam)->right = ((LPRECT)lParam)->left + 200;
}
}
if (((LPRECT)lParam)->right - ((LPRECT)lParam)->left < 200)
{
if (wParam == WMSZ_RIGHT)
((LPRECT)lParam)->right = ((LPRECT)lParam)->left + 200;
if (wParam == WMSZ_LEFT)
((LPRECT)lParam)->left = ((LPRECT)lParam)->right - 200;
if (wParam == WMSZ_BOTTOMLEFT)
{
((LPRECT)lParam)->left = ((LPRECT)lParam)->right - 200;
if (((LPRECT)lParam)->bottom - ((LPRECT)lParam)->top < 200)
((LPRECT)lParam)->bottom = ((LPRECT)lParam)->top + 200;
}
if (wParam == WMSZ_BOTTOMRIGHT)
{
((LPRECT)lParam)->right = ((LPRECT)lParam)->left + 200;
if (((LPRECT)lParam)->bottom - ((LPRECT)lParam)->top < 200)
((LPRECT)lParam)->bottom = ((LPRECT)lParam)->top + 200;
}
if (wParam == WMSZ_TOPLEFT)
{
((LPRECT)lParam)->left = ((LPRECT)lParam)->right - 200;
if (((LPRECT)lParam)->bottom - ((LPRECT)lParam)->top < 200)
((LPRECT)lParam)->top = ((LPRECT)lParam)->bottom - 200;
}
if (wParam == WMSZ_TOPRIGHT)
{
((LPRECT)lParam)->right = ((LPRECT)lParam)->left + 200;
if (((LPRECT)lParam)->bottom - ((LPRECT)lParam)->top < 200)
((LPRECT)lParam)->top = ((LPRECT)lParam)->bottom - 200;
}
}
break;
case WM_PAINT:
n++;
wsprintf(Buffer, TEXT("重绘的次数:%d "), n);
TextOut(GetDC(hwnd), 0, 15, Buffer, lstrlen(Buffer));
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
} 楼主我可以做,等我一下我暂时在上课,晚上给你做。 要在一个函数里面申请空间的方法就要是传递双重指针,不然申请的空间会丢失。要不然就用return。
如果想完全搞懂就看看汇编代码。 #include <windows.h>
#include "strsafe.h"
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow)
{
TCHAR szAppName[] = TEXT("Windows程序设计");
WNDCLASSEX wndClass;
MSG msg;
HWND hwnd;
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hIconSm = NULL;
wndClass.hInstance = hInstance;
wndClass.lpszClassName = TEXT("AppTest0");
wndClass.lpszMenuName = NULL;
wndClass.lpfnWndProc = WndProc;
wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_IME;
RegisterClassEx(&wndClass);
hwnd = CreateWindowEx(WS_EX_TOPMOST, TEXT("AppTest0"), szAppName, WS_OVERLAPPED |
WS_CAPTION |
WS_SYSMENU |
WS_MINIMIZEBOX|WS_SIZEBOX
, 0, 0, 400, 600,
NULL, NULL, hInstance, NULL);
int a = ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static int cxScr = 0, cyScr = 0, cxClient = 0, cyClient = 0,time;
TCHAR Buffer;
PAINTSTRUCT ps;
switch (msg)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_CREATE:
cxScr = GetSystemMetrics(SM_CXSCREEN);
cyScr = GetSystemMetrics(SM_CYSCREEN);
break;
case WM_SIZING:
wsprintf(Buffer, TEXT("%d, %d"), wParam, lParam);
TextOut(GetDC(hwnd), 0, 0, Buffer, wcslen(Buffer));
if (((LPRECT)lParam)->bottom - ((LPRECT)lParam)->top < 200)
{
if (wParam == WMSZ_BOTTOM || wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_BOTTOMRIGHT)
((LPRECT)lParam)->bottom = ((LPRECT)lParam)->top + 200;
else
((LPRECT)lParam)->top = ((LPRECT)lParam)->bottom - 200;
}
if (((LPRECT)lParam)->right - ((LPRECT)lParam)->left < 200)
{
if (wParam == WMSZ_TOP ||wParam == WMSZ_TOPRIGHT)
((LPRECT)lParam)->right = ((LPRECT)lParam)->left + 200;
else
((LPRECT)lParam)->left = ((LPRECT)lParam)->right - 200;
}
break;
case WM_PAINT:
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
测试可用 {:9_240:}路过路过。。。 {:7_146:} 本帖最后由 hustjinghu 于 2016-11-12 00:18 编辑
使用WM_GETMINMAXINFO消息,该消息lParam 中MINMAXINFO 结构体可以设置窗口的最大最小尺寸、默认位置等信息。
The WM_GETMINMAXINFO message is sent to a window when the size or position of the window is about to change. An application can use this message to override the window's default maximized size and position, or its default minimum or maximum tracking size.
A window receives this message through its WindowProc function.
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_GETMINMAXINFO
WPARAM wParam, // not used
LPARAM lParam // window information (LPMINMAXINFO)
);
Parameters
wParam
This parameter is not used.
lParam
Pointer to a MINMAXINFO structure that contains the default maximized position and dimensions, and the default minimum and maximum tracking sizes. An application can override the defaults by setting the members of this structure.
页:
[1]