主頁 > 後端開發 > x01.paint: 繪圖程式

x01.paint: 繪圖程式

2020-09-30 23:16:25 後端開發

繪圖程式結構簡單,邏輯也不復雜,例如在工具列 (tool_frame) 中選擇畫線 (draw_line), 在選項欄(top_frame) 設定,然后在畫布 (canvas_frame) 中進行繪制即可,其他如畫方畫園等,無論是操作還是實作,都基本類同,

1. 效果圖:

           

2. 代碼:

import os
import sys
import tkinter as tk
from tkinter import colorchooser, messagebox, filedialog

# 為參考 utils,在 site-packages 目錄下新建 mypath.pth 檔案,
# 添加所需匯入模塊的目錄路徑, 如 ‘x01.lab/py/’ 所在路徑,
import utils
from paint.core import CanvasFrame, R, ToolFrame, TopFrame

sys.path.append(utils.R.CurrentDir)


class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('x01.paint')
        utils.R.win_center(self)

        self.menu = tk.Menu(self)
        utils.R.generate_menus(self,['file', 'edit', 'help'], sepindies=(2, 4))
        self.configure(menu=self.menu)

        self.top_frame = TopFrame(self)
        self.top_frame.configure(height=25)
        self.top_frame.pack(side='top', fill='x', pady=2)

        self.tool_frame = ToolFrame(self)
        self.tool_frame.configure(width=80, relief='raised')
        self.tool_frame.pack(side='left', fill='y', pady=3)

        self.canvas_frame = CanvasFrame(self)
        self.canvas_frame.pack(side='right', fill='both', expand='yes')

        self.tool_frame.tool_click(0)

    def file_1_new(self):
        self.canvas_frame.canvas.delete(tk.ALL)
        self.canvas_frame.canvas.config(bg='white')

    def file_2_save(self):
        filename = filedialog.asksaveasfilename(master=self, title='Save',
            filetypes=[('postscript file', '*.ps'), ('All Files', '*.*')])
        if not filename: return
        self.canvas_frame.canvas.postscript(file=filename, colormode='color')
        messagebox.showinfo(title=self.title, message=filename + ' Save success!')

    def file_3_quit(self):
        self.destroy()
    
    def edit_1_undo(self):
        items = list(self.canvas_frame.canvas.find('all'))
        try:
            last_item = items.pop()
        except IndexError:
            return
        self.canvas_frame.canvas.delete(last_item)

    def edit_2_zoom_in(self):
        self.canvas_frame.canvas.scale('all', 0,0,1.2,1.2)
        self.canvas_frame.canvas.config(scrollregion=self.canvas_frame.canvas.bbox(tk.ALL))

    def edit_3_zoom_out(self):
        self.canvas_frame.canvas.scale('all', 0,0,0.8,0.8)
        self.canvas_frame.canvas.config(scrollregion=self.canvas_frame.canvas.bbox(tk.ALL))

    def help_1_about(self):
        messagebox.showinfo('x01.paint', '繪圖程式,著作權屬于x01(黃雄)所有,')


if __name__ == "__main__": 
    win = MainWindow()
    win.mainloop()
main.py
import cmath
import math
import os
import sys
import tkinter as tk

from tkinter import colorchooser, ttk 

import utils


class R:
    Functions = (
        "draw_line", "draw_oval", "draw_rectangle", "draw_arc",
        "draw_triangle", "draw_star", "draw_irregular_line", "draw_super_shape", 
        "draw_text", "delete_item", "fill_item", "duplicate_item", 
        "move_to_top", "drag_item", "enlarge_item_size", "reduce_item_size"
    )

    SuperShapes = {
        "shape A": (1.5, 1.5, 5, 2, 7, 7),
        "shape B": (1.5, 1.5, 3, 5, 18, 18),
        "shape C": (1.4, 1.4, 4, 2, 4, 13),
        "shape D": (1.6, 1.6, 7, 3, 4, 17),
        "shape E": (1.9, 1.9, 7, 3, 6, 6),
        "shape F": (4, 4, 19, 9, 14, 11),
        "shape G": (12, 12, 1, 15, 20, 3),
        "shape H": (1.5, 1.5, 8, 1, 1, 8),
        "shape I": (1.2, 1.2, 8, 1, 5, 8),
        "shape J": (8, 8, 3, 6, 6, 6),
        "shape K": (8, 8, 2, 1, 1, 1),
        "shape L": (1.1, 1.1, 16, 0.5, 0.5, 16)

    }


