C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,362 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,488 people online right now. Registration is fast and FREE... Join Now!




How do I make my app. border disappear?

 

How do I make my app. border disappear?, ...when it loses mouse focus

fooboo

2 Jul, 2009 - 02:30 AM
Post #1

The king of Nynex
Group Icon

Joined: 28 Jul, 2006
Posts: 3,022



Thanked: 1 times
Dream Kudos: 150
My Contributions
I have an app. that I want to sit on my desktop a bit like a yahoo widget.
I have made the background transparent so all I can see when it's running are two lines of text on my desktop's background image.
The trouble is to get it to look like this I had to turn off the form's border. I like how it looks but now I can't drag it around the screen. It'll start up wherever I tell it the default start co-ordinates are and then it'll have to stay there.
If I enable the border I can position the form on my desktop to my hearts content but my nice transparent app. will have a big blue border round it wherever I put it.

What I would like is one of a couple of things.
I've been using a context menu strip so I can right click and choose exit (no border so no red X to click). I'd like to add a toggle to that so I can choose to switch the border on and off. This would mean I can switch it on, move the app to where I want it, then switch it off so it doesn't get accidentally dragged elsewhere and doesn't have a big ugly blue border. The problem is I can't find how to reference the "FormBorderStyle" setting (if that's the right method?).

Or I could have the border just popup on mouse over and go away again when it loses focus...which I also don't know how to do.

Any help or alternate method suggestions would be greatly appreciated.

User is offlineProfile CardPM
+Quote Post


lesPaul456

RE: How Do I Make My App. Border Disappear?

2 Jul, 2009 - 05:48 AM
Post #2

D.I.C Head
Group Icon

Joined: 16 Apr, 2009
Posts: 221



Thanked: 31 times
Dream Kudos: 175
My Contributions
If you would like to leave the border off, but give the user the ability to move it, check out this code snippet.
User is offlineProfile CardPM
+Quote Post

fooboo

RE: How Do I Make My App. Border Disappear?

2 Jul, 2009 - 05:51 AM
Post #3

The king of Nynex
Group Icon

Joined: 28 Jul, 2006
Posts: 3,022



Thanked: 1 times
Dream Kudos: 150
My Contributions
Thanks, I'll have a go. That looks like an idea solution.
User is offlineProfile CardPM
+Quote Post

fooboo

RE: How Do I Make My App. Border Disappear?

2 Jul, 2009 - 06:37 AM
Post #4

The king of Nynex
Group Icon

Joined: 28 Jul, 2006
Posts: 3,022



Thanked: 1 times
Dream Kudos: 150
My Contributions
I'm back.
I don't think I'm doing it right. I am unfamiliar with creating my own events so I think I've screwed it up. It can't find definitions for IsMousDown and LastCursorPosition.
User is offlineProfile CardPM
+Quote Post

fooboo

RE: How Do I Make My App. Border Disappear?

2 Jul, 2009 - 07:03 AM
Post #5

The king of Nynex
Group Icon

Joined: 28 Jul, 2006
Posts: 3,022



Thanked: 1 times
Dream Kudos: 150
My Contributions
Another solution that was offered was to put this:

CODE
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            // *always* let the base class process the message
            base.WndProc(ref m);
            const int WM_NCHITTEST = 0x84;
            const int HTCAPTION = 2;
            const int HTCLIENT = 1; // if Windows is querying where the mouse is and the base form class said
            // it's on the client area, let's cheat and say it's on the title bar instead
            if (m.Msg == WM_NCHITTEST && m.Result.ToInt32() == HTCLIENT) m.Result = new IntPtr(HTCAPTION);
        }

in the form class. This compiles without any errors but doesn't actually seem to do anything as I can't drag the form around still.
User is offlineProfile CardPM
+Quote Post

janne_panne

RE: How Do I Make My App. Border Disappear?

2 Jul, 2009 - 07:12 AM
Post #6

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 527



Thanked: 107 times
My Contributions
QUOTE(fooboo @ 2 Jul, 2009 - 06:37 AM) *

I don't think I'm doing it right. I am unfamiliar with creating my own events so I think I've screwed it up. It can't find definitions for IsMousDown and LastCursorPosition.


You should post your code.

Did you do what the instructions said at section 2?
QUOTE
2. Add these variables to the class:
- private Point LastCursorPosition;
- private bool IsMouseDown;


If you added them, they should be found.

Then you should just add these events to the form and add the code that was on the snippet:

CODE


        this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
        this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
        this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);


        bool IsMouseDown = false;
        Point LastCursorPosition;

        private void Form1_MouseDown(object sender, MouseEventArgs e) {

            IsMouseDown = true;

            LastCursorPosition = new Point(e.X, e.Y);

        }

        private void Form1_MouseUp(object sender, MouseEventArgs e) {

            this.Location = new Point(this.Left - (this.LastCursorPosition.X - e.X), this.Top - (this.LastCursorPosition.Y - e.Y));
    
            this.Invalidate();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e) {

            this.IsMouseDown = false;
        }


I hope this helps you.
User is offlineProfile CardPM
+Quote Post

