|
Hello Heath,
Thank you very much for your detailed response! One of my friends already suggested a similar method (using a different message constant) to me which scrolls by one line of text at a time, but I will surely test your method as well.
This is the code my friend suggested, if anyone is interested:
<br />
private const uint WM_HSCROLL = 0x0114;<br />
private const uint WM_VSCROLL = 0x0115;<br />
private const uint SB_LINEUP = 0;<br />
private const uint SB_LINELEFT = 0;<br />
private const uint SB_LINEDOWN = 1;<br />
private const uint SB_LINERIGHT = 1;<br />
<br />
SendMessage(this.rTB1.Handle, WM_VSCROLL, SB_LINEUP, 0);<br />
SendMessage(this.rTB1.Handle, WM_VSCROLL, SB_LINEDOWN, 0);<br />
SendMessage(this.rTB1.Handle, WM_HSCROLL, SB_LINELEFT, 0);<br />
SendMessage(this.rTB1.Handle, WM_HSCROLL, SB_LINERIGHT, 0);<br />
<br />
<br />
[DllImport("User32")]<br />
private static extern uint SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);<br />
Thanks again for your help,
Rado
Radoslav Bielik
http://www.neomyz.com/poll [^] - Get your own web poll
|
|
|
|
|
WOW CodeProject is so cool
I was just about to ask this question only to come and find it answered =)
CP RULEZ!!
|
|
|
|
|
Is there anyway to scroll the TextBox when you are not focused on the application?
|
|
|
|
|
If you send the message to the RichTextBox using its handle. That handle won't change during the control's lifetime even if the application or the control looses focus. If you are having this problem, you're either using the wrong message (although I can't imagine what) or trying to use the handle from the active Window.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hello every one:
I have a question about Head off API functions.
I have a article about it,but it is written with vc++.
I know that we can do it in vc++,but I want to do it in
C#,if I do this ,I have some questions about it .first:
there are some ASM in it,Secnod I can not write DLL like
vc++,here is the code,please help me .sorry for my poor English.
BOOL VirtualProtectEx(
HANDLE hProcess, // 要修改内存的进程句柄
LPVOID lpAddress, // 要修改内存的起始地址
DWORD dwSize, // 修改内存的字节
DWORD flNewProtect, // 修改后的内存属性
PDWORD lpflOldProtect // 修改前的内存属性的地址
);
BOOL WriteProcessMemory(
HANDLE hProcess, // 要写进程的句柄
LPVOID lpBaseAddress, // 写内存的起始地址
LPVOID lpBuffer, // 写入数据的地址
DWORD nSize, // 要写的字节数
LPDWORD lpNumberOfBytesWritten // 实际写入的子节数
);
BOOL ReadProcessMemory(
HANDLE hProcess, // 要读进程的句柄
LPCVOID lpBaseAddress, // 读内存的起始地址
LPVOID lpBuffer, // 读入数据的地址
DWORD nSize, // 要读入的字节数
LPDWORD lpNumberOfBytesRead // 实际读入的子节数
);
It is DLL here:
HHOOK g_hHook;
HINSTANCE g_hinstDll;
FARPROC pfMessageBoxA;
int WINAPI MyMessageBoxA(HWND hWnd, LPCTSTR lpText,LPCTSTR lpCaption,UINT uType);
BYTE OldMessageBoxACode[5],NewMessageBoxACode[5];
HMODULE hModule ;
DWORD dwIdOld,dwIdNew;
BOOL bHook=false;
void HookOn();
void HookOff();
BOOL init();
LRESULT WINAPI MousHook(int nCode,WPARAM wParam,LPARAM lParam);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
if(!init())
{
MessageBoxA(NULL,"Init","ERROR",MB_OK);
return(false);
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
if(bHook) UnintallHook();
break;
}
return TRUE;
}
LRESULT WINAPI Hook(int nCode,WPARAM wParam,LPARAM lParam)//空的钩子函数
{
return(CallNextHookEx(g_hHook,nCode,wParam,lParam));
}
HOOKAPI2_API BOOL InstallHook()//输出安装空的钩子函数
{
g_hinstDll=LoadLibrary("HookApi2.dll");
g_hHook=SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)Hook,g_hinstDll,0);
if (!g_hHook)
{
MessageBoxA(NULL,"SET ERROR","ERROR",MB_OK);
return(false);
}
return(true);
}
HOOKAPI2_API BOOL UninstallHook()//输出御在钩子函数
{
return(UnhookWindowsHookEx(g_hHook));
}
BOOL init()//初始化得到MessageBoxA的地址,并生成Jmp XXX(MyMessageBoxA)的跳转指令
{
hModule=LoadLibrary("user32.dll");
pfMessageBoxA=GetProcAddress(hModule,"MessageBoxA");
if(pfMessageBoxA==NULL)
return false;
_asm
{
lea edi,OldMessageBoxACode
mov esi,pfMessageBoxA
cld
movsd
movsb
}
NewMessageBoxACode[0]=0xe9;//jmp MyMessageBoxA的相对地址的指令
_asm
{
lea eax,MyMessageBoxA
mov ebx,pfMessageBoxA
sub eax,ebx
sub eax,5
mov dword ptr [NewMessageBoxACode+1],eax
}
dwIdNew=GetCurrentProcessId(); //得到所属进程的ID
dwIdOld=dwIdNew;
HookOn();//开始拦截
return(true);
}
int WINAPI MyMessageBoxA(HWND hWnd, LPCTSTR lpText,LPCTSTR lpCaption, UINT uType )//首先关闭拦截,然后才能调用被拦截的Api 函数
{
int nReturn=0;
HookOff();
nReturn=MessageBoxA(hWnd,"Hook",lpCaption,uType);
HookOn();
return(nReturn);
}
void HookOn()
{
HANDLE hProc;
dwIdOld=dwIdNew;
hProc=OpenProcess(PROCESS_ALL_ACCESS,0,dwIdOld);//得到所属进程的句柄
VirtualProtectEx(hProc,pfMessageBoxA,5,PAGE_READWRITE,&dwIdOld);//修改所属进程中MessageBoxA的前5个字节的属性为可写
WriteProcessMemory(hProc,pfMessageBoxA,NewMessageBoxACode,5,0);//将所属进程中MessageBoxA的前5个字节改为JMP 到MyMessageBoxA
VirtualProtectEx(hProc,pfMessageBoxA,5,dwIdOld,&dwIdOld);//修改所属进程中MessageBoxA的前5个字节的属性为原来的属性
bHook=true;
}
void HookOff()//将所属进程中JMP MyMessageBoxA的代码改为Jmp MessageBoxA
{
HANDLE hProc;
dwIdOld=dwIdNew;
hProc=OpenProcess(PROCESS_ALL_ACCESS,0,dwIdOld);
VirtualProtectEx(hProc,pfMessageBoxA,5,PAGE_READWRITE,&dwIdOld);
WriteProcessMemory(hProc,pfMessageBoxA,OldMessageBoxACode,5,0);
VirtualProtectEx(hProc,pfMessageBoxA,5,dwIdOld,&dwIdOld);
bHook=false;
}
//测试文件:
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
if(!InstallHook())
{
MessageBoxA(NULL,"Hook Error!","Hook",MB_OK);
return 1;
}
MessageBoxA(NULL,"TEST","TEST",MB_OK);//可以看见Test变成了Hook,也可以在其他进程中看见
if(!UninstallHook())
{
MessageBoxA(NULL,"Uninstall Error!","Hook",MB_OK);
return 1;
}
return 0;
}
|
|
|
|
|
So, you want us to rewrite your program for you?!
This wouldn't be hard to rewrite in managed code, but you should take a look at the System.Runtime.InteropServices namespace, which you'll need to marshal the struct fields and function parameters, as well as marshal packed data from LPVOID to structures that you'll have to define. And use MessageBox.Show from the System.Windows.Forms namespace instead of MessageBoxA (which also warrants the warning that in .NET, all strings are treated as Unicode unless explicitly made to be otherwise - when available, though, use CharSet.Auto ).
And you cannot write assembler in your code. .NET languages are managed languages (meaning that the common languages runtime, or CLR, takes care of all memory). You can use unsafe code and perform pointer manipulation, but this is not recommended since you bypass the managed runtime and, thus, certain other code access permissions are required that aren't granted by default except to code executed from your local machine.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thank you very much.
it is ok,I hope that there is someone to rewrite it for us
in C#.If we can do it ,I think it is a very useful code.
It is a simple example to head off "messagebox" of API functions.We can extern it to very useful aspect.
And the same time ,I have a question about hook message.
This afternoon ,I wrote a journal record function,but I failed.could you give me a example about journal hook.
Thank you very much.
sdfasd
|
|
|
|
|
The only way you're going to learn C# is to just try it...and read the documentation! I can't stress enough how important it is to read the documentation and the class library. The code you provided wouldn't be hard to translate to C#. Just understand what can be done with the .NET base class library (BCL) and what you'll need to P/Invoke. And, if I remember correctly, you won't need to P/Invoke anything.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
DO a search for a C# article here on CP by the name of "minesweeper" something, minesweeper should be enough. I think he does something similar to what you are trying.
Good luck.
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
Thank you ,I think you do not understand my means.
I want to head off API functions.I saw the article age,
it is a article about rewrite Memory.In my opinion,that is
a part of it.
sorry for my poor English
sdfasd
|
|
|
|
|
veryu sorry to ask such a stupid question here...but i am trying to learn C# on my own.so i will be very thanksful if someone could answer my stupid ques..
i made a new app with C# and chose windows application at the project...
now i got the project..there is one form present...
now in old VS i go to class wizard and add the OnPaint() handler...
how do I add the OnPaint() handler in VS.Net???
can someone pls tell how to add a event handler???
do i have to type the whole thing everytime???
can someone pls help me??
tks...
Have a Super Blessed Day!
-------------------------
For God has not given us a spirit of fear, but of power and of love and of a sound mind.
2 Timothy 1:7
"For God so loved the world that He gave His only begotten Son, that whoever believes in Him should not perish but have everlasting life."
John 3:16
"Therefore you also be ready, for the Son of Man is coming at an hour you do not expet."
Luke 12:40
|
|
|
|
|
First, you should understand what happens to the code since VS.NET merely manipulates code and other resource files. Dragging and dropping is hardly "programming". .NET uses an event handler system that is easy to understand and - many times - can't be hooked up using the designer.
To use VS.NET to do this, though, use your Property Grid to switch to events (the first couple of buttons at the top of the Property Grid show properties or events, respectively). Find the Paint event and either double-click it, or type your own event handler method name and press Enter. Like VS6 and below, it will jump you to the method block to begin writing code.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
i dont know hwere is the property grid...how to find it???which one is it???can u pls tell me...tks a lot...i have been looking for property grid for very long....
can u tell me which one is it??how to find it???
tks a lot./..
Have a Super Blessed Day!
-------------------------
For God has not given us a spirit of fear, but of power and of love and of a sound mind.
2 Timothy 1:7
"For God so loved the world that He gave His only begotten Son, that whoever believes in Him should not perish but have everlasting life."
John 3:16
"Therefore you also be ready, for the Son of Man is coming at an hour you do not expet."
Luke 12:40
|
|
|
|
|
As with practically every other Windows application, right-click on an object in your form (or the form itself) and click "Properties". The property grid will recieve the focus. It is also on by default and should be obvious. It's always there as a docked Window but can be undocked. You should read the product documentation if you're unsure.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Is there a way, I can Disable Font Style(Bold, Italics etc) and Font Size from Font Dialog box in C#.
|
|
|
|
|
Not properties and methods in .NET, no. You'd have to P/Invoke SendMessage and GetWindow . You'd then have to call GetWindow several times with the right params (which you should define as consts or an enum - see the native GetWindow documentation in MSDN) to get the two controls you want to disable. This is also risky because this common dialog may change in the future, thus throwing the controls possibly out of an order you've defined. You could just enumerate them all till you find two windows of the right Windows class (which means more P/Invoking). Then you can call SendMessage(handle, WM_ENABLE, false, 0) , where handle is the Window handle of the each control.
A better way to handle this would just be to define your own dialog which includes only those things you want. You can make it robust like the FontDialog so that it accepts a Font from which to initialize the form's values, and returns a Font created from the old Font (as the template) with the new attributes selected in the dialog. This is just like the FileDialog.Font property.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
hi All,
This problem is for C# windows application.
I have a user control whcih contains one picture box along with other
controls.
Depending on certain conditions I want to change images on the picturebox.
But at the same time I want to refer only assembly of the user control in
other solution.
If I am using method System.Drawing.Image.FromFile("filename") I have to put
gif files containing those images in bin directory of solution in whcih I am
using user control.
But I don't want that.
I want that only assebly needs to be referenced.
Is there any way for this.
If yes please send code snipet.
Thanks
Shailaja
|
|
|
|
|
If you mean embedded resources in a .NET assembly, then embed resources in the assembly from which images can be selected, then use the Bitmap(Type, String) constructor. The Type parameter should be a type that has the same namespace as the embedded resources (in a mannger of speak), and the String should be the name of the resource that begins with the same namespace. See that constructor documentation for more details.
You can also create an image from a stream, which means you can use Assembly.GetManifestResourceStream to get a Stream for the embedded resource, which you then create an image from, and assign that to PictureBox.Image .
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi Heath,
thanks for your input.
I did not get what you have suggested in 1st para.
What I did is I embedded the resources by changing their build property to embedded resource. And I tried to get the required resource from Assembly Manifest by using statements
System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = thisExe.GetManifestResourceStream ("ShapeLibrary.5side_Equipment.gif");
baseShape1.PropDisplayImage = Image.FromStream(file);
While I am trying to compile the project getting this exception message
" An exception occurred while trying to create an instance of ShapeLibrary.BaseShape. The exception was "'null' is not a valid value for 'stream'.".
Whereas "ShapeLibrary" is name of assembly containing Embedded resources. BaseShape is the user control in this Library in which I am putting image from this perticular code above.
For that I have written public property which sets image on the picture box.
Have I missed anything?
Regards
Shailaja
|
|
|
|
|
If you're getting that error while compiling, the code above has nothing to do with it. It's not JIT'ed and executed during compile-time, only at run-time. And the name of the assembly doesn't matter when resolving the embedded resource per se. If you're using VS.NET, the resource will contain the base namespace (configurable in the project properties, which is defaulted to the assembly name) plus dot-separated namespaces for each folder the resource is in (for source files, the namespace uses these as well for default namespaces), plus the resource name. If you're not sure about this, open your compiled assembly in ildasm.exe (the IL Disassembler, part of the .NET Framework SDK) and examine the MANIFEST file. Toward the bottom you'll see .mresource entries. Type the name into GetManifestResourceStream exactly as you see it there.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi
I noticed diffrence in the way these embedded resource files are described in assembly manifest.
Some are without '' and some within ''
Is it affecting the working of my code?
for Eg:
.mresource public ShapeLibrary.Sensor.gif
{
}
.mresource public 'ShapeLibrary.4side_Equipment.gif'
{
}
.mresource public 'ShapeLibrary.5side_Equipment.gif'
{
}
So should I use stringname = "'ShapeLibrary.4side_Equipment.gif'";
where as earlier I was not using ''?
I tried doing this but still the exception persists.
Regards
Shailaja
|
|
|
|
|
Again, are you getting this error when compiling your project, or when executing it?
And as far as the single quotes go, you should use them if they are specified there, but I'm curious how they got there in the first place. Check the resource name in the property grid when you select the file in your project that is the embedded resource and make sure there are no single quotes.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I am getting this error while compiling. I could not reach till execution stage.
Regards
Shailaja
|
|
|
|
|
Then you have a different problem than the name of the resource in your source code - that isn't checked by the compiler, except for syntax and type-safety. Having those single quotes in your resource names (for the project, not the code) could be causing problems for the assembler. Like I said in my previous post, try to figure out why the single quotes are there and get rid of them. You might have to manually edit your .csproj file, but it isn't difficult. Be sure to do a complete rebuild when you're finished to make sure the resources are properly embedded.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi Heath,
I checked csproject file also opening it in notepad.
The resource files having single cots in manifest are shown like pther files not having double cots.
if you don't mind can I zip the solution and send it to you?
Regards
Shailaja
|
|
|
|
|