Click here to Skip to main content
15,936,633 members
Home / Discussions / C#
   

C#

 
AnswerRe: substring from form1 to form2.... Pin
Jibesh14-Mar-13 17:35
professionalJibesh14-Mar-13 17:35 
GeneralRe: substring from form1 to form2.... Pin
Goaty6510914-Mar-13 18:57
Goaty6510914-Mar-13 18:57 
QuestionBlueTooth Bytes Disappearing Pin
C-P-User-314-Mar-13 13:03
C-P-User-314-Mar-13 13:03 
QuestionView in Nhibernate Pin
hadi_ganji14-Mar-13 6:06
hadi_ganji14-Mar-13 6:06 
AnswerRe: View in Nhibernate - Repost Pin
Richard MacCutchan14-Mar-13 6:24
mveRichard MacCutchan14-Mar-13 6:24 
QuestionUnable to set registry value Pin
sjs4u13-Mar-13 21:27
sjs4u13-Mar-13 21:27 
AnswerRe: Unable to set registry value Pin
dusty_dex14-Mar-13 5:07
dusty_dex14-Mar-13 5:07 
QuestionHow to use CreateTextServices in C# Pin
cpw999cn13-Mar-13 20:32
cpw999cn13-Mar-13 20:32 
I am trying to draw a TextBox in my DUI framework. At last, I find a way to create a windowless RichTextBox with a custom ITextHost interface.I've got a C++ implementation. But when I try to implement it from C#, I got a lot of exceptions.

C++ Code:
C++
class ITextHost : public IUnknown
{
public:

	//@cmember Get the DC for the host
	virtual HDC 		TxGetDC() = 0;

	//@cmember Release the DC gotten from the host
	virtual INT			TxReleaseDC(HDC hdc) = 0;
	
	//@cmember Show the scroll bar
	virtual BOOL 		TxShowScrollBar(INT fnBar, BOOL fShow) = 0;

	//@cmember Enable the scroll bar
	virtual BOOL 		TxEnableScrollBar (INT fuSBFlags, INT fuArrowflags) = 0;

	//@cmember Set the scroll range
	virtual BOOL 		TxSetScrollRange(
							INT fnBar,
							LONG nMinPos,
							INT nMaxPos,
							BOOL fRedraw) = 0;

	//@cmember Set the scroll position
	virtual BOOL 		TxSetScrollPos (INT fnBar, INT nPos, BOOL fRedraw) = 0;

	//@cmember InvalidateRect
	virtual void		TxInvalidateRect(LPCRECT prc, BOOL fMode) = 0;

	//@cmember Send a WM_PAINT to the window
	virtual void 		TxViewChange(BOOL fUpdate) = 0;
	
	//@cmember Create the caret
	virtual BOOL		TxCreateCaret(HBITMAP hbmp, INT xWidth, INT yHeight) = 0;

	//@cmember Show the caret
	virtual BOOL		TxShowCaret(BOOL fShow) = 0;

	//@cmember Set the caret position
	virtual BOOL		TxSetCaretPos(INT x, INT y) = 0;

	//@cmember Create a timer with the specified timeout
	virtual BOOL 		TxSetTimer(UINT idTimer, UINT uTimeout) = 0;

	//@cmember Destroy a timer
	virtual void 		TxKillTimer(UINT idTimer) = 0;

	//@cmember Scroll the content of the specified window's client area
	virtual void		TxScrollWindowEx (
							INT dx,
							INT dy,
							LPCRECT lprcScroll,
							LPCRECT lprcClip,
							HRGN hrgnUpdate,
							LPRECT lprcUpdate,
							UINT fuScroll) = 0;
	
	//@cmember Get mouse capture
	virtual void		TxSetCapture(BOOL fCapture) = 0;

	//@cmember Set the focus to the text window
	virtual void		TxSetFocus() = 0;

	//@cmember Establish a new cursor shape
	virtual void 	TxSetCursor(HCURSOR hcur, BOOL fText) = 0;

	//@cmember Converts screen coordinates of a specified point to the client coordinates
	virtual BOOL 		TxScreenToClient (LPPOINT lppt) = 0;

	//@cmember Converts the client coordinates of a specified point to screen coordinates
	virtual BOOL		TxClientToScreen (LPPOINT lppt) = 0;

	//@cmember Request host to activate text services
	virtual HRESULT		TxActivate( LONG * plOldState ) = 0;

	//@cmember Request host to deactivate text services
   	virtual HRESULT		TxDeactivate( LONG lNewState ) = 0;

	//@cmember Retrieves the coordinates of a window's client area
	virtual HRESULT		TxGetClientRect(LPRECT prc) = 0;

	//@cmember Get the view rectangle relative to the inset
	virtual HRESULT		TxGetViewInset(LPRECT prc) = 0;

