本文介绍了虚拟键盘(像键盘一样的Swype)Windows窗体应用程序C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用c#在Windows窗体中创建类似swype的键盘

I am trying to create a swype like keyboard in Windows Forms using c#

我有两个问题:一个.我无法重现手指滑动动作.b.我无法识别不同按键下的字母.

I have two problems:a. I am not able to reproduce the finger swipe action.b. I am not able to recognize the letters under different keys.

第一个问题:

我使用了Graphics类和Pen类,并像这样使用它:

I used the Graphics Class and the Pen class and used it like this :

    bool draw;
    Graphics g;
    Pen p;
    int prevX;
    int prevY;

    private void Form1_Load(object sender, EventArgs e)
    {
        draw = false;
        g = CreateGraphics();
        p = new Pen(Color.Black, 2);
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        draw = true;
        prevX = e.X;
        prevY = e.Y;
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        draw = false;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (draw)
        {
            g.DrawLine(p, prevX, prevY, e.X, e.Y);
            prevX = e.X;
            prevY = e.Y;
        }
    }

但是这里的问题是它从屏幕的左上角开始绘制,而不是从按钮的位置开始绘制.它也不会覆盖按钮.路径中的任何控件都与该行重叠.

But the problem here is that it starts drawing from the Left-top corner of the screen and not from the position of the button. It also does't draw over the buttons. Any control that is in the path overlaps the line.

第二期:

a.我使用鼠标按下事件将布尔值"Allow"设置为true,然后在鼠标移动时尝试获取按钮的文本.就像上面的图片一样.我尝试从字母"S"开始,并且当我移至其他字母时,只有字母"S"继续记录.其他方法的鼠标进入"或鼠标悬停"事件都不会发生.

a. I used the mouse down event to set a boolean value "Allow" as true and then when the mouse moves I tried getting the text of the button. Just as in the above picture. I tried to start from letter "S" and as I move over other letters only the letter "S" keeps recording. The Mouse Enter or Mouse Hover event of the other methods do not even occur.

b.我也尝试过使用拖放事件,但它也无法正常工作.我不确定,但我假设按钮不是可拖动的对象,所以拖放事件无效.

b. I also tried using dragdrop event but it doesn't work too. I am not sure but I am assuming that as buttons are not draggable objects the dragdrop event doesn't work.

我不理解或困惑如何实现自己的目标.

I don't understand or am confused how to achieve my goal.

推荐答案

您可以创建自定义控件并自己处理其绘制.

You can create a custom control and handle its drawing yourself.

但是,如果您要使用按钮控件并希望在这些按钮上绘制,则可以将透明面板作为这些按钮的叠加层,并在透明面板上绘制滑动路径:

But if you are going to use Button controls and want to draw over those buttons, you can put a transparent panel as overlay on those buttons and draw the swipe path over the transparent panel:

透明面板

using System.Windows.Forms;
public class TransparentPanel : Panel
{
    const int WS_EX_TRANSPARENT = 0x20;
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;
            return cp;
        }
    }
    protected override void OnPaintBackground(PaintEventArgs e)
    {
    }
}

样本表格

这只是一个示例,用于演示透明控件的用法.为此,只需创建一个表单并将其内容替换为以下代码即可:

This is just an example to demonstrate the usage of transparent control. To do so, it's enough to create a form and replace it's contents with following code:

TransparentPanel overlay;
TableLayoutPanel table;
List<Point> points = new List<Point>();
List<String> keys = new List<string>();
public Form1()
{
    overlay = new TransparentPanel() { Dock = DockStyle.Fill };
    this.Controls.Add(overlay);

    table = new TableLayoutPanel()
    { ColumnCount = 6, RowCount = 4, Dock = DockStyle.Fill };
    this.Controls.Add(table);

    var list = Enumerable.Range('A', 'Z' - 'A' + 1)
        .Select(x => ((char)x).ToString()).ToList();

    list.ForEach(x => table.Controls.Add(new Button()
    { Text = x, Width = 32, Height = 32 }));

    overlay.MouseDown += OverlayMouseDown;
    overlay.MouseMove += OverlayMouseMove;
    overlay.MouseUp += OverlayMouseUp;
    overlay.Paint += OverlayPaint;
}
void OverlayMouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
        ProccessPoint(e.Location);
}
void OverlayMouseDown(object sender, MouseEventArgs e)
{
    Clear();
    ProccessPoint(e.Location);
}
void OverlayMouseUp(object sender, MouseEventArgs e)
{
    if (points.Count > 0)
        MessageBox.Show(string.Join(",", keys));
    Clear();
}
void OverlayPaint(object sender, PaintEventArgs e)
{
    if (points.Count >= 3)
        e.Graphics.DrawCurve(Pens.Red, points.ToArray());
}
void ProccessPoint(Point p)
{
    points.Add(p);
    var c = table.Controls.Cast<Control>()
        .Where(x => table.RectangleToScreen(x.Bounds)
        .Contains(overlay.PointToScreen(p))).FirstOrDefault();
    if ((c != null) && (keys.Count == 0 || keys[keys.Count - 1] != c.Text))
        keys.Add(c.Text);
    overlay.Invalidate();
}
void Clear()
{
    keys.Clear();
    points.Clear();
    this.Refresh();
}

这篇关于虚拟键盘(像键盘一样的Swype)Windows窗体应用程序C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 17:27