Example #1
0
        public static void StringToHGlobalAuto()
        {
            String s = null;

            // passing null string should return 0
            Assert.Equal(0, (long)Marshal.StringToHGlobalAuto(s));

            s = "Hello World";
            IntPtr ptr = Marshal.StringToHGlobalAuto(s);

            // make sure the native memory is correctly laid out
            for (int i = 0; i < s.Length; i++)
            {
                char c = (char)Marshal.ReadInt16(IntPtr.Add(ptr, i << 1));
                Assert.Equal(s[i], c);
            }

            // make sure if we convert back to string we get the same value
            String s2 = Marshal.PtrToStringAuto(ptr);

            Assert.Equal(s, s2);

            // free the native memory
            Marshal.FreeCoTaskMem(ptr);
        }
Example #2
0
        /// <include file='doc\ToolBarButton.uex' path='docs/doc[@for="ToolBarButton.GetTBBUTTONINFO"]/*' />
        /// <devdoc>
        ///     Returns a TBBUTTONINFO object that represents this ToolBarButton.
        /// </devdoc>
        internal NativeMethods.TBBUTTONINFO GetTBBUTTONINFO(bool updateText, int newCommandId) {

            NativeMethods.TBBUTTONINFO button = new NativeMethods.TBBUTTONINFO();
            button.cbSize = Marshal.SizeOf(typeof(NativeMethods.TBBUTTONINFO));
            button.dwMask = NativeMethods.TBIF_IMAGE
                            | NativeMethods.TBIF_STATE | NativeMethods.TBIF_STYLE;

            // Comctl on Win98 interprets null strings as empty strings, which forces
            // the button to leave space for text.  The only workaround is to avoid having comctl 
            // update the text.
            if (updateText) {
                button.dwMask |= NativeMethods.TBIF_TEXT;
            }

            button.iImage = ImageIndexer.ActualIndex;

            if (newCommandId != commandId) {
                commandId = newCommandId;
                button.idCommand = newCommandId;
                button.dwMask |= NativeMethods.TBIF_COMMAND;
            }

            // set up the state of the button
            //
            button.fsState = 0;
            if (enabled) button.fsState |= NativeMethods.TBSTATE_ENABLED;
            if (partialPush && style == ToolBarButtonStyle.ToggleButton) button.fsState |= NativeMethods.TBSTATE_INDETERMINATE;
            if (pushed) button.fsState |= NativeMethods.TBSTATE_CHECKED;
            if (!visible) button.fsState |= NativeMethods.TBSTATE_HIDDEN;

            // set the button style
            //
            switch (style) {
                case ToolBarButtonStyle.PushButton:
                    button.fsStyle = NativeMethods.TBSTYLE_BUTTON;
                    break;
                case ToolBarButtonStyle.ToggleButton:
                    button.fsStyle = NativeMethods.TBSTYLE_CHECK;
                    break;
                case ToolBarButtonStyle.Separator:
                    button.fsStyle = NativeMethods.TBSTYLE_SEP;
                    break;
            }


            if (text == null) {
                button.pszText = Marshal.StringToHGlobalAuto("\0\0");
            }
            else {
                string textValue = this.text;
                PrefixAmpersands(ref textValue);
                button.pszText = Marshal.StringToHGlobalAuto(textValue);
            }

            return button;
        }
Example #3
0
        /// <summary>
        ///  Returns a TBBUTTONINFOW object that represents this ToolBarButton.
        /// </summary>
        internal ComCtl32.TBBUTTONINFOW GetTBBUTTONINFO(bool updateText, int newCommandId)
        {
            var button = new ComCtl32.TBBUTTONINFOW
            {
                cbSize = (uint)Marshal.SizeOf <ComCtl32.TBBUTTONINFOW>(),
                dwMask = ComCtl32.TBIF.IMAGE | ComCtl32.TBIF.STATE | ComCtl32.TBIF.STYLE
            };

            // Older platforms interpret null strings as empty, which forces the button to
            // leave space for text.
            // The only workaround is to avoid having comctl update the text.
            if (updateText)
            {
                button.dwMask |= ComCtl32.TBIF.TEXT;
            }

            button.iImage = ImageIndexer.ActualIndex;

            if (newCommandId != commandId)
            {
                commandId        = newCommandId;
                button.idCommand = newCommandId;
                button.dwMask   |= ComCtl32.TBIF.COMMAND;
            }

            // set up the state of the button
            button.fsState = 0;
            if (enabled)
            {
                button.fsState |= ComCtl32.TBSTATE.ENABLED;
            }

            if (partialPush && style == ToolBarButtonStyle.ToggleButton)
            {
                button.fsState |= ComCtl32.TBSTATE.INDETERMINATE;
            }

            if (pushed)
            {
                button.fsState |= ComCtl32.TBSTATE.CHECKED;
            }

            if (!visible)
            {
                button.fsState |= ComCtl32.TBSTATE.HIDDEN;
            }

            // set the button style
            switch (style)
            {
            case ToolBarButtonStyle.PushButton:
                button.fsStyle = (byte)ComCtl32.TBSTYLE.BUTTON;
                break;

            case ToolBarButtonStyle.ToggleButton:
                button.fsStyle = (byte)ComCtl32.TBSTYLE.CHECK;
                break;

            case ToolBarButtonStyle.Separator:
                button.fsStyle = (byte)ComCtl32.TBSTYLE.SEP;
                break;
            }

            if (text == null)
            {
                button.pszText = Marshal.StringToHGlobalAuto("\0\0");
            }
            else
            {
                string textValue = text;
                PrefixAmpersands(ref textValue);
                button.pszText = Marshal.StringToHGlobalAuto(textValue);
            }

            return(button);
        }