我写了一个小程序,创建一个与编辑框关联的UpDown控件。
我用一个资源脚本进行了测试,问题是一样的:向上箭头减少数字,向下箭头增加数字。我不知道我的错误在哪里。
我用Visual Studio 2022在Windows 11机器上编写了这个程序。
下面是我的代码:
#include <Windows.h>
#include <CommCtrl.h>
#include "resource.h"
#pragma comment(lib, "comctl32.lib")
static HWND et1, ud1;
static HINSTANCE hinstancia;
LRESULT OnDestroy(HWND hwnd, WPARAM wp, LPARAM lp)
{
PostQuitMessage(0);
return 0;
}
/////////////////////////////////////////////////////////////////////
LRESULT OnCreate(HWND hwnd, WPARAM wp, LPARAM lp)
{
et1 = CreateWindowEx(0, L"edit", L"", WS_CHILD | WS_VISIBLE, 10, 10, 40, 16, hwnd, NULL, hinstancia, NULL);
if (!et1)
{
MessageBox(NULL, L"I was unable to create the input control", L"Error", MB_OK);
exit(0);
}
ud1 = CreateWindowEx(0, L"MSCTLS_UPDOWN32", L"", WS_CHILD | WS_VISIBLE | UDS_ALIGNRIGHT | UDS_ARROWKEYS | UDS_AUTOBUDDY | UDS_SETBUDDYINT, 50, 10, 12, 16, hwnd, NULL, hinstancia, NULL);
if (!ud1)
{
MessageBox(NULL, L"I was unable to create the up down control", L"Error", MB_OK);
exit(0);
}
return 0;
}
/////////////////////////////////////////////////////////////////////
LRESULT CALLBACK wndproc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg)
{
case WM_DESTROY: return OnDestroy(hwnd, wp, lp);
case WM_CREATE: return OnCreate(hwnd, wp, lp);
default: return DefWindowProc(hwnd, msg, wp, lp);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
INITCOMMONCONTROLSEX ice;
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
hinstancia = hInstance;
ice.dwSize = sizeof(INITCOMMONCONTROLSEX);
ice.dwICC = ICC_UPDOWN_CLASS;
InitCommonControlsEx(&ice);
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hInstance = hInstance;
wc.lpszClassName = L"Test1";
wc.hbrBackground = (HBRUSH) GetStockObject(GRAY_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.lpfnWndProc = wndproc;
wc.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, L"Window class was not registered", L"Error", MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_APPWINDOW, L"Test1", L"Sample", WS_CAPTION | WS_SYSMENU | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
MessageBox(NULL, L"Window was not createed", L"Error", MB_OK);
return 0;
}
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
有人能告诉我我做错了什么吗?
我希望用向上箭头递增数字,用向下箭头递减数字。
1条答案
按热度按时间2ledvvac1#
我同意@雷米勒博的观点,
根据文档:步骤7:创建上下控件
若要使控件在用户单击控件的向上箭头时向上计数,必须设置up-down控件的范围和方向。为此,您可以向控件发送一个包含上限和下限值的
UDM_SETRANGE
消息。代码如下:
您可以参考示例:上下控制示例代码
您还可以根据需要设置Up-Down控件的值范围: