做一个桌面悬浮翻页时钟

毛玻璃效果翻页桌面悬浮时钟,TopMost(Topmost=“True”),不在任务栏显示(ShowInTaskbar=“False”),在托盘区显示图标,双击托盘区图标实现最小化和还原,右键托盘图标可选“最小化”和“退出”

1.安装HandyControl

2.前端代码

<Window
    x:Class="IClock.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:hc="https://handyorg.github.io/handycontrol"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="IClock"
    Width="300"
    Height="80"
    AllowsTransparency="True"
    Background="Transparent"
    Icon="/clock.png"
    ShowInTaskbar="False"
    Topmost="True"
    WindowStyle="None"
    mc:Ignorable="d">
    <Window.Resources>
        <ContextMenu
            x:Key="ContextMenu"
            Width="160">
            <MenuItem
                Click="Min"
                Header="最小化" />
            <MenuItem
                Click="Exit"
                Header="退出软件" />
        </ContextMenu>
    </Window.Resources>
    <Grid>
        <hc:NotifyIcon
            ContextMenu="{StaticResource ContextMenu}"
            Icon="/clock.png"
            MouseDoubleClick="NotifyIcon_MouseDoubleClick"
            Text="IClock"
            Visibility="Visible" />
        <Border CornerRadius="10"
            Background="White"
            Opacity="0.4" />
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <Viewbox>
                <hc:FlipClock
                    Margin="10"
                    MouseLeftButtonDown="FlipClock_MouseLeftButtonDown" />
            </Viewbox>
        </StackPanel>
    </Grid>
</Window>

3.后端代码

	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
		}

		private void Exit(object sender, RoutedEventArgs e)
		{
			Close();
		}

		private void Min(object sender, RoutedEventArgs e)
		{
			WindowState = WindowState.Minimized;
		}

		private void NotifyIcon_MouseDoubleClick(object sender, RoutedEventArgs e)
		{
			WindowState = WindowState == WindowState.Minimized ? WindowState.Normal : WindowState.Minimized;
		}

		private void FlipClock_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			this.DragMove();
		}
	}

4.源代码

【免费】wpf桌面悬浮时钟源代码资源-CSDN文库

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里提供一种实现方式: 1. 在 Android Studio 中创建一个新的空项目。 2. 在项目中添加一个悬浮窗口布局文件,命名为 `floating_window.xml`,并在其中添加一个 `TextView` 显示当前时间。 3. 创建一个 `Service` 类来管理悬浮窗口。在 `onCreate()` 方法中创建 `WindowManager` 实例,并通过 `WindowManager.LayoutParams` 设置参数来显示悬浮窗口。在 `onDestroy()` 方法中移除悬浮窗口。 4. 在 `Service` 类中实现一个 `Runnable` 接口的方法,用于更新悬浮窗口中的时间。在 `run()` 方法中使用 `Handler` 定时更新时间。 5. 在 `Service` 类中重写 `onStartCommand()` 方法,启动 `Runnable` 对象,并将其放入线程池中执行。 代码示例: floating_window.xml ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/time_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:textColor="#FFFFFF" android:textStyle="bold"/> </RelativeLayout> ``` FloatingWindowService.java ``` public class FloatingWindowService extends Service implements Runnable { private WindowManager mWindowManager; private WindowManager.LayoutParams mLayoutParams; private TextView mTimeTextView; private Handler mHandler = new Handler(); private ScheduledExecutorService mExecutorService; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); // 创建 WindowManager 实例 mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); // 创建悬浮窗口布局参数 mLayoutParams = new WindowManager.LayoutParams(); mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; mLayoutParams.format = PixelFormat.RGBA_8888; mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP; mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT; mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; // 创建悬浮窗口视图 LayoutInflater inflater = LayoutInflater.from(this); View floatingWindow = inflater.inflate(R.layout.floating_window, null); mTimeTextView = floatingWindow.findViewById(R.id.time_textview); // 将悬浮窗口添加到 WindowManager 中 mWindowManager.addView(floatingWindow, mLayoutParams); // 启动定时器 mExecutorService = Executors.newSingleThreadScheduledExecutor(); mExecutorService.scheduleAtFixedRate(this, 0, 1, TimeUnit.SECONDS); } @Override public void onDestroy() { super.onDestroy(); // 移除悬浮窗口 if (mWindowManager != null) { mWindowManager.removeView(mTimeTextView); } // 关闭定时器 if (mExecutorService != null && !mExecutorService.isShutdown()) { mExecutorService.shutdown(); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Override public void run() { mHandler.post(new Runnable() { @Override public void run() { // 更新时间 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); String timeString = sdf.format(new Date()); mTimeTextView.setText(timeString); } }); } } ``` 在 AndroidManifest.xml 文件中注册 `FloatingWindowService` 服务: ``` <service android:name=".FloatingWindowService"/> ``` 在 MainActivity 中启动 `FloatingWindowService` 服务: ``` public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 启动悬浮时钟服务 Intent intent = new Intent(this, FloatingWindowService.class); startService(intent); } } ``` 这样就完成了一个简单的安卓悬浮时钟的app。需要注意的是,由于 Android 8.0 及以上版本的安全性限制,需要在应用程序中动态请求悬浮窗口权限。可以通过以下代码实现: ``` if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); intent.setData(Uri.parse("package:" + getPackageName())); startActivityForResult(intent, REQUEST_CODE_FLOATING_WINDOW_PERMISSION); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值