fooboo

RE: How Do I Make My App. Border Disappear?

2 Jul, 2009 - 07:32 AM
Post #7

The king of Nynex
Group Icon

Joined: 28 Jul, 2006
Posts: 3,022



Thanked: 1 times
Dream Kudos: 150
My Contributions
Oops I had missed step 2 blush.gif

It now compiles but I still can't move the app. When I right click on the app. it behaves as if it's part of the desktop. I get the default desktop menu most of the time and I have to click right between the two lines of text to get my own context menu to show.

I have:

CODE
        public frmAnEur()
        {
            InitializeComponent();

            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.frmAnEur_MouseDown);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmAnEur_MouseMove);
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.frmAnEur_MouseUp);
        }


then a load of code that does the actual translation (the app. translates the day and month into Kernewek for a textual clock)

then:

CODE
private void frmAnEur_MouseDown(object sender, MouseEventArgs e)
        {

            this.IsMouseDown = true;
            this.LastCursorPosition = new Point(e.X, e.Y);

        }

        private void frmAnEur_MouseMove(object sender, MouseEventArgs e)
        {

            if (this.IsMouseDown == true)
            {
                //Move the form
                this.Location = new Point(this.Left - (this.LastCursorPosition.X - e.X), this.Top - (this.LastCursorPosition.Y - e.Y));

                //Redraw the form//
                this.Invalidate();
            }

        }

        private void frmAnEur_MouseUp(object sender, MouseEventArgs e)
        {

            this.IsMouseDown = false;

        }


Do you need the rest of the code too?

This post has been edited by fooboo: 2 Jul, 2009 - 07:32 AM
User is offlineProfile CardPM
+Quote Post

lesPaul456

RE: How Do I Make My App. Border Disappear?

2 Jul, 2009 - 08:06 AM
Post #8

D.I.C Head
Group Icon

Joined: 16 Apr, 2009
Posts: 221



Thanked: 31 times
Dream Kudos: 175
My Contributions
I'm not sure if this is the problem, but I think it may be because you have a context menu strip.

The mouse down event checks to see if any mouse button is being pressed, left or right. Since context menu strips open during a right click event, you may be having some issues.

Try to check to see if the left mouse button is being pressed, then set the IsMouseDown flag to true:
csharp

if (e.Button == MouseButtons.Left)
{
IsMouseDown = true;
LastCursorPosition = new Point(e.X, e.Y);
}


Hope this helps!
User is offlineProfile CardPM
+Quote Post

fooboo

RE: How Do I Make My App. Border Disappear?

2 Jul, 2009 - 08:25 AM
Post #9

The king of Nynex
Group Icon

Joined: 28 Jul, 2006
Posts: 3,022



Thanked: 1 times
Dream Kudos: 150
My Contributions
Hmm no joy. I even deleted the cms and commented out it's methods so there should have been no remnants of it running but still no joy.
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: How Do I Make My App. Border Disappear?

2 Jul, 2009 - 09:16 AM
Post #10

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Check out a tutorial I made long time ago:
http://www.vbforums.com/showthread.php?t=525657

Using TransparencyKey always causes problems when you want it to react like a normal application, but I'm fairly sure my code works globally (as I used it in Aero forms).

EDIT: Well that tutorial's full of other crap too, what you actually want to try is this:
CODE
protected override void WndProc(ref Message m)
{    
    base.WndProc(ref m); // Call original WndProc
    if (m.Msg == 0x84 && m.Result.ToInt32() == 1)
    {
        m.Result = new IntPtr(2); // Tell WndProc they clicked on the title bar
    }
}


This post has been edited by MageUK: 2 Jul, 2009 - 09:22 AM
User is offlineProfile CardPM
+Quote Post

fooboo

RE: How Do I Make My App. Border Disappear?

3 Jul, 2009 - 02:56 AM
Post #11

The king of Nynex
Group Icon

Joined: 28 Jul, 2006
Posts: 3,022



Thanked: 1 times
Dream Kudos: 150
My Contributions
Calloh callay...I got it to do what I wanted.

It was the transparency that everything seemed to dislike. I couldn't get anything to register as focused on the form when the form was transparent so I've found a bit of a kludge that'll do until I can better understand how it all works.

I added a hover event to the visible label instead of the transparent form. That event sets a border style. I can then use the border to drag the app. to where I want it then a menu option (not a context menu) switches the border off again.

I couldn't get hover leave working as I couldn't figure out how to set the "ActiveForm" options for the form that just lost focus but the app. is now at least usable.

...now to extend what it can do biggrin.gif
User is offlineProfile CardPM
+Quote Post

fooboo

RE: How Do I Make My App. Border Disappear?

3 Jul, 2009 - 03:03 AM
Post #12

The king of Nynex
Group Icon

Joined: 28 Jul, 2006
Posts: 3,022



Thanked: 1 times
Dream Kudos: 150
My Contributions
I forgot to say thanks to all who have posted.

If anyone wants to add further/better methods I'd still appreciate the help. This method works but may not be the best way to go about it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:12PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month