鱼C论坛

 找回密码
 立即注册
查看: 6490|回复: 7

如何设置窗口的最小的宽度和高度

[复制链接]
发表于 2015-3-11 08:50:10 From FishC Mobile | 显示全部楼层 |阅读模式
5鱼币
使窗口边框在调整时有最小的范围

最佳答案

查看完整内容

明显上面的是有问题的 (修改版) #include #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 ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-3-11 08:50:11 | 显示全部楼层
零度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[1024];
        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);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-3-11 15:34:15 From FishC Mobile | 显示全部楼层
楼主我可以做,等我一下我暂时在上课,晚上给你做。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-3-11 17:30:57 | 显示全部楼层
要在一个函数里面申请空间的方法就要是传递双重指针,不然申请的空间会丢失。要不然就用return。
如果想完全搞懂就看看汇编代码。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-3-13 22:17:51 | 显示全部楼层
  1. #include <windows.h>
  2. #include "strsafe.h"

  3. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  4. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow)
  5. {
  6.         TCHAR                szAppName[] = TEXT("Windows程序设计");
  7.         WNDCLASSEX        wndClass;
  8.         MSG                        msg;
  9.         HWND                hwnd;
  10.         wndClass.cbSize = sizeof(WNDCLASSEX);
  11.         wndClass.cbClsExtra = 0;
  12.         wndClass.cbWndExtra = 0;
  13.         wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  14.         wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  15.         wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  16.         wndClass.hIconSm = NULL;
  17.         wndClass.hInstance = hInstance;
  18.         wndClass.lpszClassName = TEXT("AppTest0");
  19.         wndClass.lpszMenuName = NULL;
  20.         wndClass.lpfnWndProc = WndProc;
  21.         wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_IME;

  22.         RegisterClassEx(&wndClass);
  23.         hwnd = CreateWindowEx(WS_EX_TOPMOST, TEXT("AppTest0"), szAppName, WS_OVERLAPPED |
  24.                                                         WS_CAPTION |
  25.                                                         WS_SYSMENU |
  26.                                                         WS_MINIMIZEBOX|WS_SIZEBOX  
  27.                                                         , 0, 0, 400, 600,
  28.                                                         NULL, NULL, hInstance, NULL);
  29.         int a = ShowWindow(hwnd, SW_SHOW);
  30.         UpdateWindow(hwnd);

  31.         while (GetMessage(&msg, NULL, 0, 0))
  32.         {
  33.                 TranslateMessage(&msg);
  34.                 DispatchMessage(&msg);
  35.         }

  36.         return 0;
  37. }
  38. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  39. {
  40.         static int        cxScr = 0, cyScr = 0, cxClient = 0, cyClient = 0,time;
  41.         TCHAR                Buffer[1024];
  42.         PAINTSTRUCT        ps;

  43.         switch (msg)
  44.         {
  45.         case WM_SIZE:
  46.                 cxClient = LOWORD(lParam);
  47.                 cyClient = HIWORD(lParam);
  48.                 break;
  49.         case WM_CREATE:
  50.                 cxScr = GetSystemMetrics(SM_CXSCREEN);
  51.                 cyScr = GetSystemMetrics(SM_CYSCREEN);
  52.                 break;
  53.         case WM_SIZING:
  54.                 wsprintf(Buffer, TEXT("%d, %d"), wParam, lParam);
  55.                 TextOut(GetDC(hwnd), 0, 0, Buffer, wcslen(Buffer));
  56.                 if (((LPRECT)lParam)->bottom - ((LPRECT)lParam)->top < 200)
  57.                 {
  58.                         if (wParam == WMSZ_BOTTOM || wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_BOTTOMRIGHT)
  59.                                 ((LPRECT)lParam)->bottom = ((LPRECT)lParam)->top + 200;
  60.                         else
  61.                                 ((LPRECT)lParam)->top = ((LPRECT)lParam)->bottom - 200;
  62.                 }
  63.                 if (((LPRECT)lParam)->right - ((LPRECT)lParam)->left < 200)
  64.                 {
  65.                         if (wParam == WMSZ_TOP ||  wParam == WMSZ_TOPRIGHT)
  66.                                 ((LPRECT)lParam)->right = ((LPRECT)lParam)->left + 200;
  67.                         else
  68.                                 ((LPRECT)lParam)->left = ((LPRECT)lParam)->right - 200;
  69.                 }
  70.                
  71.                 break;
  72.         case WM_PAINT:
  73.                 BeginPaint(hwnd, &ps);

  74.                 EndPaint(hwnd, &ps);
  75.                 break;
  76.         case WM_DESTROY:
  77.                 PostQuitMessage(0);
  78.                 break;
  79.         }
  80.         return DefWindowProc(hwnd, msg, wParam, lParam);
  81. }
复制代码

测试可用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-3-13 22:21:45 | 显示全部楼层
路过路过。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-9-16 15:16:18 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-11-12 00:17:38 | 显示全部楼层
本帖最后由 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.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 13:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表