|
Option 1:
Use the System::Diagnostics::Process class and run the .exe of the form.
Option 2:
Call ShowDialog() on the form.
|
|
|
|
|
That won't work without remoting. That executes the entry point of the application which usually calls Application.Run , but a calling app has no control over that without using remoting.
-----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-----
|
|
|
|
|
Don't think of projects - think of assemblies. Just like you create a form from the System.Windows.Forms, you do from another projects. One cool trick in VS.NET is to add a project reference as a dependency to the project that needs it. Just right-click on the project that needs a class from the other project, click Add Reference, then click the Projects tab. Double-click the project to add it and click OK to close the dialog. The great thing about this is that when you do a Debug, Release, or custom build configuration, the appropriate assembly reference is used.
To use a form in another assembly, just instantiate the class (using the fully-qualified type name (namespace + class), or add using namespace; at the top of your source file. Instantiate the form like any other.
Now, if these two projects run side-by-side (both executing individually at the same time), you'll need to use remoting. Doing what the other person mentioned won't work - you can only execute a program's entry point that way.
-----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-----
|
|
|
|
|
That is sort of what I ended up doing. I started the project over and created the other forms I needed in the same project, instead of separate projects for each form, and then instantiated an instance of the form with the ShowDialog method to display it.
Thanks for the replies>
Chuck Schmidt
|
|
|
|
|
I'm having a problem getting my program to restore from the system tray. Minimizing to it is no problem.
private void FormMain_SizeChanged(object sender, System.EventArgs e)
{
if ( this.WindowState == FormWindowState.Minimized )
{
this.ShowInTaskbar = false;
this.notifyIcon.Visible = true;
}
}
And restoring it:
private void notifyIcon_DoubleClick(object sender, System.EventArgs e)
{
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
notifyIcon.Visible = false;
}
But I'm getting this error before the program is shown in the taskbar:
An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in system.windows.forms.dll
Additional information: Error creating window handle.
Any ideas?
|
|
|
|
|
Sorry, forgot to sign in.
|
|
|
|
|
Is it possible to have the tab key work inside of a property grid? So that when a user is editing properties, they can just hit tab to go through the list of properties?
Or is there some other key that does this with a property grid?
|
|
|
|
|
Dear all ,
if anybody know how to deal with USB port by C# ,could you send me the function that i have to use , i feel that it is not as easy as COM (RS232) programming so can you help me with it
Mhmoud Rawas
------------
Software Eng.
|
|
|
|
|
I found this article link[^] really helpful when I was trying to access a Cypress USB device through C#. Please note that it uses managed and unmanaged C++ to do the communications (not exactly what you want I imagine). If anyone else has any other information on how to communicate directly using C# I would also be interested.
Cass
|
|
|
|
|
Thank you for that information . i have found another help full document about usb you can find it in the following web site : www.usb.org
Mhmoud Rawas
------------
Software Eng.
|
|
|
|
|
I have an application written in C#... nothing really special about it but for some odd reason it loses all focus and becomes non-responsive after I tab onto another application. In the windows ALT-TAB list I see a WindowsFormsParkingWindow...
What is this and why is my app dying? No one on the web seems to know why this is happening.
modified 30-Aug-22 21:01pm.
|
|
|
|
|
Here is some more info on this issue:
If you have time, please can you have a quick look into this:
We are developing a windows forms application using VS.NET 2003 and are having endless hassles.
Our current issue is the application seems to lose focus and will no longer receive focus or fire click events.
We can invoke this behaviour by ALT-TABBING from our application and then returning to it.
We need to forceably shut it down and reopen it.
When this happens, we can ALT-TAB and then there is non-visual window called “WindowsFormsParkingWindow“ that seems to come from the framework.
This link mentions it, but is of no help.
http://www.csharp-station.com/ShowPost.aspx?PostID=2564
Any ideas?
A very similar issue we have is that the same application won’t close when the top-right window close button is pressed or when ALT-F4 is pressed.
Although we have had the “WindowsFormsParkingWindow” before, the app didn’t respond to ALT-F4 beforehand too.
We are positive that we are not overriding onClose events or trapping these keypresses.
Microsoft doesn’t have KB on this currently.
Cheers,
Simon
sig :: "Don't try to be like Jackie. There is only one Jackie.... Study computers instead.", Jackie Chan on career choices.
article :: animation mechanics in SVG blog:: brokenkeyboards
|
|
|
|
|
Hi,
Let's say that I have a TextBox or a RichTextBox on my windows form. I need to programmatically scroll it up or down, but I can't seem to find a way how to do it, the only method I have seen is scroll to caret. Is it possible at all, by a call to some method or by forwarding events from keyboard/mousewheel to the respective control?
Thanks in advance,
Rado
Radoslav Bielik
http://www.neomyz.com/poll [^] - Get your own web poll
|
|
|
|
|
You'll have to P/Invoke SendMessage and create a couple consts (or a enum) with the values for EM_GETSCROLLPOS (1245) and EM_SETSCROLLPOS (1246). You'll also need to define a POINT struct as it is in Windows. Then, call SendMessage with the handle of the RichTextBox , which you can get from the Handle property inherited by Control :
[DllImport("user32.dll")]
private void SendMessage(IntPtr hWnd, int msg, int wParam, ref POINT lParam);
private struct POINT
{
public long x;
public long y;
}
private const int EM_GETSCROLLPOS = 1245;
private const int EM_SETSCROLLPOS = 1246;
POINT p = new POINT();
p.x = x-coordinate;
p.y = y-coordinate;
SendMessage(myRichTextBox.Handle, EM_SETSCROLLPOS, 0, ref p); You can roughly calculate using the font properties where you want to scroll to, or use EM_GETSCROLLPOS to get the current position, then add or subtract an x and / or y value and call EM_SETSCROLLPOS to scroll the text down or up, respectively.
Read more about EM_SETSCROLLPOS in the MSDN library for more information.
-----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 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
|
|
|
|
|