这段代码有什么问题?
我想要一个进度条出现,但我什么也没得到。 我已经复制了这个视频的代码,但没有复制窗口的设置。
这不是我程序的全部,所以可能那里的代码有问题。
如果你想要所有的代码,只要问我,我会把它全部编辑进去。
#include <iostream>
#include <Windows.h>
#include <CommCtrl.h>
// PB Defines
HINSTANCE hInstance;
HWND hProgress;
DWORD IDC_TIMER;
DWORD nCounter;
// Window #defines
#define WNDNAME L"ProgressBar"
#define WND_CLASS L"ProgressBarclass"
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
int main() {}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdshow) {
hInstance = hInst;
WNDCLASSW wc = { 0 };
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = hInst;
wc.lpszClassName = WND_CLASS;
wc.lpfnWndProc = WindowProcedure;
if (!RegisterClassW(&wc)) {
MessageBoxA(NULL, "Register Failed", "Register Failed", MB_OK);
}
CreateWindowW(WND_CLASS, WNDNAME, WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE,
625, 200, 650, 750, NULL, NULL, NULL, NULL);
MSG msg = { 0 };
while (GetMessage(&msg, NULL, NULL, NULL)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
switch (msg)
{
case WM_INITDIALOG: {
hProgress = CreateWindowEx(0, PROGRESS_CLASS, (LPTSTR)NULL,
WS_CHILD | WS_VISIBLE | PBS_SMOOTH, 45, 625, 300, 35,
hWnd, (HMENU)0, hInstance, NULL);
SendMessage(hProgress, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
SendMessage(hProgress, PBM_SETSTEP, (WPARAM)10, 0);
SetTimer(hWnd, IDC_TIMER, 100, 0);
nCounter = 0;
break;
}
case WM_TIMER: {
nCounter++;
SendMessage(hProgress, PBM_STEPIT, 0, 0);
if (nCounter == 10) {
KillTimer(hWnd, IDC_TIMER);
}
break;
}
case WM_CREATE:
break;
case WM_DESTROY:
PostQuitMessage(-1);
break;
default:
return DefWindowProcW(hWnd, msg, wp, lp);
}
}
使用 Visual Studio 和 Win10.
提前致谢。
CreateWindowW()
创建主窗口,它不会发送 WM_INITDIALOG
,因此您永远不会创建进度条或计时器。这就是为什么您看不到他们做任何事情的原因。
DialogBox()
而不是 CreateWindowW()
创建其主窗口。 DialogBox()
发送WM_INITDIALOG
(因此得名)。
CreateWindowW()
时,你需要将你的WM_INITDIALOG
逻辑转移到WM_CREATE
中。