class CanvasFrame(tk.Frame):
    current_item = None 
    fill = 'red'
    outline = 'red'
    width = 2.0 
    number_of_spokes = 5
    arrow = None
    dash = None
    x1,y1,x2,y2 = 0,0,0,0
    selected_super_shape = 'shape A'

    def __init__(self, master):
        super().__init__(master)
        self.master = master

        self.init_canvas()
        self.bind_events()

    def bind_events(self):
        self.canvas.bind('<Button-1>', self.mouse_left_pressed)
        self.canvas.bind('<Button1-Motion>', self.mouse_pressed_motion)
        self.canvas.bind('<Motion>', self.mouse_unpressed_motion)

    def mouse_left_pressed(self, e=None):
        self.x1 = self.x2 = e.x
        self.y1 = self.y2 = e.y
        self.execute_selected_method()

    def mouse_pressed_motion(self, e=None):
        self.x2 = e.x 
        self.y2 = e.y 
        self.canvas.delete(self.current_item)
        self.execute_selected_method()

    def mouse_unpressed_motion(self, e=None): 
        self.master.tool_frame.current_coordinate_label.config(text='x:{}\ny:{}'.format(e.x,e.y))

    def init_canvas(self):
        self.canvas = tk.Canvas(self, background='white', width=500, height=500, scrollregion=(0,0,800,800))
        x_scroll = tk.Scrollbar(self, orient='horizontal')
        x_scroll.pack(side='bottom', fill='x')
        x_scroll.config(command=self.canvas.xview)
        y_scroll = tk.Scrollbar(self, orient='vertical')
        y_scroll.pack(side='right', fill='y')
        y_scroll.config(command=self.canvas.yview)
        self.canvas.config(xscrollcommand=x_scroll.set, yscrollcommand=y_scroll.set)
        self.canvas.pack(side='right', fill='both', expand='yes')

    def execute_selected_method(self):
        self.current_item = None 
        func = getattr(self, self.master.tool_frame.selected_function, self.function_not_defined)
        func()

    def function_not_defined(self): pass

    def draw_line(self):
        self.current_item = self.canvas.create_line(self.x1, self.y1, self.x2, self.y2, fill=self.fill, 
            width=self.width, arrow=self.arrow, dash=self.dash)

    def draw_oval(self):
        self.current_item = self.canvas.create_oval(self.x1, self.y1, self.x2, self.y2, outline=self.outline,
            fill=self.fill, width=self.width)

    def draw_rectangle(self):
        self.current_item = self.canvas.create_rectangle(self.x1, self.y1, self.x2, self.y2, outline=self.outline,
            fill=self.fill, width=self.width)

    def draw_arc(self):
        self.current_item = self.canvas.create_arc(self.x1,self.y1,self.x2, self.y2, outline=self.outline, 
            fill=self.fill, width=self.width)

    def draw_triangle(self):
        dx = self.x2 - self.x1
        dy = self.y2 - self.y1
        z = complex(dx,dy)
        radius, angle0 = cmath.polar(z)
        edges = 3
        points = list()
        for edge in range(edges):
            angle = angle0 + edge * (2*math.pi)/edges
            points.append(self.x1 + radius * math.cos(angle))
            points.append(self.y1 + radius * math.sin(angle))
        self.current_item = self.canvas.create_polygon(points, outline=self.outline, 
            fill=self.fill, width=self.width)

    def draw_star(self):
        dx = self.x2 - self.x1 
        dy = self.y2 - self.y1
        z = complex(dx,dy)
        radius_out, angle0 = cmath.polar(z)
        radius_in = radius_out / 2
        points = []
        for edge in range(self.number_of_spokes):
            angle = angle0 + edge * (2*math.pi) / self.number_of_spokes
            points.append(self.x1 + radius_out * math.cos(angle))
            points.append(self.y1 + radius_out * math.sin(angle))
            angle += math.pi / self.number_of_spokes
            points.append(self.x1 + radius_in * math.cos(angle))
            points.append(self.y1 + radius_in * math.sin(angle))
        self.current_item = self.canvas.create_polygon(points, outline=self.outline,
            fill=self.fill, width=self.width)

    def draw_irregular_line(self):
        self.current_item = self.canvas.create_line(self.x1,self.y1,self.x2,self.y2,
            fill=self.fill, width=self.width)
        self.canvas.bind('<B1-Motion>', self.update_irregular_line)

    def update_irregular_line(self, e=None):
        self.x1, self.y1 = self.x2, self.y2
        self.x2,self.y2 = e.x, e.y
        self.draw_irregular_line()

    def draw_super_shape(self):
        points = self.get_shape_points(self.selected_super_shape)
        self.current_item = self.canvas.create_polygon(points, fill=self.fill,
            outline=self.outline, width=self.width)

    def get_shape_points(self, key):
        a,b,m,n1,n2,n3 = R.SuperShapes[key]
        points = []
        for i in self.float_range(0, 2*math.pi, 0.01):
            raux = (abs(1 / a * abs(math.cos(m*i/4))) ** n2
                + abs(1 / b * abs(math.sin(m*i/4))) ** n3)
            r = abs(raux) ** (-1/n1)
            x = self.x2 + r * math.cos(i)
            y = self.y2 + r * math.sin(i)
            points.extend((x,y))
        return points

    def float_range(self, start, end, step):
        while start < end:
            yield start 
            start += step 

    def draw_text(self): 
        text = self.master.top_frame.text_entry.get()
        fontsize = self.master.top_frame.fontsize_spinbox.get()
        self.current_item = self.canvas.create_text(self.x2,self.y2, text=text,
            font=('', fontsize), fill=self.fill)

    def delete_item(self):
        self.current_item = None 
        self.canvas.delete('current')

    def fill_item(self):
        try:
            self.canvas.itemconfig('current', fill=self.fill, outline=self.outline)
        except TclError:
            self.canvas.itemconfig('current', fill=self.fill)

    def duplicate_item(self):
        try:
            function_name = 'create_' + self.canvas.type('current')
        except TypeError:
            return 
        coordinates = tuple(map(lambda i: i+10, self.canvas.coords('current')))
        configs = self.get_configs()
        self.function_wrapper(function_name, coordinates, configs)

    def get_configs(self):
        configs = {}
        for k,v in self.canvas.itemconfig('current').items():
            if v[-1] and v[-1] not in ['0', '0.0', '0,0', 'current']:
                configs[k] = v[-1]
        return configs

    def function_wrapper(self, function_name, *arg, **kwargs):
        func = getattr(self.canvas, function_name)
        func(*arg, **kwargs)

    def move_to_top(self):
        self.current_item = None
        self.canvas.tag_raise('current')

    def drag_item(self): 
        self.canvas.move('current', self.x2-self.x1, self.y2-self.y1)
        self.canvas.bind('<B1-Motion>', self.drag_update)

    def drag_update(self, e=None):
        self.x1, self.y1 = self.x2, self.y2
        self.x2, self.y2 = e.x, e.y 
        self.drag_item()

    def enlarge_item_size(self): 
        self.current_item = None 
        if self.canvas.find_withtag('current'):
            self.canvas.scale('current', self.x2, self.y2, 1.2, 1.2)
            self.canvas.config(scrollregion=self.canvas.bbox(tk.ALL))

    def reduce_item_size(self): 
        self.current_item = None 
        if self.canvas.find_withtag('current'):
            self.canvas.scale('current', self.x2, self.y2, 0.8, 0.8)
            self.canvas.config(scrollregion=self.canvas.bbox(tk.ALL))


