Tag: c#

  • C# – 本機報表列印後程式失焦的解決方案(讓當前程式獲取焦點)

    最近在開發本機report列印程式時,碰到列印後程式就會失焦的狀況,試了很多參數還是無法排除現象。目前先用重新取得焦點的方式處理,程式段如下:

    using System.Runtime.InteropServices;

    public static class NativeMethods
    {
    [DllImport(“user32.dll”)]
    internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);

    [DllImport(“user32.dll”)]
    internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    public static void FocusApplication()
    {
    Process currentProcess = Process.GetCurrentProcess();
    IntPtr hWnd = currentProcess.MainWindowHandle;
    if (hWnd != IntPtr.Zero)
    {
    SetForegroundWindow(hWnd);
    ShowWindow(hWnd, (int)User32.SW_MAXIMIZE);
    }
    }

    private enum User32
    {
    SW_FORCEMINIMIZE = 11,
    SW_HIDE = 0,
    SW_MAXIMIZE = 3,
    SW_MINIMIZE = 6,
    SW_RESTORE = 9,
    SW_SHOW = 5,
    SW_SHOWDEFAULT = 10,
    SW_SHOWMAXIMIZED = 3,
    SW_SHOWMINIMIZED = 2,
    SW_SHOWMINNOACTIVE = 7,
    SW_SHOWNA = 8,
    SW_SHOWNOACTIVATE = 4,
    SW_SHOWNORMAL = 1,
    }
    }

  • .NET Core Web API – 讀取appsettings.json參數

    不同於傳統僅依賴於IIS的ASP.NET,跨平台的.NET Core程式參數是放在appsettings.json中,且取用方式也略有不同。

    (more…)

  • .Net – 解決user.config莫名奇妙消失的問題

    user.config消失的原因不明,但是這問題會造成.net程式無法存取User Property參數並發生錯誤。

    (more…)

  • C#-byte[] to BSTR

    使用 UTF7編碼

    var bytes = new byte[] {0xB1, 0xA6};

    var bstr = Encoding.UTF7.GetString(bytes);