stackoverflow源码搜集整理

I have developed winform application and I have set formborderstyle=none. Thatz why when I am running application I can’t minimize it through taskbar. Does any body knows solution for this?
I tried following code.. adding it in my form.
const int WS_CLIPCHILDREN = 0x2000000;
const int WS_MINIMIZEBOX = 0x20000;
const int WS_MAXIMIZEBOX = 0x10000;
const int WS_SYSMENU = 0x80000;
const int CS_DBLCLKS = 0x8;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style = WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_SYSMENU;
cp.ClassStyle = CS_DBLCLKS;
return cp;
}
}
I am now able to minimize the application from taskbar. But the problem is it is creating two intances of my application one which I need and the other which is unneccessary.

Does any body knows solution for this.. or does anyone has some other solution which works ?

Viewed 5k times

5

3
I have developed winform application and I have set formborderstyle=none. Thatz why when I am running application I can’t minimize it through taskbar. Does any body knows solution for this?

I tried following code.. adding it in my form.

const int WS_CLIPCHILDREN = 0x2000000;
const int WS_MINIMIZEBOX = 0x20000;
const int WS_MAXIMIZEBOX = 0x10000;
const int WS_SYSMENU = 0x80000;
const int CS_DBLCLKS = 0x8;
protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.Style = WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_SYSMENU;
        cp.ClassStyle = CS_DBLCLKS;
        return cp;
    }
}

I am now able to minimize the application from taskbar. But the problem is it is creating two intances of my application one which I need and the other which is unneccessary.

Does any body knows solution for this.. or does anyone has some other solution which works ?

c# winforms
shareimprove this questionfollow
edited Mar 3 ’11 at 12:21

Sangram Nandkhile
12.9k1515 gold badges7272 silver badges107107 bronze badges
asked Mar 3 ’11 at 12:17

Sachin Patil
14111 gold badge33 silver badges1010 bronze badges
1
Every style flag that you’ve overridden CreateParams to include is already exposed by the .NET Framework. There’s absolutely no reason you should have to use code like that in the first place. I also don’t understand what you mean by “minimize form from taskbar”. When the application is open, click on its icon in the taskbar, and it will minimize. Quite simple, no code required. Windows natively supports this. Beyond that, I can guarantee that the code you’ve shown will not create two instances of your application. Something else is wrong, but you haven’t given us enough information. – Cody Gray? Mar 3 ’11 at 12:22
2
The thing is when you set formborderstyle=none then at runtime you cannnot minimize application from taskbar … try it yourself.. – Sachin Patil Mar 3 ’11 at 12:24
and that two instances problem is only creeted because of above code that I can guarantee because when I am running application on commenting above code it’s working fine. guys plz help.. – Sachin Patil Mar 3 ’11 at 12:26
Yeah, you’re right. I missed the part where you said you were disabling the form’s border. Seemed illogical that you’d want to minimize a form that didn’t have a border. Doesn’t change the fact that I can’t repro the behavior you describe. Forms don’t just decide to instantiate another instance of themselves when the user clicks on their icon in the taskbar. You have some code that’s responsible for doing that. Take that out and your problem is solved. – Cody Gray? Mar 3 ’11 at 12:58
add a comment
1 Answer
Active
Oldest
Votes

15

A borderless form should always be one that the user is not expected to minimize. The discoverability principle starts to apply here: most users don’t know that you can minimize a window by clicking on its taskbar icon. They’re going to expect to be able to do it by clicking the – button next to the big red x.

The right solution is to choose a different border style for your form, one that includes the title bar and the minimize box. Windows will automatically behave as expected. Things are much easier when you follow the standard conventions of your platform, not only for you as a programmer, but for your users. It also fixes that nasty flickering effect when your form is created or restored where I can see the standard caption bar for a few seconds.

Of course, you’ll inevitably want to do this anyway, so despite my better judgement, I’ll try to provide a solution. The first problem is I can’t reproduce the behavior you describe (Windows Server 2008 R2, .NET 4.0). Adding exactly the code shown to a new WinForms project, and setting the form’s FormBorderStyle property to “None”, there’s no way I can get two windows to show up. Clicking on the taskbar icon causes the form to minimize, clicking it again restores it.