	//@cmember Get the default character format for the text
	virtual HRESULT 	TxGetCharFormat(const CHARFORMATW **ppCF ) = 0;

	//@cmember Get the default paragraph format for the text
	virtual HRESULT		TxGetParaFormat(const PARAFORMAT **ppPF) = 0;

	//@cmember Get the background color for the window
	virtual COLORREF	TxGetSysColor(int nIndex) = 0;

	//@cmember Get the background (either opaque or transparent)
	virtual HRESULT		TxGetBackStyle(TXTBACKSTYLE *pstyle) = 0;

	//@cmember Get the maximum length for the text
	virtual HRESULT		TxGetMaxLength(DWORD *plength) = 0;

	//@cmember Get the bits representing requested scroll bars for the window
	virtual HRESULT		TxGetScrollBars(DWORD *pdwScrollBar) = 0;

	//@cmember Get the character to display for password input
	virtual HRESULT		TxGetPasswordChar(TCHAR *pch) = 0;

	//@cmember Get the accelerator character
	virtual HRESULT		TxGetAcceleratorPos(LONG *pcp) = 0;

	//@cmember Get the native size
    virtual HRESULT		TxGetExtent(LPSIZEL lpExtent) = 0;

	//@cmember Notify host that default character format has changed
	virtual HRESULT 	OnTxCharFormatChange (const CHARFORMATW * pcf) = 0;

	//@cmember Notify host that default paragraph format has changed
	virtual HRESULT		OnTxParaFormatChange (const PARAFORMAT * ppf) = 0;

	//@cmember Bulk access to bit properties
	virtual HRESULT		TxGetPropertyBits(DWORD dwMask, DWORD *pdwBits) = 0;

	//@cmember Notify host of events
	virtual HRESULT		TxNotify(DWORD iNotify, void *pv) = 0;

	// Far East Methods for getting the Input Context
//#ifdef WIN95_IME
	virtual HIMC		TxImmGetContext() = 0;
	virtual void		TxImmReleaseContext( HIMC himc ) = 0;
//#endif

	//@cmember Returns HIMETRIC size of the control bar.
	virtual HRESULT		TxGetSelectionBarWidth (LONG *lSelBarWidth) = 0;

};


