|
Try the MouseDown event of your PictureBox . The event handler receives an argument of type MouseEventArgs .
|
|
|
|
|
Thanks for the reply... I have thought of that, however I don't want a mouse down to change anything... if the user clicks the mouse down on the pic and leaves the bounds of the pic it will still change the image. I would like the image changed only if there is a click. It seems I may have to write a MouseDown MouseUp combination to handle this... Can anyone think of an easier way to deal with this?
|
|
|
|
|
You can use the Control class static properties like MouseButtons, MousePosition and the instance methods PointToClient and PointToScreen.
For additional information refer the msdn documentation.
|
|
|
|
|
I used a MouseDown in combination with a MouseUp and tested to make sure that the mouse position was still inside that bounds of the pictureBox. If anyone else was having trouble with this here is the code:
<br />
private void NowPBox_MouseDown(object sender, MouseEventArgs e)<br />
{<br />
if(e.Button == MouseButtons.Left)<br />
leftClick = true;<br />
}<br />
<br />
private void NowPBox_MouseUp(object sender, MouseEventArgs e)<br />
{<br />
if(leftClick)<br />
{<br />
if(e.X + NowPBox.Left >= NowPBox.Left && e.X + NowPBox.Left <= NowPBox.Right)<br />
{<br />
if(e.Y + NowPBox.Top >= NowPBox.Top && e.Y + NowPBox.Top <= NowPBox.Bottom)<br />
{<br />
if(!NowPBox.PressedState)<br />
{<br />
this.NowPBox.Image = this.NavButtons.Images[2];<br />
NowPBox.PressedState = true;<br />
<br />
if(SkinsBox.PressedState)<br />
{<br />
this.SkinsBox.Image = this.NavButtons.Images[3];<br />
SkinsBox.PressedState = false;<br />
}<br />
<br />
this.panel1.Visible = false;<br />
this.panel2.Visible = true;<br />
}<br />
}<br />
}<br />
}<br />
}<br />
|
|
|
|
|
Hi, I am trying to return a BSTR from a C++ function that takes a single BSTR argument. I am using the ATL type CComBSTR. My C++ function looks as follows:
extern "C" BSTR __stdcall MyStringFunction( BSTR s )<br />
{<br />
std::string str = string((LPSTR)s);<br />
...<br />
...<br />
return CComBSTR(str.c_str());<br />
}
At the moment my C# declaration looks like this:
[DllImport(@"C:\MyDll.dll", CharSet = CharSet.Unicode)]<br />
[return : MarshalAs(UnmanagedType.BStr)] public static extern string MyStringFunction( [MarshalAs(UnmanagedType.BStr)] string s );
I have had varying success with different combinations. When I don't add the MarshalAs attribute to the return string, I get the string back ok, but when it gets large ( a few 1000 characters) , I hit problems. With the current declaration, which according to documentation is correct, I only receive the first two characters.
Does anyone know, or can suggest what the problem with my approach is? I am aware of Unicode/Ansi issues, but as far as I can tell, what I have should work.
Thanks,
Christopher
The bomb lives only as it is falling
|
|
|
|
|
std::string str = string((LPSTR)s); Definitely wrong. You need to actually perform the conversion, not simply try to cast it. Remember that BSTR s are always Unicode on Windows. Either use a std::wstring , or use WideCharToMultiByte to convert it.
Your marshalling looks fine.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Thanks for the reply. The cast seems to work fine, I have messagebox pop-ups in the C++ to debug it. The string is actually a comma-delimited string that i convert into an array of longs. This is all to avoid passing arrays through the interop boundary . Anyway, I have changed the c# declaration which wasn't working to this, which does work.
[DllImport(@"C:\MyDll.dll", CharSet = CharSet.Auto)]<br />
[return : MarshalAs(UnmanagedType.BStr)] public static extern string MyStringFunction( [MarshalAs(UnmanagedType.AnsiBStr)] string s );
i.e.: I return a BStr, but pass in an AnsiBstr. This works up to the point where the string grows to around 16384, which is 2^14. At that point, my c# function throws an exception:
Object reference not set to an instance of an object
Is there any limit to the size of string you can pass across the interop boundary?
Christopher
The bomb lives only as it is falling
|
|
|
|
|
"Object reference not set to an instance of an object" usually comes out of a System.NullReferenceException . The CLR may throw a NullReferenceException if any part of the code called takes an access violation SEH exception.
Check your C++ code for possible access violations.
I still think you should fix your C++ code. ANSI BSTR s are extremely esoteric and generally wrong. BSTR should, IMO, be reserved for use in Automation-compliant COM objects, and not used in 'flat' APIs. If you must do this via strings, use the LPCSTR /LPSTR types and MarshalAs(UnmanagedType.LPStr) .
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
hi my dear
how can i set text alignment to justify property?
thanks alot
hassan azizi
|
|
|
|
|
The RichTextBox.SelectionAlignment property (of type HorizontalAlignment ) doesn't not support justification.
However, you can do this by P/Invoking SendMessage and sending the EM_SETPARAFORMAT message with a PARAFORMAT2 structure with the dwMask field set to PFM_ALIGNMENT (0x0008) at least, and the wAlignment set to PFA_JUSTIFY (0x0004). This applies to the whole current paragraph and is only supported in the Rich Edit common control (which is encapsulated by the RichTextBox ) 2.0, which probably shouldn't be a problem on modern Windows OSes on which your application would be running; otherwise, you can always deploy a new common controls library with your product.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi dear stewart
thanks for your reply
may i ask you to wrtie sendmessage methode code for me?
best regards
hassan azizi
|
|
|
|
|
[DllImport("user32.dll")]
private static extern int SendMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int msg,
int wParam,
ref PARAMFORMAT2 format); This signature is specifically for your needs. This will marshal things correctly without having to do it yourself. A generic SendMessage declaration would look like:
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int msg,
IntPtr wParam,
IntPtr lParam); If you're not sure how to do this, you're going to have to learn. Interop with unmanaged code also requires some knowledge with the unmanaged APIs you're dealing with - in this case, the Windows Common Controls.
For instance, you're going to have to declare a managed structure for the unmanaged PARAFORMAT2 structure documented in the Platform SDK for the Windows Common Controls.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi
would i pass handle of richtextbox to sendmessage function?
thanx
hassan azizi
|
|
|
|
|
Yes, using its Handle property.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi
- how i can find numeric values for API params like EM_SETPARAFORMAT ans so?
- how i can find refrence for windows dlls(like kernel32,shell32,user32) methods?
- have i to set all parameters value of PARAFORMAT2 structure or not?
thanx
|
|
|
|
|
You can find all this information in the Platform SDK, both in the documentation and in the C/C++ header files, like winuser.h and commctrl.h (most common defs). You should also check on http://pinvoke.net[^] for the managed signatures for unmanaged functions.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
well done
i found it : #define EM_SETPARAFORMAT (WM_USER + 71)
but what is tha value of WM_USER in my use?
thanx so much stewart
|
|
|
|
|
That's also a pre-prof def you'll find in winuser.h. I'd recommend indexing your various include folders to make them easier to find, or use a good text editor like VIM[^] along with ctags[^] to easily find tokens such as this while typing code in the text editor.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
ifound that :
#define WM_USER 0x0400
i use send message like below :
..........
public const int EM_SETPARAFORMAT = 1095;
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] int msg,
int wParam, ref PARAFORMAT2 format);
[StructLayout(LayoutKind.Sequential)]
public struct PARAFORMAT2
{
public UInt16 cbSize;
public UInt64 dwMask;
public double wNumbering;
public double wEffects;
public long dxStartIndent;
public long dxRightIndent;
public long dxOffset;
public double wAlignment;
public short cTabCount;
public long[] rgxTabs;
public long dySpaceBefore;
public long dySpaceAfter;
public long dyLineSpacing;
public short sStyle;
public Byte bLineSpacingRule;
public Byte bOutlineLevel;
public double wShadingWeight;
public double wShadingStyle;
public double wNumberingStart;
public double wNumberingStyle;
public double wNumberingTab;
public double wBorderSpace;
public double wBorderWidth;
public double wBorders;
};
PARAFORMAT2 Format = new PARAFORMAT2();
Format.dwMask = 0x0008;
Format.wAlignment = 0x0004;
Format.wNumbering = 0;
Format.cbSize = (UInt16)Marshal.SizeOf(Format);
Format.wEffects = 0x00400000;
Format.rgxTabs = new long[]{0};
Format.cTabCount = 1;
Format.bOutlineLevel= 0;
SendMessage(PickControl.SelectedControl.Handle,EM_SETPARAFORMAT,0,ref Format);
...........
but it's execution takes no effect to my richtextbox!
and sendmessage returns zero value!
what is my problem?
|
|
|
|
|
Do you know anything about native and managed data types? Your structure is completely wrong, thus it's not being marshalled correctly which explains why you're not seeing anything change. A LONG in C is not a Int64 (or long alias, which is recommended for readibility) but instead a Int32 (or int ).
The structure should be defined like this:
[StructLayout(LayoutKind.Sequential)]
public struct ParaFormat2
{
[MarshalAs(UnmanagedType.SysUInt)] public IntPtr cbSize;
[MarshalAs(UnmanagedType.U4)] public int dwMask;
[MarshalAs(UnmanagedType.U2)] public short wNumbering;
[MarshalAs(UnmanagedType.U2)] public short wEffects;
public int dxStartIndent;
public int dxRightIndent;
public int dxOffset;
[MarshalAs(UnmanagedType.U2)] public short wAlignment;
public short cTagCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)] public int[] rgxTabs;
public int dySpaceBefore;
public int dySpaceAfter;
public int dylineSpacing;
public short sStyle;
public byte blineSpacingRule;
public byte bOutlineLevel;
[MarshalAs(UnmanagedType.U2)] public short wShadingWeight;
[MarshalAs(UnmanagedType.U2)] public short wShadingStyle;
[MarshalAs(UnmanagedType.U2)] public short wNumberingStart;
[MarshalAs(UnmanagedType.U2)] public short wNumberingStyle;
[MarshalAs(UnmanagedType.U2)] public short wNumberingTab;
[MarshalAs(UnmanagedType.U2)] public short wBorderSpace;
[MarshalAs(UnmanagedType.U2)] public short wBorderWidth;
[MarshalAs(UnmanagedType.U2)] public short wBorders;
} The cbSize is defined as an IntPtr and marshalled as UnmanagedType.SysUInt because a UINT is defined as an unsigned int , where int is a platform-specific type. For 32-bit OSes, it's 32 bits. For 64-bit OSes, it's 64 bits. The OS, of course, is dependent on the bit-width of the processor.
To create the struct you need, do something like this:
ParaFormat2 format = new ParaFormat2();
format = new IntPtr(Marshal.SizeOf(typeof(ParaFormat2)));
format.dwMask = 8;
format.wAlignment = 4;
SendMessage(richTextBox1.Handle, 1095, 0, ref format);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
thanks stewart
it worked nice!
but i have another problem :
my language is right to left when
i send message to justify text align
the richtextbox RightToLeft property becomes
to RightToLeft.NO but it's first value was RightToLeft.Yes!
and setting that again to RightToLeft.Yes does not affect the text!
it only works properly when RightToLeft property value is RightToLeft.No!
what can i do?
how can i have deep study on native and managed data types and marshalling?
do i need special resources to do this study?
best wishes
|
|
|
|
|
Study the Platform SDK and the .NET Framework SDK. Get some experience writing native C/C++ applications which use Windows messaging, perhaps even extending the common controls.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for the Justify module.
Have you ever found the solution to the RightToLeft problem?
This problem also appears when using RightToLeft fonts.
Cheers!
|
|
|
|
|
hi my dear
if there is any free rtf parser component please
notify me about that or help me to find right ways to
writing complete rtf parser
thanks alot
hassan azizi
|
|
|
|
|
The RichTextBox both parses and displays RTF. If you don't need to display it (which brings-up the question, why bother parsing it unless you plan on converting and saving it?), you can use a Windows Rich Edit control, but that requires a lot of re-declaring COM interfaces and P/Invoking native methods. If you're not advanced in the ways of interoperability in .NET, you're probably better off finding a third-party solution, which you most likely won't find for free. It's a lot of work to correctly support all that is contained in the RTF spec. What do you expect?
Microsoft MVP, Visual C#
My Articles
|
|
|
|