But there is a way to simplify your code. And you should probably be OR-ing the style flags that you’re adding with the existing style flags, rather than replacing the existing flags. Replace your code with this:

const int WS_MINIMIZEBOX = 0x20000;
const int CS_DBLCLKS = 0x8;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= WS_MINIMIZEBOX;
cp.ClassStyle |= CS_DBLCLKS;
return cp;
}
}
If that doesn’t fix your problem (and I’m skeptical that it will), then as I suspected, there’s something else wrong in your code that you haven’t shown us. Just because you can comment out a few lines of code and your program works as expected doesn’t necessarily imply that the problem lies in those lines of code. They can be perfectly correct, but interfering with a hack you’ve used elsewhere.

I am using this code to make my form (FormBorderStyle=none) with rounded edges:

[DllImport(“Gdi32.dll”, EntryPoint = “CreateRoundRectRgn”)]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);

public Form1()
{
InitializeComponent();
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
And this to set a custom border on the Paint event:

ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid);

But see this screenshot.

The inside form rectangle doesn’t have rounded edges.
How can I make the blue inside form rectangle to have rounded edge too so it wont look like the screenshot?
Note you’re leaking the handle returned by CreateRoundRectRgn(), you should free it with DeleteObject() after it is used.
The Region.FromHrgn() copies the definition, so it won’t free the handle.

[DllImport(“Gdi32.dll”, EntryPoint = “DeleteObject”)]
public static extern bool DeleteObject(IntPtr hObject);

public Form1()
{
InitializeComponent();
IntPtr handle = CreateRoundRectRgn(0, 0, Width, Height, 20, 20);
if (handle == IntPtr.Zero)
; // error with CreateRoundRectRgn
Region = System.Drawing.Region.FromHrgn(handle);
DeleteObject(handle);
}
(would add as comment but reputation is ded)

I am working with the Webbrowser control on a windows.form application written in C#. I would like to write a method for deleting the cookies from the Webbrowers control after it visits a certain site. Unfortunately, I don’t know how to do that exactly and haven’t found a lot of help on the internet.
If anyone has experience actually doing this, not just hypothetical because it might be trickier than it seems, I don’t know.

int count = webBrowser2.Document.Cookie.Length;
webBrowser2.Document.Cookie.Remove(0,count);
I would just assume something like the above code would work but I guess it won’t. Can anyone shed some light on this whole cookie thing?

If you have JavaScript enabled you can just use this code snippet to clear to clear the cookies for the site the webbrowser is currently on.

