|
But I responded as such. I've seen way too many people get all hung up on how pretty/tight the code looks, or how efficient they *think* it is, when it matters not one iota. Trimming a few variables/statements from an already small function just screams of wasted time. Unless a profiler was used to gather actual metrics, there's really no use in doing it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I totally, completely despise putting the { on the same line as the if/while/whatever. That makes it very hard to find matching braces when you're skimming through code. The { and } absolutely must be in the same column.
|
|
|
|
|
That was part of my modification as well
And I agree totally, with {} being in the same column.
|
|
|
|
|
This is an old debate and is a matter of preference or company policy.
I prefer and use the option 2, because I find it easier to read and I like to be consistent with my placement of braces.
Once upon a time I used option 1, but that was when programming in C. Now days with C++ and now .NET, the length of the lines are harder to control. I do not like to have to search for the starting brace, because it interferes with my flow of thought. Life is complicated enough as it is.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
I'm late getting in, but I just now read it.
Two viewpoints:
First: Scanning code, looking for a quick idea of what's happening. In this context, definitely the second format. Without spending 15 seconds to readjust my glasses, and beginning to mentally parse thru each symbol in the function, I couldn't make heads or tails of the first format.
Second: When you KNOW this function has an issue, and you simply MUST trace thru it. Then perhaps the first format, although I didn't get that deep. I stopped after 15 seconds (since I'm not getting paid for looking thru that code).
I TRY to write code knowing that non-programmers (but smart folk nonetheless) will be picking thru my code at some point in the future, and I try to write as literarily as possible. Non-literary symbols (the standard C++ operators like &, *, ::, etc.) can make the code difficult to read, even for me, and I do this stuff 8 hours/day!
By the way; excellent discussion on exit points. Thanks to the contributors.
David
|
|
|
|
|
Hi frnds,
Im trying to get the handle of the active window...to take the image of it...
the problem is that the window is in other process...and GetActiveWindow() gets the window of current thread...since both are different threads the above fn is not working...
guide me in this matter...
|
|
|
|
|
What about GetForegroundWindow()?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thankx a lot Mark... that worked for a bit...
The prob with ForGr..() is tht if we open any other window while this is code is running...then screenshot is changed cos it takes the picture of the window with which the user is interacting at that moment...
I'll explain my scenario a bit...
Im opening an application from my code...then that application is run and tht app will create a dialog...until the dialog is destroyed the control doesnt return to the next line of code...so im creating another thread and taking the screen shot...
GetActiveWindow() returns the handle of the window of the present thread but i want the handle to the window of another thread...
i got a start in this dir...
i used GetWindowThreadProcessId() to get the threadID and used GetGUIThreadInfo() to get the handle for the window...
but i got got stuck here...its throwing a external link error...
can u guide me in this...
Inder...
|
|
|
|
|
dudedotnet wrote: its throwing a external link error...
What specific error?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
error LNK2001: unresolved external symbol _GetGUIThreadInfo
|
|
|
|
|
Is your project configured as a win32 or MFC app?
I'm not sure why it wouldn't link - user32.lib is the import library.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
configured as win32 application
|
|
|
|
|
Hi
I have access to a CAD program that allows me to insert an ActiveX Control into its main window.
I wish to insert a TreeView, but fail to find any good ActiveX Control TreeViews. My idea is then to use the System.Windows.Forms.TreeView from the .NET framework. Is that readily usable as an ActiveX control, somehow?
I, of course, need to be able to change the content of the tree as well as receiving event from the tree. Everything is programmed in C++ using Visual Studio 2005.
I can see that I can create a wrapper for any .NET control using COM Callable Wrappers and REGASM, but if this is really necessary then surely someone must have done this before me!
Please advise. What is the easiest way to handle this problem.
Thanks
LG
---
See news at: www.dzone.com
|
|
|
|
|
Hi,
I would like to know if there is a way to switch windows focus to another application thats already running from a current application?? For example, I have two MFC applications (each application uses up one screen of the two screens connected to the system) running on a system, I spend most of the time running one of those applications, but occasionally I wanna do a quick click on another application, I dont want to waste time to drag mouse and check where the button on the other application is to click on it.
So is there a way to achieve this??
-Pavan
PKNT
|
|
|
|
|
Kiran Satish wrote: I would like to know if there is a way to switch windows focus to another application thats already running from a current application??
SetForegroundWindow()?
Kiran Satish wrote: but occasionally I wanna do a quick click on another application
That's different than just setting the focus to another application.
Do you want to click a button in another app remotely?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for answering my questions, actually the two applications(app1, app2) are on the same system, I just want to set focus to another application(app2) when I click a button/keyboard character in app1. Once I set the focus onto app2 I can read another keystroke thats now will be read in app2 and do appropriate work.
From MSDN it says- BOOL SetForegroundWindow( HWND hWnd ); How can I get handle for app2 in app1?? They both are started individually.
-Pavan.
PKNT
|
|
|
|
|
No matter what you use, you're going to need to know something
about the other process.
In this case, you need to know the other process' HWND.
FindWindow() can help if you know what you're looking for.
GetWindowThreadProcessId() may be useful as well.
It depends what info you have to identify the other process.
Once you have the window handle, you can use SetForegroundWindow()
to activate it. You should then call GetLastActivePopup(), and if it returns
a different window handle then the one you activated, call
SetForegroundWindow() on that window.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
This question is not completely related to this thread, but while working on the above, I came accross a situation where I need to get keyboard events and I am trying to use WM_KEYDOWN function to capture the events from a dilaog window thats created in a main dialog application, I tried it directly in main dialog window also, but it does nothing. I had a message box in it to pop up whenever it enter OnKeyDown() function.
I know it works, because I used the same way in another application which is in Doc/View architecture. Am I doing anything wrong here??
-Pavan.
PKNT
|
|
|
|
|
Generally, the problem is that only one window at a time can
have keyboard focus. In a dialog, it's usually one of the controls
in the dialog that has the keyboard focus, not the dialog itself.
This means you need to either
a) Handle the WM_KEYDOWNMESSAGE in the appropriate
window (control) (sub)class or
b) Override CWinApp::PreTranslateMessage() and filter every WM_KEYDOWN
that is sent on the thread.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I tried to use the override method, but I have some problems in reading the key code from it.
BOOL CMyApp::PreTranslateMessage(MSG* pMsg) <br />
{<br />
if(pMsg->message == 256)
MSGFILTER * lpMsgFilter = (MSGFILTER *)pMsg->wParam;<br />
<br />
return CDialog::PreTranslateMessage(pMsg);<br />
}
When I try to read the message into lpMsgFilter and check in debug, it is having NULL values in it and the application gives Unhandled exception..:Access Violation error. Is there any other way to read the keycode??
-Pavan.
PKNT
|
|
|
|
|
You can't call the CDialog PreTranslateMessage() from there.
I'm not sure how that even compiled.
You should be calling the base class' method.
If the message is WM_KEYDOWN, wParam is the virtual key code,
not a pointer.
Also, why would you hardcode 256 instead of using WM_KEYDOWN?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Its working and I can read the key codes directly from value passed by PreTranslateMessage.
Coming back to focuswindow, I am trying to focus another child window from other child window (both are in the same main application) and want to focus all my keystrokes on a particular button on that child window. Then, any keystrokes even like space bar should be applied to that button, at present as soon as I change focus to the child window and when I press spacebar, it applies it to the first button that I created while creating the dialogbox. How can i change this behavior, tried SetDefId(ID) and it doesnt help either.
I am using WM_KEYDOWN only, not 256 (I used it just like tht nothing particular).
Any suggestions??
thanks,
-Pavan.
PKNT
|
|
|
|
|
You should be setting focus to the button if you want the button to have focus.
If you set focus to the button's parent, yo'll get focus at whatever control
the parent passes focus to.
You can post WM_NEXTDLGCTL to the parent to set focus to a certain control
in the window.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am trying in this way and it still goes to the default button on that window that I created first.
I have three dialogs (main dialog, child dialog1, child dialog2), now I am trying to set focus onto dialog2 from dialog1 and also set the control to button4 on dialog2 and it always goes to button1 on dialog2 as soon as I set the focus on dialog2.
<br />
(parent->dialog2)->SetFocus();<br />
CButton *Button;<br />
Button=(CButton *)GetDlgItem(IDB_GRAB);<br />
(parent->dialog2)->PostMessageA(WM_NEXTDLGCTL,Button->GetDlgCtrlID(),TRUE);
Is it correct??
-Pavan.
PKNT
|
|
|
|
|
Kiran Satish wrote: Is it correct??
No.
(parent->dialog2)->SetFocus();<br />
CButton *Button;<br />
Button=(CButton *) (parent->dialog2)->
GetDlgItem(IDB_GRAB);<br />
(parent->dialog2)->PostMessageA(WM_NEXTDLGCTL,(WPARAM)(HWND)*Button,TRUE);
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|