Knowledge Level: Intermediate
=======================
මෙම උදාහරණය සදහා භාවිතා කොට ඇත්තේ MS Visual C++ 6 මෘදුකාංගය බව සළකන්න.
උදාහරණය සදහා භාවිතා කොට ඇති පරිගණක වැඩසටහන මෙතැනින් Download කරන්න.
MS Visual C++ 6 මගින් අළුත් Win32 Application ප්රොජෙක්ට් එකක් සාදා, එයට රිසෝසස් තුනක් ඇඩ් කරන්න:
- Dialog Resource - IDMAINDLG
- Icon Resource - IDSTICON
- Menu Resource - IDSTMENU
දැන් WinMain ෆන්ක්ෂන් එක සහිත cpp ෆයිල් එක තුළ පහත දැක්වෙන includes යොදන්න:
windows.h
shellapi.h
"resource.h"
දැන් පහත දැක්වෙන වේරියබල්ස් සහ ෆන්ක්ෂන්ස් declare කරන්න:
static HMENU g_hMenuTop = NULL;
HWND hwnd;
NOTIFYICONDATA nid;
static HWND g_hWndMessages = NULL;
static HICON g_hIconTray = NULL;
void ShowSystrayMenu(HWND hwnd);
HWND CreateMessageWindow(HINSTANCE hInstance);
BOOL SysTrayCMD(UINT nAction);
BOOL CALLBACK WndProc(HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
const UINT g_nMsgIcon = ::RegisterWindowMessage("systrayexample.TrayIcon.Notify");
පහත දැක්වෙන්නේ ඉහත ෆන්ක්ෂන්ස් define කරන හැටිය. ඒවායින් සිදුකෙරෙන දේ ගැන කෙටි හැදින්වීමක්ද කර ඇත:
HWND CreateMessageWindow(HINSTANCE hInstance)
{
// create a simple dialog-based window to handle the
// messages from the tray
HWND hwndMessage = ::CreateDialog(
hInstance, // instance of the application
MAKEINTRESOURCE(IDMAINDLG), // dialog template
NULL, // parent to the desktop
WndProc); // our message handling
ShowWindow(hwndMessage, SW_SHOWNORMAL);
return(hwndMessage);
}
system tray අයිකනය රයිට් ක්ලික් කිරීම මගින් ඇතිවන messages handle කිරීම සදහා dialog box එකක් සාදයි.
BOOL SysTrayCMD(UINT nAction)
{
// start filling the icon data
nid;
::ZeroMemory(&nid, sizeof(nid));
nid.cbSize = sizeof(nid); // size of struct
nid.hWnd = g_hWndMessages; // window to get messages
nid.uCallbackMessage = g_nMsgIcon; // message ID from icon
nid.hIcon = g_hIconTray; // icon to place in the tray
nid.uID = 1; // ID of icon
::strcpy(nid.szTip, "Program Blocker 1.0"); // tip
nid.uFlags = NIF_ICON|NIF_TIP|NIF_MESSAGE; // valid fields
return(::Shell_NotifyIcon(nAction, &nid));
}
sys tray අයිකනයෙහි properties සකස් කර එය system tray එකට ඇඩ් කරයි.
void ShowSystrayMenu(HWND hwnd)
{
// load the top-level menu and fetch the first
// submenu, then destroy it
HMENU hmenu = ::GetSubMenu(g_hMenuTop, 0);
// make current window foreground
::SetForegroundWindow(hwnd);
// grab the point of right-click and pop up the menu
POINT pt;
::GetCursorPos(&pt);
if(::TrackPopupMenu(
hmenu, // menu to pop up
TPM_RIGHTALIGN, // align the popup
pt.x, pt.y, // point to popup menu from
0, // must be 0
hwnd, // owner
NULL)) // ignored
{
// force task switch to owner application
::PostMessage(hwnd, WM_USER, 0, 0);
}
}
systray අයිකනය මත රයිට් ක්ලික් කළ විට මෙනුවක් පෙන්වයි.
BOOL CALLBACK WndProc(HWND hwnd, UINT nMsg,
WPARAM wParam, LPARAM lParam)
{
/*** Systray notification MSG handling ***/
if(nMsg==g_nMsgIcon)
{
if(lParam==WM_RBUTTONUP)
{
// display the context menu
ShowSystrayMenu(hwnd);
}
}
/**** COMMAND MSG handling ****/
else if(nMsg==WM_COMMAND)
{
// Systray - Add program block
if(LOWORD(wParam)==ID_ICON_ADPROGBLK)
{
Beep(1000,1000);
}
}
return (FALSE);
}
systray මෙනූ අයිතමයක් සිලෙක්ට් කළ විට සිදුවිය යුතු දෑ සිදු කරයි (systray message handling).
දැන් WinMain තුළ පහත දැක්වෙන කෝඩ් එක ටයිප් කරන්න:
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_hWndMessages = ::CreateMessageWindow(hInstance);
g_hIconTray = ::LoadIcon(hInstance, MAKEINTRESOURCE(IDSTICON));
g_hMenuTop = ::LoadMenu(hInstance,
MAKEINTRESOURCE(IDSTMENU));
if(SysTrayCMD(NIM_ADD))
{
// simple message loop that dispatches until user quits
MSG msg;
while (::GetMessage(&msg, NULL, 0, 0)) // (returns 0 on WM_QUIT)
{
// make sure to let the dialog message loop handle
// dlg wnd messages
if(!::IsDialogMessage(g_hWndMessages, &msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg); // (dispatches msgs to WndProc())
}
}
}
systray messages හැන්ඩ්ල් කිරීම සදහා වින්ඩෝවක් සාදයි.
systray අයිකනය සහ මෙනුව ලෝඩ් කරයි.
දැන් මෙම වැඩසටහන් ක්රියාක්මක කළ විට ඩයලොග් බොක්සයක් විවෘත වන අතර, System tray අයිකනයක් පෙන්වයි. එය රයිට් ක්ලික් කල විට මෙනුවක් මතු වන අතර, එයින් ඔබට අයිතමයක් සිලෙක්ට් කළ හැකිය.
No comments:
Post a Comment