In the process of writing a desktop software, use the WebBrowser to implement the interface. There is a requirement to prohibit WebBrowser from being refreshed via the F5 button. In the spirit of doing while learning principle, this dish checked Baidu, originally so simple, code as follows:
This false;
Haha, success. Continue to complete the next task, write and write, found the wrong, very wrong. Just that code, not only the F5 this shortcut key disabled, and even CTRL + C, CTRL + V these shortcuts are prohibited. This is how it should be good. Then the first reflection of the dish: keyboard hooks. But then I thought, using C # also use hooks, obviously posture is not right ah. Well, not until the last resort, never use the hook treasure. Next, the dish has gone further and farther on the road of no return.
All over the street, string through the alley, over the Baidu, searched all over the Google, and finally even MSDN and open stack have been checked over. See the answer of the netizens: it is difficult to do, almost impossible. (See the Pit Daddy teammates are not confined to our side). Just as the dish to pick up the hook when the treasure, holding the last trace of the hope of struggle, turned over a WebBrowser function list, turned over, immediately before a bright, Qiang Qiang Qiang Qiang:
Public classwebbroswer:system.windows.forms.webbrowser{ Publicwebbroswer ():Base() { } Public Override BOOLpreProcessMessage (refMessage msg) { Switch(Msg. MSG) { CaseApicaller.wm_keydown:intVK =Ptr.ptrtoint (Msg. WParam); if(VK = = the)return false;//Hook off F5 Break; } return Base. preProcessMessage (refmsg); }}
The main idea of this DAFA is to overload WebBrowser and dispose of F5 keyboard messages. (The Apicaller class in the code above is a custom class, just Judge Wm_keydown)
In fact, before this, the dish in WndProc and defwndproc between the two functions toss for a long time. Has not turned the corner, WebBrowser keyboard message wm_keydown, WM_KEYUP will not pass these two functions at all. Because before these two functions, they are distributed to the internal child controls. Fortunately, in time to wake up, this is completely eating no cultural loss, hope these experiences summary, can bring help to everyone. If this knowledge point is useful to you, please continue to propagate.
C # WebBrowser prohibit F5 Refresh