示例#1
0
        public PreviewImageDialogViewModel(Bitmap sourceBitmap)
        {
            this.sourceBitmap = sourceBitmap;
            sourceBitmapGdi   = sourceBitmap.GetHbitmapHandle();

            imageWidth    = 100;
            imageHeight   = 100;
            sourceRect    = new Int32Rect(0, 0, sourceBitmap.Width, sourceBitmap.Height / 6);
            destRect      = new Int32Rect(0, 0, (int)ImageWidth, (int)ImageHeight);
            sizingMargins = new Int32Rect(6, 5, 6, 5);
            drawOption    = 4;

            RenderImage();
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var isActivated = (bool)value;
            var img = isActivated ? Resources.Img_Deactivate_48 : Resources.Img_Activate_48;
            using (img)
            {
                using (var handle = new SafeBitmapHandle(img.GetHbitmap(), true))
                {
                    var result = Imaging.CreateBitmapSourceFromHBitmap(
                        handle.DangerousGetHandle(),
                        IntPtr.Zero,
                        Int32Rect.Empty,
                        BitmapSizeOptions.FromEmptyOptions());

                    return result;
                }
            }
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var isActivated = (bool)value;
            var img         = isActivated ? Resources.Img_Deactivate_48 : Resources.Img_Activate_48;

            using (img)
            {
                using (var handle = new SafeBitmapHandle(img.GetHbitmap(), true))
                {
                    var result = Imaging.CreateBitmapSourceFromHBitmap(
                        handle.DangerousGetHandle(),
                        IntPtr.Zero,
                        Int32Rect.Empty,
                        BitmapSizeOptions.FromEmptyOptions());

                    return(result);
                }
            }
        }
示例#4
0
        public DialogResult ShowDialog(IWin32Window owner)
        {
            if (String.IsNullOrEmpty(_targetName))
            {
                throw new ArgumentNullException("TargetName");
            }

            DialogResult        result;
            int                 passwordMaxLength = CREDUI_MAX_PASSWORD_LENGTH + 1;
            StringBuilder       username          = new StringBuilder(_userName, CREDUI_MAX_USERNAME_LENGTH);
            SafeBitmapHandle    banner            = _banner != null ? new SafeBitmapHandle(_banner.GetHbitmap(), true) : new SafeBitmapHandle(IntPtr.Zero, false);
            SafeUnmanagedBuffer password          = new SafeUnmanagedBuffer(passwordMaxLength * sizeof(char));

            for (int i = 0; i < sizeof(char); i++)
            {
                password[i] = 0;
            }

            CheckNotDisposed();

            var info = new NativeMethods.CREDUI_INFO()
            {
                hwndParent     = owner != null ? owner.Handle : IntPtr.Zero,
                hbmBanner      = banner.DangerousGetHandle(),
                pszCaptionText = _caption,
                pszMessageText = _message
            };

            info.cbSize = Marshal.SizeOf(info);

            try
            {
                var nativeResult = NativeMethods.CredUIPromptForCredentials(ref info, _targetName, IntPtr.Zero, 0,
                                                                            username, CRED_MAX_USERNAME_LENGTH,
                                                                            new HandleRef(password, password.DangerousGetHandle()), passwordMaxLength,
                                                                            ref _saveChecked, _options);

                /*IntPtr outBuffer;
                 * uint authPackage = 0;
                 * uint outBufferSize;
                 * info.hbmBanner = IntPtr.Zero;
                 * nativeResult = NativeMethods.CredUIPromptForWindowsCredentials(ref info, 0, ref authPackage, new HandleRef(null, IntPtr.Zero), 0, out outBuffer, out outBufferSize, ref _saveChecked,
                 *  0x1 | 0x200);
                 *
                 * int nameLen = 0, passLen = 0, dNameLength = 0;
                 * NativeMethods.CredUnPackAuthenticationBuffer(0, outBuffer, (int)outBufferSize, null, ref nameLen, null, ref dNameLength, null, ref passLen);
                 * StringBuilder sName = new StringBuilder(nameLen), sPass = new StringBuilder(passLen), dName = new StringBuilder(dNameLength);
                 * bool pos = NativeMethods.CredUnPackAuthenticationBuffer(0, outBuffer, (int)outBufferSize, sName, ref nameLen,
                 *  dName, ref dNameLength, sPass, ref passLen);
                 *
                 *
                 * var b = new SafeCoUnmanagedBuffer(outBuffer, checked((int)outBufferSize), true);
                 *
                 * string s = Marshal.PtrToStringUni(outBuffer);*/

                switch (nativeResult)
                {
                case NativeMethods.CredUIReturnCodes.NO_ERROR:
                    _userName = username.ToString();

                    Password.Clear();
                    byte[] unicodeBytes = new byte[sizeof(char)];
                    char   currentChar  = Char.MaxValue;
                    for (int i = 0; ;)
                    {
                        for (int j = 0; j < sizeof(char); j++)
                        {
                            unicodeBytes[j] = password[i];
                            password[i++]   = 0;
                        }

                        if ((currentChar = Encoding.Unicode.GetChars(unicodeBytes)[0]) != '\0')
                        {
                            Password.AppendChar(currentChar);
                        }
                        else
                        {
                            break;
                        }
                    }

                    result = DialogResult.OK;
                    break;

                case NativeMethods.CredUIReturnCodes.ERROR_CANCELLED:
                    result = DialogResult.Cancel;
                    break;

                default:
                    throw new Win32Exception((int)nativeResult);
                }
            }
            finally
            {
                banner.Dispose();
                password.Dispose();
            }

            return(result);
        }