tanvon++

November 22, 2006

Wana add a message handler for your own custom message

Filed under: Windows — tanvon malik @ 1:37 pm

Few days back I received an email asking a question, here is it all with my answer

Qst

Can you tell me  if I use the following

AfxGetMainWnd()->SendMessage(MY_MESSAGE, 0,(LPARAM) pValue);

The above message is sent to the parent window The message handler is

  • LRESULT MyParent::OnMyMessage(WPARAM wParam, LPARAM lParam )
  • {
  •     *(int *) lParam = 123;
  •             return 0;
  • }

then can I make Message map of the form MY_MESSAGE and have a handler function OnMyMessage ? Since message maps are something that are provided by Windows API and they can be generated through Class Wizard where messages and their corresponding functions are already given. Ex WM_KEYDOWN with function OnKeyDown() Where should I map if (MY_MESSAGE) call OnMyMessage();

My Answer

As long as I understood the situation is you want to add a message handler for your own custom message.

#define WM_MYMESSAGE (WM_USER + 100)

in the header file just have this

afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);

in cpp file do this

  • BEGIN_MESSAGE_MAP(CMyWnd, CMyParentWndClass)
  • ON_MESSAGE(WM_MYMESSAGE, OnMyMessage)
  • END_MESSAGE_MAP()
  • LRESULT MyParent::OnMyMessage( WPARAM wParam, LPARAM lParam )
  • {
  •     // do what ever you wnat to do
  •     *(int *)lParam = 123;
  •      return 0;
  • }

Wana change the Desktop Wallpaper Programmatically (IActiveDesktop)

Filed under: Windows — tanvon malik @ 1:19 pm

 Initialize the COM

     CoInitialize(NULL);

Create the instance

     ::CoCreateInstance(CLSID_ActiveDesktop,                NULL,CLSCTX_INPROC_SERVER,IID_IActiveDesktop, (void**)&pAD);

Wallpaper and other methods of IActiveDesktop will only work when active desktop is enabled. So enable it first

  • COMPONENTSOPT opt;
  • opt.dwSize = sizeof(opt);
  • opt.fActiveDesktop = opt.fEnableComponents = TRUE;
  • HRESULT hr = pAD->SetDesktopItemOptions(&opt,0);
  • pAD->ApplyChanges(AD_APPLY_REFRESH);

Set the wallpaper

pAD->SetWallpaper(L"c:mywall.jpg", NULL );

Want to change the way wallpaper is viewed, I like it to cover the whole desktop

  • WALLPAPEROPT wp;
  • wp.dwSize = sizeof(wp);
  • wp.dwStyle = WPSTYLE_STRETCH;
  • pAD->SetWallpaperOptions( &wp, NULL);
  • pAD->ApplyChanges(AD_APPLY_ALL);

And remember to free up resources after you have finished by

    CoUninitialize();

include the file below

    #include <shlobj.h>

Compile time errors…………

Yes if the above code you will try to compile with MFC it will give plenty of errors. See MSDN KB196342. Workaround is, in your project’s “stdafx.h” include the “wininet.h” at the place shown below

  • #include <afxwin.h> // MFC core and standard components
  • #include <afxext.h> // MFC extensions
  • #include <wininet.h>

tanvon Goes .NET

Filed under: DirectShow — tanvon malik @ 1:14 pm

 Download code and Demo

DirectX 9 also comes for the managed world. Although not as powerful as for unmanaged, But it works and it is much easier in managed flavor then the unmanaged. Before DirectX 9 for audio video work you only have to install DirectX SDK, and do the work. But now Microsoft splits audio video section from the DirectX, Now it is part of Platform SDK, and is known as DirectShow, Now want to work with audio video install Platform SDK, But if you want to use VMR then you will have to also install DirectX 9 SDK as well. But for managed world not too much problem, Just install DirectX 9 all is OK. Use Microsoft.DirectX.AudioVideoPlayback namespace it provides us classes for easy video playback and control

Blog at WordPress.com.