webBrowser.Navigate(“javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split(‘; ‘);for(e=0;e<a.length&&a[e];e++){f++;for(b=’.’+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,”)){for(c=location.pathname;c;c=c.replace(/.$/,”)){document.cookie=(a[e]+’; domain=’+b+’; path=’+c+’; expires=’+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())”)
It’s derived from this bookmarklet for clearing cookies.

I modified the solution from here: //mdb-blog.blogspot.ru/2013/02/c-winforms-webbrowser-clear-all-cookies.html
Actually, you don’t need an unsafe code. Here is the helper class that works for me:

public static class WinInetHelper
{
public static bool SupressCookiePersist()
{
// 3 = INTERNET_SUPPRESS_COOKIE_PERSIST
// 81 = INTERNET_OPTION_SUPPRESS_BEHAVIOR
return SetOption(81, 3);
}

public static bool EndBrowserSession()
{
    // 42 = INTERNET_OPTION_END_BROWSER_SESSION 
    return SetOption(42, null);
}
static bool SetOption(int settingCode, int? option)
{
    IntPtr optionPtr = IntPtr.Zero;
    int size = 0;
    if (option.HasValue)
    {
        size = sizeof (int);
        optionPtr = Marshal.AllocCoTaskMem(size);
        Marshal.WriteInt32(optionPtr, option.Value);
    }

    bool success = InternetSetOption(0, settingCode, optionPtr, size);

    if (optionPtr != IntPtr.Zero) Marshal.Release(optionPtr);
    return success;
}
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool InternetSetOption(

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool InternetSetOption(
    int hInternet,
    int dwOption,
    IntPtr lpBuffer,
    int dwBufferLength
);

}

I found a solution, for deleting all cookies. the example found on the url, deletes the cookies on application (process) startup.

//mdb-blog.blogspot.com/2013/02/c-winforms-webbrowser-clear-all-cookies.html
The solution is using InternetSetOption Function to announce the WEBBROWSER to clear all its content.

int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
int* optionPtr = &option;

bool success = InternetSetOption(0, 81/INTERNET_OPTION_SUPPRESS_BEHAVIOR/, new IntPtr(optionPtr), sizeof(int));
if (!success)
{
MessageBox.Show(“Something went wrong !>?”);
}

Viewed 61k times
23
15
I am working with the Webbrowser control on a windows.form application written in C#. I would like to write a method for deleting the cookies from the Webbrowers control after it visits a certain site. Unfortunately, I don’t know how to do that exactly and haven’t found a lot of help on the internet.

If anyone has experience actually doing this, not just hypothetical because it might be trickier than it seems, I don’t know.

int count = webBrowser2.Document.Cookie.Length;
webBrowser2.Document.Cookie.Remove(0,count);
I would just assume something like the above code would work but I guess it won’t. Can anyone shed some light on this whole cookie thing?

c# cookies webbrowser-control
shareimprove this questionfollow
edited Aug 11 ’14 at 15:53

Sam
6,79788 gold badges4141 silver badges6262 bronze badges
asked May 26 ’09 at 21:00

Proximo
3,85188 gold badges3737 silver badges5252 bronze badges
I’m curious. Would setting the object “webBrowser2” to a new instance of the WebBrowser control reset the cookies? – gehsekky May 26 ’09 at 21:13
No, you physically have to delete them. It’s just a matter of knowing the correct directory to look and catching access denied exceptions for files in use – Proximo Jun 26 ’09 at 0:29
1
I know what a cookie is. I need to delete a specific cookie. – user Oct 23 ’09 at 7:29
The HtmlDocument.Cookie documentation says that you can only set a single cookie at a time. – Jason Harrison Apr 13 ’16 at 18:49
add a comment
10 Answers
Active
Oldest
Votes

25

If you have JavaScript enabled you can just use this code snippet to clear to clear the cookies for the site the webbrowser is currently on.

webBrowser.Navigate(“javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split(‘; ‘);for(e=0;e<a.length&&a[e];e++){f++;for(b=’.’+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,”)){for(c=location.pathname;c;c=c.replace(/.$/,”)){document.cookie=(a[e]+’; domain=’+b+’; path=’+c+’; expires=’+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())”)
It’s derived from this bookmarklet for clearing cookies.

shareimprove this answerfollow
edited Nov 11 ’09 at 15:17

Peter Mortensen
25.6k2121 gold badges9090 silver badges118118 bronze badges
answered Aug 5 ’09 at 16:24

Jordan Milne
43811 gold badge66 silver badges1010 bronze badges
2
Just as a side note, if you need to be able to clear cookies for all sites, you’re probably better off using something like the axWebBrowser COM object. .NET’s built in WebBrowser control is pretty locked down. I only used this when switching to axWebBrowser wasn’t an option and the browser only needed to be able to browse one site. – Jordan Milne Aug 10 ’09 at 18:39
thank you sir .. i was looking for a code like this for ages .. thank thank – Saeed A Suleiman Jun 19 ’15 at 14:31
4
Note: HttpOnly cookies are not available to javascript. – Jason Harrison Apr 13 ’16 at 18:49
add a comment

13

I modified the solution from here: //mdb-blog.blogspot.ru/2013/02/c-winforms-webbrowser-clear-all-cookies.html

Actually, you don’t need an unsafe code. Here is the helper class that works for me:

public static class WinInetHelper
{
public static bool SupressCookiePersist()
{
// 3 = INTERNET_SUPPRESS_COOKIE_PERSIST
// 81 = INTERNET_OPTION_SUPPRESS_BEHAVIOR
return SetOption(81, 3);
}

public static bool EndBrowserSession()
{
    // 42 = INTERNET_OPTION_END_BROWSER_SESSION 
    return SetOption(42, null);
}
Tags: