1

I have an application A that listen an application B (using Hook)

[Application A]1

ListBox 1 = All application in my desktop with Button or TextBox

[Application B]

As you can see I already have Red Rectangle, I can use two method :

var dc = GetWindowDC((LBListControl.SelectedItem as Data).Value);
            using (Graphics g = Graphics.FromHdc(dc))
            {
                g.DrawRectangle(Pens.Red, 0,0,50,20);
                g.Dispose();
                g.ReleaseHdc(dc);
            }
            // paintedRectangle = lRectangle[i];
            //ControlPaint.DrawReversibleFrame(paintedRectangle, SystemColors.Highlight, FrameStyle.Dashed);

// -> Second Method. It's function that I call when I change my ListBox.SelectedIndex

private void LBListControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PaintRedRectangle(LBListControl.SelectedIndex);
            }
            catch (ArgumentOutOfRangeException)
            {

            }
        }

lRectangle is a list that contains X, Y of my button / textbox (Widht / Height are set by myself).

I heard that using the handle i could change BorderStyle / BorderColor of a controller. If it's true I didn't found a thing about that and I would like to know if you know something.

Else how could I supress my RedRectangle when I want to show someone else ?

BTW : Using g.ReleaseHdc(dc); doesn't work parameter is invalid (Don't know why)

Thank you in advance

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • Take a look at [Change border-color in TextBox](https://stackoverflow.com/a/39420512/3110834) and [Change border-color in ComboBox](https://stackoverflow.com/a/34886006/3110834) for example. – Reza Aghaei Nov 10 '17 at 09:19
  • I guess you don't need to change the border-color and it's enough for you to draw a border **around** them, in fact on their parent graphics object. – Reza Aghaei Nov 10 '17 at 09:21
  • Change Border-Color of TextBox and Button when i chose them in my ListBox (the right listbox on my Application A picture). Edit : Didn't see it was a link ! Gonna read it (didn't find it at first) – Steven Jeanne Nov 10 '17 at 09:21
  • Ok so my bad, i read it yesterday via : https://stackoverflow.com/questions/9768938/change-the-bordercolor-of-the-textbox (You commented here too :) ) But I didn't try the method you shown here : https://stackoverflow.com/questions/17466067/change-border-color-in-textbox-c-sharp/39420512#39420512 EDIT : Yeah I do want to draw a border around them (that's why i'm drawing a RedRectangle, but I can't erase it to only have one rectangle – Steven Jeanne Nov 10 '17 at 09:26
  • You may also find this post useful [Put dotted border around all controls.](https://stackoverflow.com/a/40209045/3110834) – Reza Aghaei Nov 10 '17 at 09:27
  • Well, it's funny because I read this post too yet I didn't want to use an invisible form. My Application B is only to test my Application A. I'm gonna use it to hook an other application. – Steven Jeanne Nov 10 '17 at 09:30
  • There is no invisible form. It's a transparent panel. This way you can draw over all controls. – Reza Aghaei Nov 10 '17 at 09:33
  • All controls in the main context ? Does that work for third-party application ? – Steven Jeanne Nov 10 '17 at 09:35
  • No. Just for your main application. For third party application, you need to use a transparent form. – Reza Aghaei Nov 10 '17 at 09:36
  • Thank you :) You are really helpful ! I must use a transparent form then – Steven Jeanne Nov 10 '17 at 09:38

1 Answers1

0

Thanks to Reza Aghaei I made it !

I created a new form :

public partial class InvisibleFrame : Form
    {
        public InvisibleFrame()
        {
            InitializeComponent();
            BackColor = Color.Lime;
            TransparencyKey = Color.Lime;
            FormBorderStyle = FormBorderStyle.None;
            ShowInTaskbar = false;
        }
    }

That I call here :

private void PaintRedRectangle(int i)
    {
        try
        {
            ci.Location = new Point(lRectangle[i].X, lRectangle[i].Y);
            ci.Size = new Size(10, 10);
            ci.BackColor = Color.Red;
            ci.BringToFront();
            ci.Show();
        }
        catch(ObjectDisposedException)
        {
            ci = new InvisibleFrame();
            PaintRedRectangle(i);
        }
    }

And I choose the button and the TextBox I want to hook :

private void BTHookTB_Click(object sender, EventArgs e)
{
    if (sender != null)
    {
        ControlHook TextBoxHook = new ControlHook
        {
            Text = LBListControl.SelectedItem.ToString(),
            Position = lRectangle[LBListControl.SelectedIndex],
        };
        lControlHook.Add(TextBoxHook);
        LabelTBHook.Text = "TextBox : " + lControlHook[1].Text;
        BTHookTB.Enabled = false;
        BTReset.Enabled = true;
        BTHook.Enabled = true;
        LBListControl.Enabled = false;
        ci.Hide();
    }
}

I hide it.