day06 滑块进度条

 

 

 

 

#include <Windows.h>
#include <CommCtrl.h>
#include <windowsx.h>
#include "resource.h"


/**
void DialogBoxW(
  [in, optional] hInstance, 包含对话框模板的模块句柄。 如果此参数为 NULL,则使用当前可执行文件。
  [in]           lpTemplate, 对话框模板。 此参数是指向以 null 结尾的字符串的指针,
  该字符串指定对话框模板的名称或指定对话框模板的资源标识符的整数值。 
  如果参数指定资源标识符,则其高序单词必须为零,
  其低序单词必须包含标识符。 可以使用 MAKEINTRESOURCE 宏创建此值。
  [in, optional] hWndParent,拥有对话框的窗口的句柄。
  [in, optional] lpDialogFunc指向对话框过程的指针。 有关对话框过程的详细信息,请参阅 DialogProc。
);



WM_HSCROLL










*/
// 居中函数

void CenterWindow(HWND hWnd);
void SetWindowG(HWND hwnd, int width, int height);
INT_PTR CALLBACK Dlgproc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR     lpCmdLine, int       nShowCmd)
{


	DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_DIALOG1), NULL, Dlgproc);



	return 0;
}


INT_PTR CALLBACK Dlgproc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
)
{

	switch (uMsg)
	{
	case WM_INITDIALOG:
	{
		SetWindowG(hwnd,600,500);

		// 对控件的修改和获取信息,通过发送消息来
		SendDlgItemMessageW(hwnd, IDC_PROGRESS1, PBM_SETRANGE,0,MAKELPARAM(0,100));
		SendDlgItemMessageW(hwnd, IDC_SLIDER1, TBM_SETRANGE, TRUE, MAKELPARAM(0, 100));
		SendDlgItemMessageW(hwnd, IDC_PROGRESS1, PBM_SETPOS,50, 0);
		SendDlgItemMessageW(hwnd, IDC_SLIDER1,TBM_SETPOS, TRUE, 80);

		// 通过进度条来控制滑块



		
		break;
	}
	case WM_NOTIFY:
	{

		break;
	}
	case WM_HSCROLL:
	{
		if (GetDlgItem(hwnd, IDC_SLIDER1) == (HWND)lParam)
		{
			WORD pos = SendDlgItemMessageW(hwnd, IDC_SLIDER1, TBM_GETPOS,0,0);
			SendDlgItemMessageW(hwnd, IDC_PROGRESS1, PBM_SETPOS, pos, 0);
		}

		break;
	}
	case WM_VSCROLL:
	{
		break;
	}


	case WM_CLOSE:
		EndDialog(hwnd, 0);
		break;

	default:
		return FALSE;
		break;
	}
	return TRUE;
	
	
}

// 居中函数
void CenterWindow(HWND hWnd)
{
	RECT rc;
	GetWindowRect(hWnd, &rc);

	int width = rc.right - rc.left;
	int height = rc.bottom - rc.top;

	int screenWidth = GetSystemMetrics(SM_CXSCREEN);
	int screenHeight = GetSystemMetrics(SM_CYSCREEN);

	SetWindowPos(hWnd, NULL,
		(screenWidth - width) / 2,
		(screenHeight - height) / 2,
		0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
void SetWindowG(HWND hwnd,int width,int height)
{
	// DPI自适应
	UINT dpi = GetDpiForWindow(hwnd);
	int baseWidth = width;
	int baseHeight = height;
	int scaledWidth = MulDiv(baseWidth, dpi, 96);
	int scaledHeight = MulDiv(baseHeight, dpi, 96);

	SetWindowPos(hwnd, NULL, 0, 0, scaledWidth, scaledHeight,
		SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);

	// 居中显示
	CenterWindow(hwnd);
}

  

posted @ 2025-12-02 13:26  巨兽~墨菲特  阅读(3)  评论(0)    收藏  举报