My C# implementation:
C#
[ComImport, Guid("C5BDD8D0-D26E-11CE-A89E-00AA006CADC5"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface ITextHost
    {
        //@cmember Get the DC for the host
        IntPtr TxGetDC();

        //@cmember Release the DC gotten from the host
        int TxReleaseDC(IntPtr hdc);

        //@cmember Show the scroll bar
        BOOL TxShowScrollBar(int fnBar, BOOL fShow);

        //@cmember Enable the scroll bar
        BOOL TxEnableScrollBar(int fuSBFlags, int fuArrowflags);

        //@cmember Set the scroll range
        BOOL TxSetScrollRange(int fnBar, int nMinPos, int nMaxPos, BOOL fRedraw);

        //@cmember Set the scroll position
        BOOL TxSetScrollPos(int fnBar, int nPos, BOOL fRedraw);

        //@cmember InvalidateRect
        void TxInvalidateRect(RECT prc, BOOL fMode);

        //@cmember Send a WM_PAINT to the window
        void TxViewChange(BOOL fUpdate);

        //@cmember Create the caret
        BOOL TxCreateCaret(IntPtr hbmp, int xWidth, int yHeight);

        //@cmember Show the caret
        BOOL TxShowCaret(BOOL fShow);

        //@cmember Set the caret position
        BOOL TxSetCaretPos(int x, int y);

        //@cmember Create a timer with the specified timeout
        BOOL TxSetTimer(uint idTimer, uint uTimeout);

        //@cmember Destroy a timer
        void TxKillTimer(uint idTimer);

        //@cmember Scroll the content of the specified window's client area
        void TxScrollWindowEx(
            int dx,
            int dy,
            RECT lprcScroll,
            RECT lprcClip,
            IntPtr hrgnUpdate,
            RECT lprcUpdate,
            uint fuScroll);

        //@cmember Get mouse capture
        void TxSetCapture(BOOL fCapture);

        //@cmember Set the focus to the text window
        void TxSetFocus();

        //@cmember Establish a new cursor shape
        void TxSetCursor(IntPtr hcur, BOOL fText);

        //@cmember Converts screen coordinates of a specified point to the client coordinates
        BOOL TxScreenToClient(POINT lppt);

        //@cmember Converts the client coordinates of a specified point to screen coordinates
        BOOL TxClientToScreen(POINT lppt);

        //@cmember Request host to activate text services
        HRESULT TxActivate(int plOldState);

        //@cmember Request host to deactivate text services
        HRESULT TxDeactivate(int lNewState);

        //@cmember Retrieves the coordinates of a window's client area
        HRESULT TxGetClientRect(RECT prc);

        //@cmember Get the view rectangle relative to the inset
        HRESULT TxGetViewInset(RECT prc);

        //@cmember Get the default character format for the text
        HRESULT TxGetCharFormat(ref CHARFORMAT ppCF);

        //@cmember Get the default paragraph format for the text
        HRESULT TxGetParaFormat(ref PARAFORMAT ppPF);

        //@cmember Get the background color for the window
        COLORREF TxGetSysColor(int nIndex);

        //@cmember Get the background (either opaque or transparent)
        HRESULT TxGetBackStyle(out TXTBACKSTYLE pstyle);

        //@cmember Get the maximum length for the text
        HRESULT TxGetMaxLength(out int pLength);

        //@cmember Get the bits representing requested scroll bars for the window
        HRESULT TxGetScrollBars(out int pdwScrollBar);

        //@cmember Get the character to display for password input
        HRESULT TxGetPasswordChar(out char pch);

        //@cmember Get the accelerator character
        HRESULT TxGetAcceleratorPos(out int pcp);

        //@cmember Get the native size
        HRESULT TxGetExtent(SIZE lpExtent);

        //@cmember Notify host that default character format has changed
        HRESULT OnTxCharFormatChange(ref CHARFORMAT pcf);

        //@cmember Notify host that default paragraph format has changed
        HRESULT OnTxParaFormatChange(ref PARAFORMAT ppf);

        //@cmember Bulk access to bit properties
        HRESULT TxGetPropertyBits(int dwMask, ref int pdwBits);

        //@cmember Notify host of events
        HRESULT TxNotify(int iNotify, IntPtr pv);

        IntPtr TxImmGetContext();
        void TxImmReleaseContext(IntPtr himc);

        //@cmember Returns HIMETRIC size of the control bar.
        HRESULT TxGetSelectionBarWidth(out int lSelBarWidth);
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct CHARFORMAT
    {
        public int cbSize;
        public int dwMask;
        public int dwEffects;
        public int yHeight;
        public int yOffset;
        public int crTextColor;
        public byte bCharSet;
        public byte bPitchAndFamily;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string szFaceName;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct PARAFORMAT
    {
        public int cbSize;
        public int dwMask;
        public short wNumbering;
        public short wReserved;
        public int dxStartIndent;
        public int dxRightIndent;
        public int dxOffset;
        public short wAlignment;
        public short cTabCount;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public int[] rgxTabs; // MAX_TAB_STOPS
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public Int32 x;
        public Int32 y;

        public POINT(Int32 x, Int32 y) { this.x = x; this.y = y; }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SIZE
    {
        public Int32 cx;
        public Int32 cy;

        public SIZE(Int32 cx, Int32 cy) { this.cx = cx; this.cy = cy; }
    }

    public enum BOOL
    {
        False = 0,
        True
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct COLORREF
    {
        public uint ColorDWORD;

        public COLORREF(System.Drawing.Color color)
        {
            ColorDWORD = (uint)color.R + (((uint)color.G) << 8) + (((uint)color.B) << 16);
        }

        public System.Drawing.Color GetColor()
        {
            return System.Drawing.Color.FromArgb((int)(0x000000FFU & ColorDWORD),
           (int)(0x0000FF00U & ColorDWORD) >> 8, (int)(0x00FF0000U & ColorDWORD) >> 16);
        }

        public void SetColor(System.Drawing.Color color)
        {
            ColorDWORD = (uint)color.R + (((uint)color.G) << 8) + (((uint)color.B) << 16);
        }
    }

    public enum TXTBACKSTYLE
    {
        TXTBACK_TRANSPARENT = 0,		//@emem	background should show through
        TXTBACK_OPAQUE,					//@emem	erase background
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;

        public RECT(int left, int top, int right, int bottom)
        {
            this.Left = left;
            this.Top = top;
            this.Right = right;
            this.Bottom = bottom;
        }

        public Rectangle Rect
        {
            get
            {
                return new Rectangle(
                    this.Left,
                    this.Top,
                    this.Right - this.Left,
                    this.Bottom - this.Top);
            }
        }

        public static RECT FromXYWH(int x, int y, int width, int height)
        {
            return new RECT(x,
                            y,
                            x + width,
                            y + height);
        }

        public static RECT FromRectangle(Rectangle rect)
        {
            return new RECT(rect.Left,
                                rect.Top,
                                rect.Right,
                                rect.Bottom);
        }
    }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "HRESULT"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1028:EnumStorageShouldBeInt32",
        Justification = "The base type for all of these value is uint")]
    public enum HRESULT : uint
    {
        /// <summary>
        /// S_FALSE
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "FALSE")]
        S_FALSE = 0x0001,

        /// <summary>
        /// S_OK
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "OK")]
        S_OK = 0x0000,

        /// <summary>
        /// E_INVALIDARG
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "INVALIDARG")]
        E_INVALIDARG = 0x80070057,

        /// <summary>
        /// E_OUTOFMEMORY
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "OUTOFMEMORY")]
        E_OUTOFMEMORY = 0x8007000E,

        /// <summary>
        /// E_NOINTERFACE
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "NOINTERFACE")]
        E_NOINTERFACE = 0x80004002,

        /// <summary>
        /// E_FAIL
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "FAIL")]
        E_FAIL = 0x80004005,

        /// <summary>
        /// E_ELEMENTNOTFOUND
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "ELEMENTNOTFOUND")]
        E_ELEMENTNOTFOUND = 0x80070490,

        /// <summary>
        /// TYPE_E_ELEMENTNOTFOUND
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "TYPE")]
        TYPE_E_ELEMENTNOTFOUND = 0x8002802B,


        /// <summary>
        /// NO_OBJECT
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "NO_OBJECT")]
        NO_OBJECT = 0x800401E5,

        /// <summary>
        /// Win32 Error code: ERROR_CANCELLED
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "ERROR")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "CANCELLED")]
        ERROR_CANCELLED = 1223,

        /// <summary>
        /// ERROR_CANCELLED
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "ERROR")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "CANCELLED")]
        E_ERROR_CANCELLED = 0x800704C7,

        /// <summary>
        /// The requested resource is in use
        /// </summary>
        RESOURCE_IN_USE = 0x800700AA,
    }

