win32 滚动条在 C/C++ 程序中不起作用
win32 scrollbar in not working in C/C++ program
win32 滚动条在不起作用的屏幕截图
中Win32 滚动条不起作用。这是我在win32 C/C++中的代码
#include <windows.h>
#include <stdio.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND text, text2, button, mm;
HMENU hmenu;
void AddMenus(HWND);
char textsave[20];
/* Make the class name into a global variable */
char szClassName[ ] ="CodeBlocksWindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"", /* Title Text */
WS_OVERLAPPEDWINDOW | WS_VSCROLL, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
CreateWindow("EDIT", "jdslfkjssfrnrnrnflkjsdfrnflskdjfkl",
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL,
20, 70, 490, 130, hwnd, NULL, NULL, NULL);
CreateWindow("EDIT", "jdslfkjssfrnrnrnflkjsdfrnflskdjfkl",
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL,
20, 270, 490, 230, hwnd, NULL, NULL, NULL);
break;
case WM_COMMAND:
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
滚动条在此处不起作用。我希望向下拉滚动条,以便可以看到底部的其余部分。这段代码怎么写,滚动条能行吗?
如何垂直滚动
1.获取滚动单元。
滚动单位通常在处理WM_CREATE
消息时设置。例如,在您的情况下,您在编辑控件中显示文本,因此在这里我们可以将字符单元格的高度加上外部行距作为一个垂直滚动单元。若要检索特定 DC 的字体尺寸,请使用GetTextMetrics
函数。
2. 处理WM_SIZE
消息
在处理WM_SIZE
消息时,可以方便地调整滚动范围和滚动位置以反映工作区的尺寸。
SetScrollInfo
函数设置滚动条的最小和最大位置值、页面大小和滚动位置。
3. 处理WM_VSCROLL
消息。
当用户单击顶部箭头、底部箭头、滚动框上方的滚动条轴、滚动框下方的滚动条轴并拖动滚动框时,滚动条会将WM_VSCROLL
消息发送到窗口过程。
处理WM_VSCROLL
消息时,将检查滚动条请求代码并计算滚动增量。将增量应用于当前滚动位置后,使用ScrollWindowEx
函数将窗口滚动到新位置,并使用SetScrollInfo
函数调整滚动框的位置。
滚动窗口后,其部分工作区将变为无效。为了确保更新无效区域,使用UpdateWindow
函数生成WM_PAINT
消息。
以下是您可以参考的主窗口的整个工作区滚动的示例。
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
TEXTMETRIC tm;
SCROLLINFO si;
// These variables are required to display text.
static int xClient; // width of client area
static int yClient; // height of client area
static int yChar; // vertical scrolling unit
static int yPos; // current vertical scrolling position
switch (message) /* handle the messages */
{
case WM_CREATE:
{
editCtl1 = CreateWindow("EDIT", "jdslfkjssfrnrnrnflkjsdfrnflskdjfkl",
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL,
20, 70, 490, 130, hwnd, NULL, NULL, NULL);
editCtl2 = CreateWindow("EDIT", "jdslfkjssfrnrnrnflkjsdfrnflskdjfkl",
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL,
20, 270, 490, 230, hwnd, NULL, NULL, NULL);
// Get the handle to the client area's device context.
hdc = GetDC(hwnd);
// Extract font dimensions from the text metrics.
GetTextMetrics(hdc, &tm);
yChar = tm.tmHeight + tm.tmExternalLeading;
// Free the device context.
ReleaseDC(hwnd, hdc);
break;
}
case WM_SIZE:
{
// Retrieve the dimensions of the client area.
yClient = HIWORD(lParam);
xClient = LOWORD(lParam);
// Get y-coordinate (bottom) of the second edit control
RECT editCtl2Rect = { 0 };
GetWindowRect(editCtl2, &editCtl2Rect);
POINT point = { 0 };
point.x = editCtl2Rect.right;
point.y = editCtl2Rect.bottom;
// Convert screen coordinate to parent client-area coordinates
ScreenToClient(hwnd, &point);
// Set the vertical scrolling range and page size
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = point.y / yChar;
si.nPage = yClient / yChar;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
break;
}
case WM_VSCROLL:
{
// Get all the vertial scroll bar information.
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd, SB_VERT, &si);
// Save the position for comparison later on.
yPos = si.nPos;
switch (LOWORD(wParam))
{
// User clicked the top arrow.
case SB_LINEUP:
si.nPos -= 1;
break;
// User clicked the bottom arrow.
case SB_LINEDOWN:
si.nPos += 1;
break;
// User clicked the scroll bar shaft above the scroll box.
case SB_PAGEUP:
si.nPos -= si.nPage;
break;
// User clicked the scroll bar shaft below the scroll box.
case SB_PAGEDOWN:
si.nPos += si.nPage;
break;
// User dragged the scroll box.
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
break;
default:
break;
}
// Set the position and then retrieve it. Due to adjustments
// by Windows it may not be the same as the value set.
si.fMask = SIF_POS;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
GetScrollInfo(hwnd, SB_VERT, &si);
// If the position has changed, scroll window and update it.
if (si.nPos != yPos)
{
ScrollWindow(hwnd, 0, yChar * (yPos - si.nPos), NULL, NULL);
UpdateWindow(hwnd);
}
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
有关更多详细信息,请参阅"使用滚动条"。
相关文章:
- 为什么使用数组元素查找最大数字的程序不起作用?
- 用于拆分空格字符串的程序不起作用
- 反转一个数字程序不起作用,为什么?
- C++从句子中删除给定字符的程序不起作用
- 为什么当我选择>250000个采样点时,程序不起作用?
- 我的C 程序不起作用
- C++加密程序不起作用
- 发送到64位应用程序不起作用
- 猪拉丁程序不起作用
- 结构程序不起作用
- 为什么程序不起作用
- 找不到WCHAR.H文件-Xcode 9.0中断QT创建者 - 旧修复程序不起作用
- C++为什么我的程序不起作用..:S.
- C++帮助!程序不起作用
- 如果我将类声明为常量,程序不起作用...而不是康特斯普?
- 为什么我的C++程序不起作用
- c++11-实现Promise的示例程序不起作用
- 为什么我的程序不起作用以及如何阻止它快速关闭
- 我的火狐扩展程序不起作用。使用C++XPCOM组件的Firefox扩展(XPI文件)的结构是什么?
- std::在C++中设置删除程序不起作用