create_icon.cpp

本文介绍了一个简单的Windows应用程序示例,展示了如何使用Windows API进行窗口创建、图标处理及消息循环等基本操作,并针对不同版本的Windows系统进行了适配。

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1194442938015&lmt=1194190197&format=336x280_as&output=html&correlator=1194442937843&url=file%3A%2F%2F%2FC%3A%2FDocuments%2520and%2520Settings%2Flhh1%2F%E6%A1%8C%E9%9D%A2%2FCLanguage.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=583001034.1194442938&ga_sid=1194442938&ga_hid=1942779085&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> 
#include <windows.h> 
#include "Create_Icon.h"


#if defined (WIN32)
 #define IS_WIN32 TRUE
#else
 #define IS_WIN32 FALSE
#endif

#define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
#define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
#define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32

HINSTANCE hInst;   // current instance

LPCTSTR lpszAppName  = "MyApp";
LPCTSTR lpszTitle    = "My Application";

BOOL RegisterWin95( CONST WNDCLASS* lpwc );

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                      LPTSTR lpCmdLine, int nCmdShow)
{
   MSG      msg;
   HWND     hWnd;
   WNDCLASS wc;

   // Register the main application window class.
   //............................................
   wc.style         = CS_HREDRAW | CS_VREDRAW;
   wc.lpfnWndProc   = (WNDPROC)WndProc;      
   wc.cbClsExtra    = 0;                     
   wc.cbWndExtra    = 0;                     
   wc.hInstance     = hInstance;             
   wc.hIcon         = LoadIcon( hInstance, lpszAppName );
   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
   wc.lpszMenuName  = lpszAppName;             
   wc.lpszClassName = lpszAppName;             

   if ( IS_WIN95 )
   {
      if ( !RegisterWin95( &wc ) )
         return( FALSE );
   }
   else if ( !RegisterClass( &wc ) )
      return( FALSE );

   hInst = hInstance;

   // Create the main application window.
   //....................................
   hWnd = CreateWindow( lpszAppName,
                        lpszTitle,   
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, 0,
                        CW_USEDEFAULT, 0, 
                        NULL,             
                        NULL,             
                        hInstance,        
                        NULL              
                      );

   if ( !hWnd )
      return( FALSE );

   ShowWindow( hWnd, nCmdShow );
   UpdateWindow( hWnd );        

   while( GetMessage( &msg, NULL, 0, 0) )  
   {
      TranslateMessage( &msg );
      DispatchMessage( &msg ); 
   }

   return( msg.wParam );
}


BOOL RegisterWin95( CONST WNDCLASS* lpwc )
{
   WNDCLASSEX wcex;

   wcex.style         = lpwc->style;
   wcex.lpfnWndProc   = lpwc->lpfnWndProc;
   wcex.cbClsExtra    = lpwc->cbClsExtra;
   wcex.cbWndExtra    = lpwc->cbWndExtra;
   wcex.hInstance     = lpwc->hInstance;
   wcex.hIcon         = lpwc->hIcon;
   wcex.hCursor       = lpwc->hCursor;
   wcex.hbrBackground = lpwc->hbrBackground;
   wcex.lpszMenuName  = lpwc->lpszMenuName;
   wcex.lpszClassName = lpwc->lpszClassName;

   // Added elements for Windows 95.
   //...............................
   wcex.cbSize = sizeof(WNDCLASSEX);
   wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName,
                            IMAGE_ICON, 16, 16,
                            LR_DEFAULTCOLOR );
   
   return RegisterClassEx( &wcex );
}

#define EVENBYTE( i )  ((i+7)/8*8)

LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
static HICON hIcon;

   switch( uMsg )
   {
      case WM_CREATE :
              {
                 LPBYTE lpA, lpX;
                 LPBYTE lpAND, lpXOR;
                 int    i,j;
                 int    nWidth     = GetSystemMetrics( SM_CXICON );
                 int    nHeight    = GetSystemMetrics( SM_CYICON );
                 int    nIconBytes = (EVENBYTE(nWidth)/8)*nHeight;

                 // Allocate memory for the bit masks.
                 //...................................
                 lpA = lpAND = (LPBYTE)GlobalAlloc( GPTR, nIconBytes );
                 lpX = lpXOR = (LPBYTE)GlobalAlloc( GPTR, nIconBytes );

                 // Fill masks with icon image.
                 //............................
                 for( i=0; i < EVENBYTE( nWidth ) / 8; i++ )
                 {
                    for( j=0; j < nHeight; j++ )
                    {
                       *lpA++ = 0xFF;
                       if ( i > j )
                          *lpX++ = 0xFF;
                       else
                          *lpX++ = 0x11;
                    }
                 }

                 // Create icon.
                 //.............
                 hIcon = CreateIcon( hInst, nWidth, nHeight, 1, 1,
                                     lpAND, lpXOR );

                 GlobalFree( lpAND );
                 GlobalFree( lpXOR );
              }
              break;
           

      case WM_PAINT  :
              {
                 PAINTSTRUCT ps;

                 BeginPaint( hWnd, &ps );
                 DrawIcon( ps.hdc, 10, 10, hIcon );
                 EndPaint( hWnd, &ps );
              }
              break;

      case WM_COMMAND :
              switch( LOWORD( wParam ) )
              {
                 case IDM_ABOUT :
                        DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
                        break;

                 case IDM_EXIT :
                        DestroyWindow( hWnd );
                        break;
              }
              break;
     
      case WM_DESTROY :
              DestroyIcon( hIcon );
              PostQuitMessage(0);
              break;

      default :
            return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
   }

   return( 0L );
}


LRESULT CALLBACK About( HWND hDlg,          
                        UINT message,       
                        WPARAM wParam,      
                        LPARAM lParam)
{
   switch (message)
   {
       case WM_INITDIALOG:
               return (TRUE);

       case WM_COMMAND:                             
               if (   LOWORD(wParam) == IDOK        
                   || LOWORD(wParam) == IDCANCEL)   
               {
                       EndDialog(hDlg, TRUE);       
                       return (TRUE);
               }
               break;
   }

   return (FALSE);
}