C#
[DllImport("riched20.dll")]
public static extern int CreateTextServices(
       [MarshalAs(UnmanagedType.IUnknown)] object punkOuter,
       ITextHost pITextHost,
       [Out, MarshalAs(UnmanagedType.IUnknown)] out object ppUnk);

C#
ITextHost host = new CTextHost(this);
object ppUnk;
var r = CreateTextServices(null, host, out ppUnk);


I recived ExecutionEngineException and AccessViolationException again and again. Is there any problem with it?
Thanks in advance!
AnswerRe: How to use CreateTextServices in C# Pin
Bernhard Hiller13-Mar-13 22:01
Bernhard Hiller13-Mar-13 22:01 
GeneralRe: How to use CreateTextServices in C# Pin
cpw999cn13-Mar-13 22:40
cpw999cn13-Mar-13 22:40 
QuestionC# Receive from TCP send to COM2 Pin
dockwomblejr13-Mar-13 11:55
dockwomblejr13-Mar-13 11:55 
AnswerRe: C# Receive from TCP send to COM2 Pin
jschell14-Mar-13 7:57
jschell14-Mar-13 7:57 
GeneralRe: C# Receive from TCP send to COM2 Pin
dockwomblejr14-Mar-13 13:05
dockwomblejr14-Mar-13 13:05 
GeneralRe: C# Receive from TCP send to COM2 Pin
micke.andersson15-Mar-13 6:03
micke.andersson15-Mar-13 6:03 
Questionhow can i index files in c# Pin
Member 888804413-Mar-13 10:26
Member 888804413-Mar-13 10:26 
AnswerRe: how can i index files in c# Pin
R. Giskard Reventlov13-Mar-13 10:40
R. Giskard Reventlov13-Mar-13 10:40 
QuestionSplit with Regex Pin
Revolty13-Mar-13 8:21
Revolty13-Mar-13 8:21 
AnswerRe: Split with Regex Pin
Dave Kreskowiak13-Mar-13 9:30
mveDave Kreskowiak13-Mar-13 9:30 
GeneralRe: Split with Regex Pin
SledgeHammer0113-Mar-13 9:33
SledgeHammer0113-Mar-13 9:33 
GeneralRe: Split with Regex Pin
Dave Kreskowiak13-Mar-13 9:46
mveDave Kreskowiak13-Mar-13 9:46 
AnswerRe: Split with Regex Pin
Revolty13-Mar-13 10:29
Revolty13-Mar-13 10:29 
QuestionProblem with override the operators Pin
VendorX13-Mar-13 6:35
VendorX13-Mar-13 6:35 
AnswerRe: Problem with override the operators Pin
Eddy Vluggen13-Mar-13 6:37
professionalEddy Vluggen13-Mar-13 6:37 
GeneralRe: Problem with override the operators Pin
VendorX13-Mar-13 6:47
VendorX13-Mar-13 6:47 
GeneralRe: Problem with override the operators Pin
Eddy Vluggen13-Mar-13 7:20
professionalEddy Vluggen13-Mar-13 7:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.