- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用标准 Windows API 用 C++ 编写应用程序。它使用按钮进行一些简单的注册表修改。按下按钮时,它会更改显示在底部的标签。要更改它,我需要重新绘制窗口(它会根据需要自动更改标签)。但是当我重新绘制窗口时,它开始出现故障。静态标签开始闪烁,按钮完全消失,但在移动窗口后停止。这是它发生的 GIF:
这是我的 WndProc
函数:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
static HFONT s_hFont = NULL;
HWND drive;
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case APPLY_BUTTON:
SetRegistryValues(hWnd);
break;
case CDRIVE_BUTTON:
newDriveSelection = 0;
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE); // tried both this...
break;
case DDRIVE_BUTTON:
newDriveSelection = 1;
InvalidateRect(hWnd, hWndSize, NULL); // ...and this
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_CREATE:
{
const TCHAR* fontName = _T("Tahoma");
const long nFontSize = 10;
HDC hdc = GetDC(hWnd);
LOGFONT logFont = {0};
logFont.lfHeight = -MulDiv(nFontSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
logFont.lfWeight = FW_MEDIUM;
_tcscpy_s(logFont.lfFaceName, fontName);
s_hFont = CreateFontIndirect(&logFont);
ReleaseDC(hWnd, hdc);
//s_hFont = (HFONT)GetStockObject();
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
HWND CDrvButton = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Set to C: Drive", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
20, // x position
20, // y position
156, // Button width
21, // Button height
hWnd, // Parent window
(HMENU)CDRIVE_BUTTON, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
HWND DDrvButton = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Set to D: Drive", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
20, // x position
53, // y position
156, // Button width
21, // Button height
hWnd, // Parent window
(HMENU)DDRIVE_BUTTON, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
HWND quit = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Quit", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
20, // x position
125, // y position
54, // Button width
21, // Button height
hWnd, // Parent window
(HMENU)IDM_EXIT, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
HWND apply = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Apply", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
220, // x position
125, // y position
63, // Button width
21, // Button height
hWnd, // Parent window
(HMENU)APPLY_BUTTON, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
HWND the = CreateWindow(
L"static",
L"ST_U",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
35,
82,
28,
17,
hWnd,
(HMENU)(501),
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
drive = CreateWindow(
L"static",
L"ST_U",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
66,
82,
18,
17,
hWnd,
(HMENU)(501),
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
HWND selected = CreateWindow(
L"static",
L"ST_U",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
87,
82,
196,
17,
hWnd,
(HMENU)(501),
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
HWND newdrv = CreateWindow(
L"static",
L"ST_U",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
25,
99,
276,
23,
hWnd,
(HMENU)(501),
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
SetWindowText(the, L"The");
SetWindowText(drive, GetDriveLetter());
SetWindowText(selected, L"drive is set as the current drive.");
switch (newDriveSelection) {
case 0:
SetWindowText(newdrv, L"The C: drive will be when you click Apply.");
break;
case 1:
SetWindowText(newdrv, L"The D: drive will be when you click Apply.");
break;
default:
SetWindowText(newdrv, L"");
break;
}
SendMessage(CDrvButton, WM_SETFONT, (WPARAM)s_hFont, (LPARAM)MAKELONG(TRUE, 0));
SendMessage(DDrvButton, WM_SETFONT, (WPARAM)s_hFont, (LPARAM)MAKELONG(TRUE, 0));
SendMessage(apply, WM_SETFONT, (WPARAM)s_hFont, (LPARAM)MAKELONG(TRUE, 0));
SendMessage(quit, WM_SETFONT, (WPARAM)s_hFont, (LPARAM)MAKELONG(TRUE, 0));
SendMessage(the, WM_SETFONT, (WPARAM)s_hFont, (LPARAM)MAKELONG(TRUE, 0));
SendMessage(drive, WM_SETFONT, (WPARAM)s_hFont, (LPARAM)MAKELONG(TRUE, 0));
SendMessage(selected, WM_SETFONT, (WPARAM)s_hFont, (LPARAM)MAKELONG(TRUE, 0));
SendMessage(newdrv, WM_SETFONT, (WPARAM)s_hFont, (LPARAM)MAKELONG(TRUE, 0));
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
DeleteObject(s_hFont);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
什么可能导致我的代码出现问题?我认为这是 WM_PAINT 处理程序的问题,因为有时在取消最小化程序时也会发生这种情况。
最佳答案
每次应用程序重绘时您都在创建组件 - 处理 WM_PAINT消息,这就是为什么会出现闪烁。将所有内容从:
HDC hdc = BeginPaint(hWnd, &ps);
// move everything in here to WM_CREATE message handling section
EndPaint(hWnd, &ps);
block 到 WM_CREATE部分。 paint 事件用于绘画,而不是用于创建窗口(组件)、发送消息或处理输入。声明:
switch (newDriveSelection)
更适合 WM_COMMAND消息处理部分。
关于c++ - 使用 RedrawWindow 或 SendMessage(WM_PAINT) 重绘后窗口出现故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45776329/
有人可以解释一下为什么这个脚本不起作用吗? function destroy(ID) { if (confirm("Deleting is a very bad thing! Sure?")
我正在尝试使 WCF Silverlight 故障按此方式工作: MSDN aricle 将 SL 故障添加到我的 Web.config 文件后,我收到以下警告: The element 'behav
这是我要删除的 Haskell 函数 2::Int和 5::Int从列表中: remPrimesFactors25 :: [Int] -> [Int] remPrimesFactors25 [] =
当我想用 ffmpeg 连接和录制两个 mp4 视频时,我遇到了这个问题。我得到的输出是: [concat @ 0x2566e80] DTS 4079 #0:0 (h264 (native) ->
我想在delphi中编写一个程序来模拟以特定速度移动的鼠标指针(类似于AutoIT MouseMove函数)。要么是我的代码错误,要么是 SetCursorPos 在被调用太多次后出现故障。这是我的功
我将“wa、or 和 id”(来自这些州的访问者)设置为重定向到 website1.com - 当我访问该网站时,它会将我重定向到 website1.com(因此它知道我在 WA) 。但如果我将 wa
我们目前正在争论通过 WCF channel 抛出错误与传递指示状态或服务响应的消息是否更好。 故障带有 WCF 的内置支持,您可以使用内置的错误处理程序并做出相应的 react 。然而,这会带来开销
不确定我在这里做错了什么,如果有任何帮助,我们将不胜感激。 尝试创建一个名为“control”的新变量,并在行变量等于这些数字时将其编码为 1,否则编码为 0。 data$control= ifels
我想在应用洞察中记录成功调用的百分比。我看到这篇文章https://learn.microsoft.com/en-us/azure/azure-monitor/app/sampling我认为固定速率采
我正在尝试使用 SVD 和特征分解来使用动态模式分解进行一些数据分析。我遇到了一个简单的问题,即从 Matlab 和 Python 获得不同的结果。我很困惑,不知道为什么 Python 给我错误的结果
This question already has an answer here: mysqli_fetch_assoc() expects parameter / Call to a member
我刚刚开始我的一个实验室,在那里我计算类(class)的 GPA,其信息存储在结构的链接列表中。截至目前,我正在尝试打印所有类(class)信息,以确保它们已正确初始化并添加到链接列表中。 我遇到了一
我正在尝试学习如何使用 visual studio 为 C++ 制作 GUI。但是我在使用 GetWindowText() 函数时遇到了一些问题。它不会将 LPTSTR 标题更改为文本框中的文本,并且
我有一个奇怪的问题。它似乎只出现在测试者的 iPhone 5s 上。它可以在运行最新 iOS (8.3) 的 iPhone 5、6 和 6 plus 上正常运行。 这是代码 -(NSString *)
我正在尝试更新 Core Data 中的一些记录。我正在采取以下步骤来完成它 带谓词的获取函数从核心数据中检索记录 将结果集存储在对象数组中 遍历数组并更新每条记录 调用保存上下文 我遇到了两个问题
我通过 Storyboard设计了 tableView,在一个单元格中我有一个按钮和一个标签。按钮在 Storyboard上有标签 1 和标签在 Storyboard上有标签 2。在 cellForR
我实现了这个方法,当在文本字段中输入了未经授权的字符或已使用的用户名时,向用户发送多个警报 View : func textFieldShouldEndEditing(textField: UITex
伙计们,我在运行程序时遇到了这个非常奇怪的错误。这是重要的代码: 变量(编辑): const short int maxX = 100; const short int maxZ = 100; con
我有这个修改过的 Matrix Javascript 代码,我想摆脱第一次运行的所有与自身重叠的字符串。有人知道我该如何管理吗?另外,我想在我的网页上多次使用此代码,我需要声明新变量,不是吗?但是当我
有谁知道是否有网站(甚至非 Microsoft)有关于 COMExceptions/HRESULTS 的详细信息。 当我尝试在使用 Copy() 函数后保存我的 Excel 工作簿时,我收到此错误:
我是一名优秀的程序员,十分优秀!