// ToolBoxBar.cpp: implementation of the CToolBoxBar class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "ToolBoxBar.h" #include "resource.h" #include "grouptybase.h" #include "Sp_drawView.h" #include "sp_draw.h" #include "group_doc.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif const COLORREF clrList = RGB (0, 204, 153); ///////////////////////////////////////////////////////////////////////////// // CToolBoxBar BEGIN_MESSAGE_MAP(CToolBoxBar, CBCGPDockingControlBar) //{{AFX_MSG_MAP(CToolBoxBar) ON_WM_CREATE() ON_WM_SIZE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CToolBoxBar construction/destruction CToolBoxBar::CToolBoxBar() { // TODO: add one-time construction code here m_grouptype.Empty(); } CToolBoxBar::~CToolBoxBar() { } ///////////////////////////////////////////////////////////////////////////// // CWorkspaceBar message handlers int CToolBoxBar::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CBCGPDockingControlBar::OnCreate(lpCreateStruct) == -1) return -1; CRect rectDummy; rectDummy.SetRectEmpty (); // m_images.Create (IDB_SHORTCUTS, 32, 0, RGB (255, 0, 255)); // CBitmap bmpIcons; // bmpIcons.LoadBitmap (IDB_SHORTCUTS); m_images.Create (48, 64, ILC_COLOR32 | ILC_MASK, 0, 10); m_images.SetImageCount(10); // m_images.Add (&bmpIcons, RGB (255, 0, 255)); // Create CListCtrl windows. // TODO: create your own tab windows here: const DWORD dwStyle = LVS_ICON | LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL; m_wndList.Create (dwStyle, CRect (0, 0, 0, 0), this, (UINT)-1); m_wndList.SetBkColor (clrList); m_wndList.SetTextBkColor (clrList); m_wndList.m_pBar=this; // Setup list content: m_wndList.SetImageList (&m_images, LVSIL_NORMAL); //m_wndList.InsertItem (0,_T("One"),0); //m_wndList.InsertItem (1,_T("Two"),1); //m_wndList.InsertItem (2,_T("Three"),2); //m_wndList.InsertItem (3,_T("Four"),3); return 0; } void CToolBoxBar::OnSize(UINT nType, int cx, int cy) { CBCGPDockingControlBar::OnSize(nType, cx, cy); // List control should cover a whole client area: m_wndList.SetWindowPos (NULL, 0, 0, cx, cy, SWP_NOACTIVATE | SWP_NOZORDER); }
07-18
import os import sys import json import shutil import threading import winreg import ctypes from PIL import Image, ImageTk import tkinter as tk from tkinter import ttk, messagebox, filedialog from ctypes import wintypes # 启用Windows 10/11的亚克力效果 if sys.platform == 'win32': try: DWMWA_USE_IMMERSIVE_DARK_MODE = 20 DWMWA_WINDOW_CORNER_PREFERENCE = 33 DWMWA_SYSTEMBACKDROP_TYPE = 38 # Windows 11的背景效果类型 class BackdropType: DWMSBT_DISABLE = 1 # 无特效 DWMSBT_MAINWINDOW = 2 # 云母效果 DWMSBT_TRANSIENTWINDOW = 3 # 亚克力效果 DWMSBT_TABBEDWINDOW = 4 # 标签页效果 # 设置窗口特效 def set_window_effect(hwnd, effect_type): if not hasattr(ctypes, "windll"): return try: dwm = ctypes.windll.dwmapi value = ctypes.c_int(effect_type) dwm.DwmSetWindowAttribute( wintypes.HWND(hwnd), DWMWA_SYSTEMBACKDROP_TYPE, ctypes.byref(value), ctypes.sizeof(value) ) except Exception as e: print(f"设置窗口特效失败: {e}") # 设置深色模式 def set_dark_mode(hwnd): if not hasattr(ctypes, "windll"): return try: dwm = ctypes.windll.dwmapi value = ctypes.c_int(1) # 深色模式 dwm.DwmSetWindowAttribute( wintypes.HWND(hwnd), DWMWA_USE_IMMERSIVE_DARK_MODE, ctypes.byref(value), ctypes.sizeof(value) ) except Exception as e: print(f"设置深色模式失败: {e}") except Exception as e: print(f"初始化Windows特效失败: {e}") class DesktopGrid: """表示桌面上的一个整理格子""" def __init__(self, canvas, x, y, size, color, index, target_dir): self.canvas = canvas self.x = x self.y = y self.size = size self.color = color self.index = index self.target_dir = target_dir self.files = [] self.selected = False self.drag_start = None self.hovered = False # 创建圆角矩形 self.rect = self.create_rounded_rectangle( x, y, x+size, y+size, radius=15, fill=color, outline="#FFFFFF", width=1, alpha=180 # 70% 透明度 ) # 创建标签 self.label = canvas.create_text( x + size/2, y + size/2, text=f"格子 {index}", fill="white", font=("Segoe UI", 11, "bold"), anchor=tk.CENTER ) # 绑定事件 self.canvas.tag_bind(self.rect, "<ButtonPress-1>", self.on_press) self.canvas.tag_bind(self.rect, "<B1-Motion>", self.on_drag) self.canvas.tag_bind(self.rect, "<ButtonRelease-1>", self.on_release) self.canvas.tag_bind(self.rect, "<Enter>", self.on_enter) self.canvas.tag_bind(self.rect, "<Leave>", self.on_leave) # 确保目标目录存在 os.makedirs(target_dir, exist_ok=True) def create_rounded_rectangle(self, x1, y1, x2, y2, radius=25, **kwargs): """创建圆角矩形(支持半透明)""" # 创建透明图像作为纹理 alpha = kwargs.pop('alpha', 255) fill = kwargs.pop('fill', '#FFFFFF') # 创建圆角矩形图像 image = Image.new('RGBA', (self.size, self.size), (0, 0, 0, 0)) pixels = image.load() # 计算圆角 for y in range(self.size): for x in range(self.size): # 检查是否在圆角范围内 dist_x = min(x, self.size - x - 1) dist_y = min(y, self.size - y - 1) # 计算到最近角的距离 if dist_x < radius and dist_y < radius: dist = ((radius - dist_x) ** 2 + (radius - dist_y) ** 2) ** 0.5 if dist > radius: continue # 设置像素颜色(带透明度) r, g, b = int(fill[1:3], 16), int(fill[3:5], 16), int(fill[5:7], 16) pixels[x, y] = (r, g, b, alpha) # 转换为Tkinter图像 self.tk_image = ImageTk.PhotoImage(image) return self.canvas.create_image( x1 + self.size/2, y1 + self.size/2, image=self.tk_image, tags="grid" ) def on_press(self, event): """鼠标按下事件""" self.drag_start = (event.x, event.y) self.selected = True self.canvas.tag_raise(self.rect) self.canvas.tag_raise(self.label) self.update_appearance() def on_drag(self, event): """拖动事件""" if self.drag_start: dx = event.x - self.drag_start[0] dy = event.y - self.drag_start[1] self.canvas.move(self.rect, dx, dy) self.canvas.move(self.label, dx, dy) self.x += dx self.y += dy self.drag_start = (event.x, event.y) # 检查是否需要吸附到其他格子 self.check_snap() def check_snap(self): """检查并执行吸附到其他格子""" snap_threshold = 20 # 吸附阈值 for grid in self.canvas.grids: if grid != self: # 水平吸附 if abs(self.x - grid.x) < snap_threshold: self.x = grid.x # 垂直吸附 if abs(self.y - grid.y) < snap_threshold: self.y = grid.y # 更新位置 self.canvas.coords(self.rect, self.x, self.y) self.canvas.coords(self.label, self.x + self.size/2, self.y + self.size/2) def on_release(self, event): """鼠标释放事件""" self.drag_start = None self.update_appearance() def on_enter(self, event): """鼠标进入事件""" self.hovered = True self.update_appearance() def on_leave(self, event): """鼠标离开事件""" self.hovered = False self.update_appearance() def update_appearance(self): """更新格子的外观(悬停/选中状态)""" if self.selected: # 选中状态 - 更亮的颜色 self.canvas.itemconfig( self.rect, image=self.create_highlighted_image(self.color, 220) ) elif self.hovered: # 悬停状态 - 稍亮的颜色 self.canvas.itemconfig( self.rect, image=self.create_highlighted_image(self.color, 200) ) else: # 正常状态 self.canvas.itemconfig( self.rect, image=self.create_normal_image(self.color) ) def create_normal_image(self, color): """创建正常状态的图像""" return self.create_rounded_rectangle( self.x, self.y, self.x+self.size, self.y+self.size, radius=15, fill=color, alpha=180 ) def create_highlighted_image(self, color, alpha): """创建高亮状态的图像""" # 计算高亮颜色(增加亮度) r = min(255, int(int(color[1:3], 16) * 1.2)) g = min(255, int(int(color[3:5], 16) * 1.2)) b = min(255, int(int(color[5:7], 16) * 1.2)) new_color = f"#{r:02x}{g:02x}{b:02x}" return self.create_rounded_rectangle( self.x, self.y, self.x+self.size, self.y+self.size, radius=15, fill=new_color, alpha=alpha ) def add_file(self, file_path): """添加文件到格子""" try: # 移动文件到目标目录 filename = os.path.basename(file_path) dest_path = os.path.join(self.target_dir, filename) # 避免覆盖同名文件 counter = 1 base_name, ext = os.path.splitext(filename) while os.path.exists(dest_path): new_filename = f"{base_name}_{counter}{ext}" dest_path = os.path.join(self.target_dir, new_filename) counter += 1 shutil.move(file_path, dest_path) self.files.append(dest_path) # 更新标签显示文件名 display_name = filename if len(filename) < 15 else filename[:12] + "..." self.canvas.itemconfig( self.label, text=f"格子 {self.index}\n{display_name}" ) return True except Exception as e: messagebox.showerror("错误", f"添加文件失败: {str(e)}") return False class DesktopOrganizerApp: def __init__(self, root): self.root = root self.root.title("高级桌面整理工具") self.root.geometry("1000x700+200+100") # 设置深色背景作为基础 self.root.configure(bg='#2B2B2B') # 设置Windows特效 if sys.platform == 'win32': try: # 获取窗口句柄 hwnd = ctypes.windll.user32.GetParent(root.winfo_id()) # 设置深色模式 set_dark_mode(hwnd) # 设置云母效果 set_window_effect(hwnd, BackdropType.DWMSBT_MAINWINDOW) except Exception as e: print(f"设置Windows特效失败: {e}") # 回退方案:设置半透明背景 self.root.attributes('-alpha', 0.95) # 修复:使用有效的背景色 self.canvas = tk.Canvas( root, bg='#2B2B2B', # 使用深灰色背景 highlightthickness=0 ) self.canvas.pack(fill=tk.BOTH, expand=True) self.canvas.grids = [] # 存储所有格子 # 初始格子布局 self.grid_colors = [ '#FF6B6B', '#4ECDC4', '#FFE66D', '#1A535C', '#FF9F1C' ] # 创建初始格子 self.create_initial_grids() # 右键菜单 self.context_menu = tk.Menu(root, tearoff=0, bg='#3C3C3C', fg='white') self.context_menu.add_command( label="添加新格子", command=self.add_new_grid, background='#3C3C3C', foreground='white' ) self.context_menu.add_command( label="删除选中格子", command=self.delete_selected_grid, background='#3C3C3C', foreground='white' ) self.context_menu.add_command( label="添加文件", command=self.add_files_to_grid, background='#3C3C3C', foreground='white' ) self.context_menu.add_separator() self.context_menu.add_command( label="保存布局", command=self.save_layout, background='#3C3C3C', foreground='white' ) self.context_menu.add_command( label="加载布局", command=self.load_layout, background='#3C3C3C', foreground='white' ) self.context_menu.add_separator() self.context_menu.add_command( label="退出", command=root.destroy, background='#3C3C3C', foreground='white' ) # 绑定事件 self.root.bind("<Button-3>", self.show_context_menu) self.root.bind("<Control-n>", lambda e: self.add_new_grid()) self.root.bind("<Delete>", lambda e: self.delete_selected_grid()) self.root.bind("<Control-s>", lambda e: self.save_layout()) self.root.bind("<Control-o>", lambda e: self.load_layout()) # 状态栏 self.status_var = tk.StringVar() self.status_var.set("就绪 | 右键菜单添加文件或格子") status_bar = ttk.Label( root, textvariable=self.status_var, relief=tk.SUNKEN, anchor=tk.W, background='#333333', foreground='white' ) status_bar.pack(side=tk.BOTTOM, fill=tk.X) # 文件监视器 self.start_file_monitor() def create_initial_grids(self): """创建初始格子布局""" grid_size = 220 positions = [ (50, 50), # 左上 (300, 50), # 右上 (50, 300), # 左下 (300, 300), # 右下 (550, 180) # 中间 ] for i, (x, y) in enumerate(positions): color = self.grid_colors[i % len(self.grid_colors)] target_dir = os.path.expanduser(f"~/Desktop/整理格子_{i+1}") grid = DesktopGrid( self.canvas, x, y, grid_size, color, i+1, target_dir ) self.canvas.grids.append(grid) def add_new_grid(self): """添加新格子""" color = self.grid_colors[len(self.canvas.grids) % len(self.grid_colors)] target_dir = os.path.expanduser(f"~/Desktop/整理格子_{len(self.canvas.grids)+1}") grid = DesktopGrid( self.canvas, 400, 300, 220, color, len(self.canvas.grids)+1, target_dir ) self.canvas.grids.append(grid) self.status_var.set(f"已添加新格子: {len(self.canvas.grids)}") def delete_selected_grid(self): """删除选中的格子""" for grid in self.canvas.grids[:]: if grid.selected: self.canvas.delete(grid.rect) self.canvas.delete(grid.label) self.canvas.grids.remove(grid) self.status_var.set("已删除选中的格子") return self.status_var.set("没有选中的格子") def save_layout(self): """保存布局到文件""" try: layout_data = [] for grid in self.canvas.grids: layout_data.append({ "x": grid.x, "y": grid.y, "size": grid.size, "color": grid.color, "index": grid.index, "target_dir": grid.target_dir }) # 保存到用户文档目录 save_path = os.path.expanduser("~/Documents/desktop_grid_layout.json") with open(save_path, "w") as f: json.dump(layout_data, f, indent=4) self.status_var.set(f"布局已保存到: {save_path}") messagebox.showinfo("成功", f"布局已保存到: {save_path}") except Exception as e: messagebox.showerror("错误", f"保存失败: {str(e)}") def load_layout(self): """从文件加载布局""" try: load_path = os.path.expanduser("~/Documents/desktop_grid_layout.json") if not os.path.exists(load_path): messagebox.showerror("错误", f"找不到布局文件: {load_path}") return with open(load_path, "r") as f: layout_data = json.load(f) # 清除现有格子 for grid in self.canvas.grids[:]: self.canvas.delete(grid.rect) self.canvas.delete(grid.label) self.canvas.grids = [] # 创建新格子 for data in layout_data: grid = DesktopGrid( self.canvas, data["x"], data["y"], data["size"], data["color"], data["index"], data["target_dir"] ) self.canvas.grids.append(grid) self.status_var.set(f"已加载布局: {load_path}") messagebox.showinfo("成功", f"已从 {load_path} 加载布局") except Exception as e: messagebox.showerror("错误", f"加载布局失败: {str(e)}") def add_files_to_grid(self): """添加文件到选中的格子""" selected_grid = None for grid in self.canvas.grids: if grid.selected: selected_grid = grid break if not selected_grid: messagebox.showwarning("警告", "请先选择一个格子") return file_paths = filedialog.askopenfilenames( title="选择要添加的文件", filetypes=[("所有文件", "*.*")] ) if not file_paths: return for file_path in file_paths: if selected_grid.add_file(file_path): self.status_var.set(f"已添加: {os.path.basename(file_path)} -> 格子 {selected_grid.index}") def show_context_menu(self, event): """显示右键菜单""" self.context_menu.post(event.x_root, event.y_root) def start_file_monitor(self): """启动文件监视器(监视桌面文件夹)""" # 获取桌面路径 desktop_path = os.path.expanduser("~/Desktop") # 使用线程监视桌面文件夹 threading.Thread( target=self.monitor_desktop, args=(desktop_path,), daemon=True ).start() self.status_var.set("已启动桌面文件监视器...") def monitor_desktop(self, path): """监视桌面文件夹并自动整理新文件""" import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler class DesktopHandler(FileSystemEventHandler): def __init__(self, app): self.app = app self.last_handled = set() self.last_event_time = 0 def on_created(self, event): # 只处理文件(跳过目录) if not event.is_directory: current_time = time.time() # 防止短时间内重复触发 if current_time - self.last_event_time < 0.5: return self.last_event_time = current_time file_path = event.src_path # 延迟处理,确保文件已完全写入 time.sleep(0.5) # 避免重复处理 if file_path not in self.last_handled: self.last_handled.add(file_path) # 在新线程中整理文件 threading.Thread( target=self.app.auto_organize_file, args=(file_path,), daemon=True ).start() event_handler = DesktopHandler(self) observer = Observer() observer.schedule(event_handler, path, recursive=False) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() def auto_organize_file(self, file_path): """自动整理文件到合适的格子""" # 确保文件存在 if not os.path.exists(file_path) or os.path.isdir(file_path): return # 获取文件扩展名 _, ext = os.path.splitext(file_path) ext = ext.lower() # 根据文件类型选择格子 category = self.classify_file(ext) # 查找对应类别的格子 target_grid = None for grid in self.canvas.grids: grid_category = self.classify_grid(grid) if grid_category == category: target_grid = grid break # 如果没有找到匹配的格子,使用第一个格子 if not target_grid and self.canvas.grids: target_grid = self.canvas.grids[0] # 添加文件 if target_grid: if target_grid.add_file(file_path): self.status_var.set(f"自动整理: {os.path.basename(file_path)} -> {target_grid.target_dir}") def classify_file(self, ext): """根据文件扩展名分类文件""" categories = { 'documents': ['.pdf', '.doc', '.docx', '.txt', '.xls', '.xlsx', '.ppt', '.pptx', '.rtf'], 'images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.webp', '.tiff'], 'videos': ['.mp4', '.avi', '.mov', '.mkv', '.flv', '.wmv', '.mpeg'], 'music': ['.mp3', '.wav', '.flac', '.aac', '.ogg', '.m4a'], 'archives': ['.zip', '.rar', '.7z', '.tar', '.gz', '.bz2'], 'executables': ['.exe', '.msi', '.bat', '.sh'], 'code': ['.py', '.js', '.html', '.css', '.java', '.cpp', '.cs', '.php'] } for cat, exts in categories.items(): if ext in exts: return cat return 'others' def classify_grid(self, grid): """根据格子中的文件类型确定格子类别""" if not grid.files: return 'others' # 统计格子中文件类型的分布 type_count = {} for file_path in grid.files: _, ext = os.path.splitext(file_path) ext = ext.lower() cat = self.classify_file(ext) type_count[cat] = type_count.get(cat, 0) + 1 # 返回最常见的类型 return max(type_count, key=type_count.get) # 创建主窗口 if __name__ == "__main__": root = tk.Tk() app = DesktopOrganizerApp(root) # 设置任务栏图标 if sys.platform == 'win32': try: base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__))) icon_path = os.path.join(base_path, "app_icon.ico") root.iconbitmap(icon_path) except: try: # 尝试从当前目录加载 root.iconbitmap("app_icon.ico") except: print("图标文件加载失败,使用默认图标") # 启动主循环 root.mainloop() [Running] python -u "e:\system\Desktop\项目所需文件\工具\桌面整理工具\Desktop Grid Organizer.py" Traceback (most recent call last): File "e:\system\Desktop\\u9879�ڏ�������\�H��\\u684c�ʐ����H��\Desktop Grid Organizer.py", line 639, in <module> root.iconbitmap(icon_path) File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2156, in wm_iconbitmap return self.tk.call('wm', 'iconbitmap', self._w, bitmap) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ _tkinter.TclError: bitmap "e:\system\Desktop\\u9879�ڏ�������\�H��\\u684c�ʐ����H��\app_icon.ico" not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "e:\system\Desktop\\u9879�ڏ�������\�H��\\u684c�ʐ����H��\Desktop Grid Organizer.py", line 643, in <module> root.iconbitmap("app_icon.ico") File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2156, in wm_iconbitmap return self.tk.call('wm', 'iconbitmap', self._w, bitmap) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ _tkinter.TclError: bitmap "app_icon.ico" not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "e:\system\Desktop\\u9879�ڏ�������\�H��\\u684c�ʐ����H��\Desktop Grid Organizer.py", line 645, in <module> print("\u56fe\u6807������\u8f7d��\u8d25�C�g�p��\u8ba4\u56fe\u6807") UnicodeEncodeError: 'cp932' codec can't encode character '\u56fe' in position 0: illegal multibyte sequence [Done] exited with code=1 in 0.766 seconds
最新发布
10-18
void PunchStdHole_Punch::showTListPhAP(int Index) { S_PunchAP* thePunch = static_cast<S_PunchAP*>(allPunch_All[Index]); char dataBuf[256]; //Create node and node data Node* newNode; //Shape=刃口形状 newNode = treeControlPunch->CreateNode("Shape=刃口形状"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->shape); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.shapeStr); //D=杆径 newNode = treeControlPunch->CreateNode("D=杆径"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->D); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.dStr); //L newNode = treeControlPunch->CreateNode("L");//创建节点 treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->L); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.lStr); newNode->SetForegroundColor(198); //P newNode = treeControlPunch->CreateNode("P");//创建节点 treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->pPunch); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.pStr); //B=刃口长度 newNode = treeControlPunch->CreateNode("B=刃口长度"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->B); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.bStr); //H newNode = treeControlPunch->CreateNode("H"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->H); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.hStr); //R newNode = treeControlPunch->CreateNode("R"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->Rpunch); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.rStr); //W newNode = treeControlPunch->CreateNode("W"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->wPunch); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.wStr); //CODE_SKC=追加工SKC newNode = treeControlPunch->CreateNode("CODE_SKC=追加工SKC"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->CODE_SKC); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.CODE_SKC_Str); newNode->SetForegroundColor(198); addNode(treeControlPunch,"CODE_LC=全长追加工",thePunch->CODE_LC,thePunch->m_punchAPJ.CODE_LC_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_LC_FW=全长追加工范围",thePunch->CODE_LC_FW,thePunch->m_punchAPJ.CODE_LC_FW_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_LKC_LKZ_PD=变更全长公差",thePunch->CODE_LKC_LKZ_PD,thePunch->m_punchAPJ.CODE_LKC_LKZ_PD_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_LKC_LKZ_XZ=变更全长公差类型",thePunch->CODE_LKC_LKZ_XZ,thePunch->m_punchAPJ.CODE_LKC_LKZ_XZ_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_KC=追加工角度",thePunch->CODE_KC,thePunch->m_punchAPJ.CODE_KC_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_WKC=WKC追加工",thePunch->CODE_WKC,thePunch->m_punchAPJ.CODE_WKC_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_KC_FW=追加工角度范围",thePunch->CODE_KC_FW,thePunch->m_punchAPJ.CODE_KC_FW_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_KFC=KFC追加工",thePunch->CODE_KFC,thePunch->m_punchAPJ.CODE_KFC_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_NKC=NKC追加工",thePunch->CODE_NKC,thePunch->m_punchAPJ.CODE_NKC_Str,198,"arrowdown","arrowdown"); //SUM1 newNode = treeControlPunch->CreateNode("SUM1"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->m_punchAP.SUM1_Str); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.SUM1_Str); //BRAND newNode = treeControlPunch->CreateNode("BRAND"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->m_punchAP.brandStr); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.brandStr); //DESCRIPTION_CN newNode = treeControlPunch->CreateNode("DESCRIPTION_CN"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->m_punchAP.descriStr); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.descriStr); //GG newNode = treeControlPunch->CreateNode("GG"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->m_punchAP.ggStr); newNode->SetColumnDisplayText(2,thePunch->m_punchAP.ggStr); }void PunchStdHole_Punch::showTListPhAPJ(int Index) { S_PunchAPJ* thePunch = static_cast<S_PunchAPJ*>(allPunch_All[Index]); char dataBuf[256]; //Create node and node data Node* newNode; //Shape=刃口形状 newNode = treeControlPunch->CreateNode("Shape=刃口形状"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->shape); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.shapeStr); //D=杆径 newNode = treeControlPunch->CreateNode("D=杆径"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->D); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.dStr); //L newNode = treeControlPunch->CreateNode("L");//创建节点 treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->L); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.lStr); newNode->SetForegroundColor(198); //P newNode = treeControlPunch->CreateNode("P");//创建节点 treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->pPunch); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.pStr); //B=刃口长度 newNode = treeControlPunch->CreateNode("B=刃口长度"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->B); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.bStr); //H newNode = treeControlPunch->CreateNode("H"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->H); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.hStr); //R newNode = treeControlPunch->CreateNode("R"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->Rpunch); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.rStr); //W newNode = treeControlPunch->CreateNode("W"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); sprintf(dataBuf,"%.2f",thePunch->wPunch); newNode->SetColumnDisplayText(1,dataBuf); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.wStr); //CODE_SKC=追加工SKC newNode = treeControlPunch->CreateNode("CODE_SKC=追加工SKC"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->CODE_SKC); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.CODE_SKC_Str); newNode->SetForegroundColor(198); addNode(treeControlPunch,"CODE_LC=全长追加工",thePunch->CODE_LC,thePunch->m_punchAPJ.CODE_LC_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_LC_FW=全长追加工范围",thePunch->CODE_LC_FW,thePunch->m_punchAPJ.CODE_LC_FW_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_LKC_LKZ_PD=变更全长公差",thePunch->CODE_LKC_LKZ_PD,thePunch->m_punchAPJ.CODE_LKC_LKZ_PD_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_LKC_LKZ_XZ=变更全长公差类型",thePunch->CODE_LKC_LKZ_XZ,thePunch->m_punchAPJ.CODE_LKC_LKZ_XZ_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_KC=追加工角度",thePunch->CODE_KC,thePunch->m_punchAPJ.CODE_KC_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_WKC=WKC追加工",thePunch->CODE_WKC,thePunch->m_punchAPJ.CODE_WKC_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_KC_FW=追加工角度范围",thePunch->CODE_KC_FW,thePunch->m_punchAPJ.CODE_KC_FW_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_KFC=KFC追加工",thePunch->CODE_KFC,thePunch->m_punchAPJ.CODE_KFC_Str,198,"arrowdown","arrowdown"); addNode(treeControlPunch,"CODE_NKC=NKC追加工",thePunch->CODE_NKC,thePunch->m_punchAPJ.CODE_NKC_Str,198,"arrowdown","arrowdown"); //SUM1 newNode = treeControlPunch->CreateNode("SUM1"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->m_punchAPJ.SUM1_Str); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.SUM1_Str); //BRAND newNode = treeControlPunch->CreateNode("BRAND"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->m_punchAPJ.brandStr); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.brandStr); //DESCRIPTION_CN newNode = treeControlPunch->CreateNode("DESCRIPTION_CN"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->m_punchAPJ.descriStr); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.descriStr); //GG newNode = treeControlPunch->CreateNode("GG"); treeControlPunch->InsertNode(newNode,NULL,NULL,Tree::NodeInsertOptionLast); newNode->SetColumnDisplayText(1,thePunch->m_punchAPJ.ggStr); newNode->SetColumnDisplayText(2,thePunch->m_punchAPJ.ggStr); }让上面的函数参照着下面的函数来改
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值