WPF绘图程序问题:切换至填充模式无法绘制矩形
Hey there! Let's figure out why your fill mode isn't drawing anything when you switch from the line tool. Based on what you shared—using stacked rectangles from a starting point and the drawing boolean flag—here are the most likely fixes and checks:
1. Ensure Radio Button Switches Update Your Tool State First
First, check the Checked event for your "Fill" radio button. If you're not resetting the drawing flag or setting a tool-tracking variable when switching modes, your mouse logic might still be stuck in line-drawing mode. Here's a quick example of what that should look like:
private void FillRadio_Checked(object sender, RoutedEventArgs e) { // Reset the fill start flag to capture a new starting point drawing = false; // If you have a variable tracking current tool (e.g., currentTool = Tool.Fill), set that here too }
Without this reset, drawing might be stuck in an incorrect state that prevents the fill logic from triggering.
2. Fix the MouseMove Logic for Fill Mode
Chances are your MouseMove event only handles line drawing, and the fill mode branch is missing or broken. Let's add the fill-specific logic, including proper starting point capture and rectangle generation:
private Point _fillStartPoint; // Store the initial starting point for fill rectangles private void cnvs_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { // Check if we're in fill mode first if (FillRadio.IsChecked == true) { if (!drawing) { // Capture the starting point when the mouse is first pressed _fillStartPoint = e.GetPosition(cnvs); drawing = true; } else { // Calculate rectangle bounds (handles mouse dragging left/up from start point) Point currentPos = e.GetPosition(cnvs); double left = Math.Min(_fillStartPoint.X, currentPos.X); double top = Math.Min(_fillStartPoint.Y, currentPos.Y); double width = Math.Abs(currentPos.X - _fillStartPoint.X); double height = Math.Abs(currentPos.Y - _fillStartPoint.Y); // Create your fill rectangle Rectangle fillRect = new Rectangle { Fill = Brushes.DarkOrange, // Replace with your desired fill color Width = width, Height = height }; Canvas.SetLeft(fillRect, left); Canvas.SetTop(fillRect, top); // Critical note: For live preview, don't add a new rectangle every move! // Instead, keep one preview rectangle and update its properties each time, then finalize on MouseUp cnvs.Children.Add(fillRect); } } else if (LineRadio.IsChecked == true) { // Your existing line drawing logic here } } }
A key tip: If you want a live drag preview, avoid adding a new rectangle every MouseMove—this will flood your canvas instantly. Instead, create one preview rectangle once, update its position/size as the mouse moves, and only add a permanent rectangle when the user releases the mouse.
3. Hook Up MouseDown/MouseUp to Control the drawing Flag
Your drawing flag should be triggered on MouseDown, not just checked in MouseMove. Add these events to properly handle the start/end of a fill operation:
private void cnvs_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed && FillRadio.IsChecked == true) { drawing = true; _fillStartPoint = e.GetPosition(cnvs); } } private void cnvs_MouseUp(object sender, MouseButtonEventArgs e) { drawing = false; // If using a preview rectangle, this is where you'd add it permanently to the canvas }
Without MouseDown setting drawing to true, the MouseMove logic will never enter the fill drawing branch.
4. Double-Check Your Canvas Settings
Quick sanity check: Make sure your cnvs canvas isn't blocked by another control, and that its IsEnabled property is set to true and Visibility is Visible. If the canvas isn't receiving mouse events, none of your logic will run.
内容的提问来源于stack exchange,提问作者ShadowTrolll

