1

我想完全摆脱“脱机工作”消息框。

在此处输入图像描述

为了给出一些上下文,这个消息框出现在运行本地webapp 的机器上。对网络的访问显然是不稳定的,所以一时的缺乏永远不会阻塞:它只会延迟一些后台通知。网页只需要显示本地资源。网址看起来像http://localhost:4444/*myApp*/...

本机运行XP pro,浏览器为IE8。

我尝试了以下解决方案但没有成功:

  1. 手动取消选中菜单选项文件/脱机工作是不够的。
  2. 设置注册表项HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\WebCheck\LoadSensLoadLCEauto然后noyes
  3. 我试图通过每隔 200 毫秒调用一次此方法以编程方式强制联机模式

    [DllImport("wininet.dll")]
    private extern static bool InternetSetOption(int hInternet,
    int dwOption, ref INTERNET_CONNECTED_INFO lpBuffer, int dwBufferLength);
    
    [StructLayout(LayoutKind.Sequential)]
    struct INTERNET_CONNECTED_INFO
    {
        public int dwConnectedState;
        public int dwFlags;
    };
    private static readonly int INTERNET_STATE_DISCONNECTED = 16;
    private static readonly int INTERNET_STATE_CONNECTED = 1;
    private static readonly int ISO_FORCE_DISCONNECTED = 1;
    private static readonly int INTERNET_OPTION_CONNECTED_STATE = 50;
    
    private static Timer aTimer;
    private bool offlineSelected = false;
    
    public void SetIEOfflineMode(bool offline)
    {
        INTERNET_CONNECTED_INFO ici = new INTERNET_CONNECTED_INFO();
    
        if (offline)
        {
            ici.dwConnectedState = INTERNET_STATE_DISCONNECTED;
            ici.dwFlags = ISO_FORCE_DISCONNECTED;
            Debug.WriteLine("switching to offline mode");
        }
        else
        {
            ici.dwConnectedState = INTERNET_STATE_CONNECTED;
            Debug.WriteLine("switching to online mode");
        }
    
        InternetSetOption(0, INTERNET_OPTION_CONNECTED_STATE, ref ici, Marshal.SizeOf(ici));
    }
    

最后一次尝试几乎成功了。“离线工作”从未保持选中状态,但有时(确实非常随机)会出现邪恶的消息框。问题在于,尽管它永远不会保持阻塞状态(工作模式切换到在线,因此页面可以正常工作),但它会打扰最终用户。

一句话:我们无法改变架构(本地 Web 应用程序),即使它看起来有点奇怪。

4

1 回答 1

1

Since anyone else couldn't help me, I finally found a solution. A bit dirty but working one. The trick is simulate a click on the "Try Again" button. I did this by using user32.dll functions. Here are the steps:

  1. You first find the parent window's handle by using the FindWindow function.
  2. You find the button with FindWindowExfrom its caption and its parent window's handle.
  3. You finally send the click with SendMessage

Here is the declaration of the required functions

// For Windows Mobile, replace user32.dll with coredll.dll
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

const uint WM_CLOSE = 0x10;
const uint BM_CLICK = 0x00F5;

And here is the method using them

private bool ClickButton(String window, String button)
{
    IntPtr errorPopUp;
    IntPtr buttonHandle;

    bool found = false;
    try
    {
        errorPopUp = FindWindow(null, window.Trim());
        found = errorPopUp.ToInt32() != 0;
        if (found)
        {
            found = false;
            buttonHandle = FindWindowEx(errorPopUp, IntPtr.Zero, null, button.Trim());
            found = buttonHandle.ToInt32() != 0;
            if (found)
            {
                SendMessage(buttonHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
                Trace.WriteLine("Clicked \"" + button + "\" on window named \"" + window + "\"");
            }
            else
            {
                Debug.WriteLine("Found Window \"" + window + "\" but not its button \"" + button + "\"");
            }
        }

    }
    catch (Exception ex)
    {
        Trace.TraceError(ex.ToString());
    }
    return found;
}

window is the title (="Work Offline") of the window and button the caption of the button (="&Try Again").

Note : Do not forget the ampersand ("&") preceding subtitled letters.

于 2013-06-07T13:20:04.590 回答