protected override void OnMouseUp(MouseEventArgs mouseArgs) { if (capturedButton != null) { if (capturedButton.IsHit(mouseArgs.X, mouseArgs.Y)) { DoCommand(capturedButton.Cmd); } capturedButton.IsSelected = false; capturedButton = null; } }
protected override void OnMouseDown(MouseEventArgs mouseArgs) { foreach (ButtonCalc button in buttons) { if (button.IsHit(mouseArgs.X, mouseArgs.Y)) { button.IsSelected = true; capturedButton = button; break; } } }
public void RedrawCalc() { // Calculate button size based on window size (button matrix is 5x5) float nfontsize = 11.0f; bool recreatefont = false; int ncomp = windowWidth; if (ncomp > windowHeight) { ncomp = windowHeight; } if (ncomp < 100) { nfontsize = 7; } else if (ncomp < 200) { nfontsize = 8; } else if (ncomp > 350) { nfontsize = 18; } else if (ncomp > 800) { nfontsize = 24; } if (windowFont == null) { recreatefont = true; } else { recreatefont = windowFont.Size != nfontsize; } if (recreatefont) { windowFont = new Font( FontFamily.GenericSansSerif, nfontsize, FontStyle.Bold); } buttonCount = 0; int x, y; int editX; int editY; int editWidth; int editHeight; int row, col; buttonWidth = windowWidth / 5; buttonHeight = windowHeight / 6; SizeMargin = Math.Min(buttonWidth / 8, buttonHeight / 8); buttonWidth -= SizeMargin * 2; buttonHeight -= SizeMargin * 2; // Calculate edit size editX = SizeMargin; editY = SizeMargin; editWidth = windowWidth - (SizeMargin * 2); editHeight = buttonHeight; // Create buttons buttonTopRow = SizeMargin + editHeight + SizeMargin; buttons = new ButtonCalc[(int)Command.Enter + 1]; y = buttonTopRow; for (row = 0; row < 5; row++) { x = SizeMargin; for (col = 0; col < 5; col++) { if (buttonCount <= (int)Command.Enter) { buttons[buttonCount] = new ButtonCalc( this, windowFont, x, y, buttonWidth, buttonHeight, SizeMargin, buttonCaptions[buttonCount], buttonColors[buttonCount], (ScreenKeyboard.Command)buttonCount); buttonCount++; } x += SizeMargin + buttonWidth + SizeMargin; } y += SizeMargin + buttonHeight + SizeMargin; } // Adjust + button // buttons[(int)Command.Add].IsTall = true; // Create edit editBox = new EditCalc(this, windowFont, new Rectangle(editX, editY, editWidth, editHeight)); editBox.IsPassword = IsPassword; editBox.EditString = calc.Render(); this.BackColor = Color.SlateGray; this.Text = "Calculator"; }