UE杂项C++

1.double转字符串可自定义保留位数

参考链接

.h

/// 
	/// double转字符串可自定义保留位数。(	sprintf_s(buffer, "%.9lf", a);9 = 保留位数)
	/// https://deepinout.com/cpp/cpp-examples/g_cpp-program-for-double-to-string-conversion.html
	/// 
	UFUNCTION(BlueprintCallable, Category = "Vince|Math")
		static FString DoubleToString();
.cpp

FString UMyBlueprintFunctionLibrary::DoubleToString()
{
	double a = 3.59564597891546;
	char buffer[100];
	sprintf_s(buffer, "%.9lf", a);
	FString ac = buffer;
	return ac;
}

2.系统/UTC时间戳转换+获取星期几

.h

UENUM(BlueprintType)
enum class EDAYOFWEEK :uint8
{


	//星期一
	Monday = 0,

	//星期二
	Tuesday,

	//星期三
	Wednesday,

	//星期四
	Thursday,

	//星期五
	Friday,

	//星期六
	Saturday,

	//星期天
	Sunday

};


	/// 
	/// 获取星期几
	/// 
	UFUNCTION(BlueprintCallable, Category = "Vince|Math")
		static void GetDayOfWeek(int32 year, int32 month, int32 day, int32& weekInt, EDAYOFWEEK& week);

	// 日期长度判断
	static FString DateLength(int32 Date);

	// 获取当前时间戳
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static int64 GetTimestamp();

// 获取选择时间戳
	UFUNCTION(BlueprintPure, Category = "Vince|Mathe")
		static int64 GetCheckTimestamp(FDateTime checkTime);


	// 时间戳转日期
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static FString TimestampToDatetime(int64 UnixTime, bool bIsLocal = true);

	// 时间戳转小时
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static FString TimestampToHour(int64 UnixTime, bool bIsLocal = true);

	// 时间戳转小时和分钟
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static FString TimestampToHourAndMinute(int64 UnixTime, bool bIsLocal = true);

	// 时间戳转系统时间另一个写法。返回FDataTime
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static FDateTime   ConvertTimestampToDateTime(int64 Timestamp);

	//  时间戳转系统时间另一个写法。把FDataTime转换成Text返回
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static FText ConvertTimestampToSystemTime(int64 Timestamp);
.cpp

void UMyBlueprintFunctionLibrary::GetDayOfWeek(int32 year, int32 month, int32 day, int32& weekInt, EDAYOFWEEK& week)
{

	FDateTime newDataTime = FDateTime(year, month, day);

	weekInt = int(newDataTime.GetDayOfWeek());

	week = EDAYOFWEEK(weekInt);

}

FString UMyBlueprintFunctionLibrary::DateLength(int32 Date)
{
	FString DateString;
	DateString = FString::FromInt(Date);
	if (Date < 10)
	{
		DateString = "0" + DateString;
	}
	return DateString;
}

int64 UMyBlueprintFunctionLibrary::GetTimestamp()
{
	return FDateTime::UtcNow().ToUnixTimestamp();
}

int64 UMSBlueprintFunctionLibrary::GetCheckTimestamp(FDateTime checkTime)
{
	int64 newTimestamp = checkTime.ToUnixTimestamp();

	int64 newDifference = FDateTime::Now().ToUnixTimestamp() - FDateTime::UtcNow().ToUnixTimestamp();

	newTimestamp -= newDifference;
	return newTimestamp;

}


FString UMyBlueprintFunctionLibrary::TimestampToDatetime(int64 UnixTime, bool bIsLocal /*= true*/)
{

	FString Date;

	if (bIsLocal)
	{
		struct tm* timeinfo = new struct tm;
		localtime_s(timeinfo, &UnixTime);
		int year = timeinfo->tm_year + 1900; // years since 1900
		int month = timeinfo->tm_mon + 1; // monthes since January - [0, 11]
		int day = timeinfo->tm_mday;
		int date = year * 10000 + month * 100 + day;
		int hour = timeinfo->tm_hour;
		int minute = timeinfo->tm_min;
		int second = timeinfo->tm_sec;

		Date = DateLength(year) + "-" + DateLength(month) + "-" + DateLength(day) + " " + DateLength(hour) + ":" + DateLength(minute) + ":" + DateLength(second);
		return Date;
	}

	FDateTime Time = FDateTime::FromUnixTimestamp(UnixTime);
	Date = DateLength(Time.GetYear()) + "-" + DateLength(Time.GetMonth()) + "-" + DateLength(Time.GetDay()) + " " + DateLength(Time.GetHour()) + ":" + DateLength(Time.GetMinute()) + ":" + DateLength(Time.GetSecond());
	return Date;
}



FString UMyBlueprintFunctionLibrary::TimestampToHour(int64 UnixTime, bool bIsLocal /*= true*/)
{


	if (bIsLocal)
	{
		struct tm* timeinfo = new struct tm;
		localtime_s(timeinfo, &UnixTime);
		int hour = timeinfo->tm_hour;
		int minute = timeinfo->tm_min;
		return DateLength(hour);

	}

	FDateTime Time = FDateTime::FromUnixTimestamp(UnixTime);
	return DateLength(Time.GetHour());
}

FString UMyBlueprintFunctionLibrary::TimestampToHourAndMinute(int64 UnixTime, bool bIsLocal)
{


	if (bIsLocal)
	{
		struct tm* timeinfo = new struct tm;
		localtime_s(timeinfo, &UnixTime);
		int hour = timeinfo->tm_hour;
		int minute = timeinfo->tm_min;
		return DateLength(hour) + ":" + DateLength(minute);

	}

	FDateTime Time = FDateTime::FromUnixTimestamp(UnixTime);
	return DateLength(Time.GetHour()) + ":" + DateLength(Time.GetMinute());
}


FDateTime  UMyBlueprintFunctionLibrary::ConvertTimestampToDateTime(int64 Timestamp)
{
	time_t RawTime = static_cast(Timestamp);
	struct tm LocalTime;
	_tzset();
	_localtime64_s(&LocalTime, &RawTime);

	FDateTime DateTime(LocalTime.tm_year + 1900, LocalTime.tm_mon + 1, LocalTime.tm_mday, LocalTime.tm_hour, LocalTime.tm_min, LocalTime.tm_sec);
	return DateTime;

}

FText UMyBlueprintFunctionLibrary::ConvertTimestampToSystemTime(int64 Timestamp)
{
	FDateTime DateTime = ConvertTimestampToDateTime(Timestamp);
	FText SystemTimeText = FText::FromString(DateTime.ToString());
	UE_LOG(LogTemp, Warning, TEXT("System Time: %s"), *SystemTimeText.ToString());
	return SystemTimeText;
}

3.获取屏幕分辨率

c++获取屏幕分辨率

UE C++ 正确加windows头文件


.h

//获取屏幕分辨率
UFUNCTION(BlueprintPure, Category = "Vince", meta = (WorldContext = "WorldContextObject", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"))
	static void GetScreenResolution(float& client_width, float& client_height);
.cpp

#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/PreWindowsApi.h"
 
//此处写要调用的windows的头文件
#include 
 
#include "Windows/PostWindowsApi.h"
#include "Windows/HideWindowsPlatformTypes.h"



void UMyBlueprintFunctionLibrary::GetScreenResolution(float& client_width, float& client_height)
{
	HDC hdc = GetDC(NULL);                           // 得到屏幕DC  
	client_width = GetDeviceCaps(hdc, HORZRES);      // 宽  
	client_height = GetDeviceCaps(hdc, VERTRES);     // 高
	ReleaseDC(NULL, hdc);                            // 释放DC
}

你可能感兴趣的:(c++,虚幻引擎)