class ToolFrame(tk.Frame):
    def __init__(self, master):
        super().__init__(master=master)
        self.master = master
        self.selected_function = R.Functions[0]
        self.foreground = 'red'
        self.background = 'white'

        self.create_tool_buttons()
        self.create_color_palette()
        self.create_current_coordinate_label()

    def create_current_coordinate_label(self):
        self.current_coordinate_label = tk.Label(self, text='x:0\ny:0')
        self.current_coordinate_label.grid(row=11,column=1, columnspan=2,padx=1, sticky='w')

    def create_color_palette(self):
        self.color_palette = tk.Canvas(self, height=55, width=55)
        self.color_palette.grid(row=10,column=1,columnspan=2, pady=5, padx=5)
        self.background_rect = self.color_palette.create_rectangle(15,15,40,40,
            outline=self.background, fill=self.background)
        self.foreground_rect = self.color_palette.create_rectangle(1,1,33,33,
            outline=self.foreground, fill=self.foreground)
        self.bind_color_palette()

    def bind_color_palette(self):
        self.color_palette.tag_bind(self.background_rect, '<Button-1>', self.set_background_color)
        self.color_palette.tag_bind(self.foreground_rect, '<Button-1>', self.set_foreground_color)

    def set_foreground_color(self, e=None):
        self.foreground = colorchooser.askcolor(title='select foreground color')[-1]
        self.color_palette.itemconfig(self.foreground_rect, outline=self.foreground, fill=self.foreground)

    def set_background_color(self, e=None):
        self.background = colorchooser.askcolor(title='select background color')[-1]
        self.color_palette.itemconfig(self.background_rect, outline=self.background, fill=self.background)

    def create_tool_buttons(self):
        for i, name in enumerate(R.Functions):
            icon = tk.PhotoImage(file=os.path.join(utils.R.CurrentDir, 'paint/icons/'+name+'.gif'))
            self.button = tk.Button(self, image=icon, command=lambda i=i: self.tool_click(i))
            self.button.grid(row=i//2,column=1+i%2, sticky='nsew')
            self.button.image=icon 

    def tool_click(self, i):
        self.selected_function = R.Functions[i]
        self.remove_top_function()
        self.add_top_function()
        self.master.canvas_frame.bind_events()

    def remove_top_function(self):
        for child in self.master.top_frame.winfo_children():
            child.destroy()
            
    def add_top_function(self): 
        name = self.selected_function.replace('_', ' ').capitalize() + ':'
        tk.Label(self.master.top_frame, text=name).pack(side='left', padx=5)
        icon = tk.PhotoImage(file=os.path.join(utils.R.CurrentDir, 'paint/icons/'+self.selected_function+'.gif'))
        label = tk.Label(self.master.top_frame, image=icon)
        label.image = icon 
        label.pack(side='left')
        self.master.top_frame.create_options(self.selected_function)


class TopFrame(tk.Frame):
    def __init__(self, master):
        super().__init__(master=master)

    def create_options(self, selected_function):
        name = '{}_options'.format(selected_function)
        func = getattr(self, name, self.function_not_defined)
        func()

    def function_not_defined(self): pass 

    def draw_line_options(self):
        self.create_fill_combobox()
        self.create_width_combobox()
        self.create_arrow_combobox()
        self.create_dash_combobox()

    def draw_oval_options(self):
        self.create_fill_combobox()
        self.create_outline_combobox()
        self.create_width_combobox()

    def draw_rectangle_options(self):
        self.create_fill_combobox()
        self.create_outline_combobox()
        self.create_width_combobox()

    def draw_arc_options(self):
        self.create_fill_combobox()
        self.create_outline_combobox()
        self.create_width_combobox()

    def draw_triangle_options(self):
        self.create_fill_combobox()
        self.create_outline_combobox()
        self.create_width_combobox()

    def draw_star_options(self):
        self.create_spokes_combobox()
        self.create_fill_combobox()
        self.create_outline_combobox()
        self.create_width_combobox()

    def draw_irregular_line_options(self):
        self.create_fill_combobox()
        self.create_width_combobox()

    def draw_super_shape_options(self):
        self.create_shapes_combobox()
        self.create_fill_combobox()
        self.create_outline_combobox()
        self.create_width_combobox()

    def draw_text_options(self):
        self.create_text_entry()
        self.create_fontsize_spinbox()
        self.create_fill_combobox()

    def fill_item_options(self):
        self.create_fill_combobox()
        self.create_outline_combobox()

    def create_fill_combobox(self):
        tk.Label(self,text='Fill:').pack(side='left', padx=5)
        self.fill_combobox = ttk.Combobox(self, state='readonly', width=5)
        self.fill_combobox.pack(side='left')
        self.fill_combobox['values'] = ('none', 'fg', 'bg', 'black', 'white')
        self.fill_combobox.bind('<<ComboboxSelected>>', self.set_fill)
        self.fill_combobox.set(self.master.canvas_frame.fill)

    def create_width_combobox(self):
        tk.Label(self, text='Width:').pack(side='left', padx=5)
        self.width_combobox = ttk.Combobox(self, state='readonly', width=3)
        self.width_combobox.pack(side='left')
        self.width_combobox['values'] = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
        self.width_combobox.bind('<<ComboboxSelected>>', self.set_width)
        self.width_combobox.set(1)

    def create_arrow_combobox(self):
        tk.Label(self, text='Arrow:').pack(side='left', padx=5)
        self.arrow_combobox = ttk.Combobox(self, state='readonly', width=5)
        self.arrow_combobox.pack(side='left')
        self.arrow_combobox['values'] = ('none', 'first', 'last', 'both')
        self.arrow_combobox.bind('<<ComboboxSelected>>', self.set_arrow)
        self.arrow_combobox.set('none')

    def create_dash_combobox(self):
        tk.Label(self, text='Dash:').pack(side='left', padx=5)
        self.dash_combobox = ttk.Combobox(self, state='readonly', width=5)
        self.dash_combobox.pack(side='left')
        self.dash_combobox['values'] = ('none', 'small', 'mediun', 'large')
        self.dash_combobox.bind('<<ComboboxSelected>>', self.set_dash)
        self.dash_combobox.set('none')

    def create_outline_combobox(self):
        tk.Label(self, text='Outline:').pack(side='left', padx=5)
        self.outline_combobox = ttk.Combobox(self, state='readonly', width=5)
        self.outline_combobox.pack(side='left')
        self.outline_combobox['values'] = ('none', 'fg', 'bg', 'black', 'white')
        self.outline_combobox.bind('<<ComboboxSelected>>', self.set_outline)
        self.outline_combobox.set(self.master.canvas_frame.outline)

    def create_spokes_combobox(self):
        tk.Label(self,text='Spokes:').pack(side='left', padx=5)
        self.spokes_combobox = ttk.Combobox(self, state='readonly', width=5)
        self.spokes_combobox.pack(side='left')
        self.spokes_combobox['values'] = tuple(i for i in range(5,50))
        self.spokes_combobox.bind('<<ComboboxSelected>>', self.set_spokes)
        self.spokes_combobox.set(5)

    def create_shapes_combobox(self):
        tk.Label(self,text='Shapes:').pack(side='left', padx=5)
        self.shapes_combobox = ttk.Combobox(self, state='readonly', width=8)
        self.shapes_combobox.pack(side='left')
        self.shapes_combobox['values'] = sorted(tuple(i for i in R.SuperShapes.keys()))
        self.shapes_combobox.bind('<<ComboboxSelected>>', self.set_shapes)
        self.shapes_combobox.set(self.master.canvas_frame.selected_super_shape)

    def create_text_entry(self):
        tk.Label(self, text='Text:').pack(side='left', padx=5)
        self.text_entry = tk.Entry(self, width=20)
        self.text_entry.pack(side='left')

    def create_fontsize_spinbox(self):
        tk.Label(self, text='Font size:').pack(side='left', padx=5)
        v = tk.IntVar()
        v.set(12)
        self.fontsize_spinbox = tk.Spinbox(self, from_=2,to=100,width=3, textvariable=v)
        self.fontsize_spinbox.pack(side='left')

    def set_width(self, e=None):
        self.master.canvas_frame.width = float(self.width_combobox.get())

    def set_fill(self, e=None):
        clr = self.fill_combobox.get()
        if clr == 'none':
            self.master.canvas_frame.fill = ''
        elif clr == 'fg':
            self.master.canvas_frame.fill = self.master.tool_frame.foreground
        elif clr == 'bg':
            self.master.canvas_frame.fill = self.master.tool_frame.background
        else :
            self.master.canvas_frame.fill = clr 

    def set_arrow(self, e=None):
        self.master.canvas_frame.arrow = self.arrow_combobox.get()


    def set_dash(self, e=None):
        dash = self.fill_combobox.get()
        if dash == 'none':
            self.master.canvas_frame.dash = None
        elif dash == 'small':
            self.master.canvas_frame.dash = 1
        elif dash == 'medium':
            self.master.canvas_frame.dash = 15
        elif dash == 'large':
            self.master.canvas_frame.dash = 100 

    def set_outline(self, e=None):
        v = self.outline_combobox.get()
        if v == 'none':
            self.master.canvas_frame.outline = ''
        elif v == 'fg':
            self.master.canvas_frame.outline = self.master.tool_frame.foreground
        elif v == 'bg':
            self.master.canvas_frame.outline = self.master.tool_frame.background
        else:
            self.master.canvas_frame.outline = v

    def set_spokes(self, e=None):
        self.master.canvas_frame.number_of_spokes = int(self.spokes_combobox.get())

    def set_shapes(self, e=None):
        self.master.canvas_frame.selected_super_shape = self.shapes_combobox.get()

if __name__ == "__main__": 
    pass     
core.py

3. 下載:

x01.lab/py/paint

 

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/143483.html

標籤:Python

上一篇:🤔字串相同ID竟然不同🤔

下一篇:hdu+1002

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more