VisualCpp.org Collection of Visual C++ Stuffs

25Apr/07Off

How do I create a CWnd from an HWND?

Let me extend it more, or CDC from HDC and so on.. MFC Framework writer given MFC programmer flexibility to easily convert information of the HANDLE to respective class..

There are two way to do so! first CWnd::Attach() and CWndDetach() Method, for temporary taking owner ship and releasing after user e.g.

void CMyClass::GetOwnerShipAndRelease(HWND a_hWnd)
{
CWnd myWnd;
myWnd.Attach(a_hWnd);

// do work on that myWnd

// Call detach
myWnd.Detach();
}

Second way to use static function FromHandle() which return pointer of respective class

 CWnd* pMyWnd = CWnd::FromHandle(a_hWnd);

Filed under: General, MFC Comments Off
16Apr/07Off

How to run an application when Computer is about to shutdown?

 Many software require to do some  some task at the time of shutdown.. or temporily / permantely halt the system shutdown. when ever system shutdown normally, it send WM_QUERYENDSESSION to every running top level window application. you just have to handle that message and return zero, instructing Operating system to stop shutdown. MFC doesn't provide you any predefined macro for that you have to handle that in ON_MESSAGE macro and Window32 Programming you can directly handle it in your WindowProc.

  Now how to shutdown again, Just call Api ExitWindowEx or InitiateSystemShutdown to shutdown the system. after finishing your Shutdown task!. have a look at this article for Shutdown your Computer .

Filed under: General, MFC, Win32 Comments Off
16Apr/07Off

I have a dialog-based application and want the dialog hidden on startup?

In VS 2005, MFC is now sending two WM_WINDOWPOSCHANGING messages to newly created dialogs. Also, if your dialog creates one or more child dialogs in CDialog::OnInitDialog, you'll receive the message from all of them. The code given should be updated to look like:

void CMyDlgBasedAppDlg::OnWindowPosChanging(WINDOWPOS *pWP)
{
    CDialog::OnWindowPosChanging(pWP);
    if (pWP->hwnd == m_hWnd && pWP->flags & SWP_SHOWWINDOW)
    {
              if (++m_nShowCount <= 2)
             {
                pWP->flags &= ~SWP_SHOWWINDOW;
             }
     }
 }

---------------------------------------------------------------
Tip taken from codeproject.com, tip courtesy :- Mr. Rick Murtagh

Filed under: Dialog Based Comments Off
16Apr/07Off

Why pure virtual funtion is always put equal to 0 or NULL?

humm. intresting thing.. actually you must be knowing the fact, when ever we declare the any function virtual, the compiler create VTABLE for it, which contain address of virtual function (Will brief you about it in next post), now by puting equal to zero, we are instructing the compiler to make entry of this function ADDRESS in VTABLE equal to zero/null.

now since function has class scope and since it pointing to null address, you can't instantiate object of that class, thus making it abstract.

Thats why you must have to override the pure virtual function in derived class! and if now we put any thing else that NULL in pure virtual function.. compiler will treat as addess and will allow the object of class instantiate.. and result are unprediactable in that case!

Filed under: General Comments Off
9Apr/07Off

How can I get the window handle of the process created using function CreateProcess?

  humm.., since there in no direct Api to do so.. we have to create our own method to do so. After executing the process using the CreateProcess, we were left with the path of executable and Process id retrieved from call of CreateProcess. now you just have to enumerate all top level window using EnumWindows or FindWindowEx function and calling GetWindowModuleFileName on each retrieved HWND and plus comparing each retrieved process id with original PID.

steps:-

  •     First Launch process using CreateProcess and store launch path and PID
  •     Now using EnumWindow  OR FindWindowEx enumerate the TopLevel Window
  •     Use GetWindowModuleFileName and CWnd::Attach on each reterieved HWND Handle, now compare PathName with stored PathName and PID with PID retrieved from CWnd::GetWindowProcessID() to get the exact Window Handle
Filed under: MFC, Win32 Comments Off
9Apr/07Off

How to convert CString Value to HWND ?

Some time some Programmer to pass data from one process to other, use runtime editing to INI file. i know it bad method.. but software industry is all about getting thing done. any ways here is way which convert CString to HWND .

I have executed the notepad.exe before trying this code.. this code first find the handle of Notepad.. make it minimize, then convert it handle to CString and again reterieve the handle from CString using _ttol and typecasting to HWND.. and then check weather we got correct handle or not by executing command on it!

HWND NotepadWnd =::FindWindow("Notepad",NULL);

::ShowWindow(NotepadWnd,SW_MINIMIZE);

CString find;
find.Format("%d",NotepadWnd);

HWND hConvertedHwnd= (HWND)_ttol(find);
::ShowWindow(hConvertedHwnd,SW_MAXIMIZE);

Filed under: General, MFC Comments Off
9Apr/07Off

How do I prevent the escape key, when pressed, from closing a dialog window?

Simply override the CYourDialog::OnCancel() virtual function, but this will also block closing from cross button present on system menu.. now to achieve both functionality do this:-

// Called when user clicks the close button
void CMyDialog::OnClose()
{
CDialog::OnCancel();
}

// called when user presses ESC button or clicks IDCANCEL button
void CMyDialog::OnCancel()
{
// Do nothing
}