Showing posts with label WinApi. Show all posts
Showing posts with label WinApi. Show all posts

[WinAPI]計算繪圖文字長度

目前是使用GetTextExtentPoint32()來取得文字長度。

回傳的參數為文字的長度跟高度,單位為Pixel。

測試結果之後再補上。

[WIN]變更程式執行優先順序

如有需要變更執行檔的執行優先順序,可呼叫SetPriorityClass Function()來達成需求。

第一個參數是目前程序的HANDLE,第二個參數則為要設定的順序參數。

目前測試結果是有效~~XDD

[WINAPI]取得磁碟空間

可使用GetDiskFreeSpaceEx()來取得目前可使用的磁碟空間、總磁碟空間以及總剩餘磁碟空間。

使用方式如下:


    char szDiskDir[MAX_PATH]  = "C:\\";

    //A pointer to a variable that receives the total number of free bytes on a disk that are available to the user who is associated with the calling thread.
    //This parameter can be NULL.
    //If per-user quotas are being used, this value may be less than the total number of free bytes on a disk.
    __int64 n64AvailableFreeDiskBytes;       

    //A pointer to a variable that receives the total number of bytes on a disk that are available to the user who is associated with the calling thread.
    //This parameter can be NULL.
    //If per-user quotas are being used, this value may be less than the total number of bytes on a disk.
    //To determine the total number of bytes on a disk or volume, use IOCTL_DISK_GET_LENGTH_INFO.        
    __int64 n64TotalDiskBytes;

    //A pointer to a variable that receives the total number of free bytes on a disk.
    //This parameter can be NULL.
    __int64 n64TotalFreeDiskBytes;

    if ( !::GetDiskFreeSpaceEx(szDiskDir, (PULARGE_INTEGER) &n64AvailableFreeDiskBytes, (PULARGE_INTEGER) &n64TotalDiskBytes,
                                                            (PULARGE_INTEGER) &n64TotalFreeDiskBytes) )
    {
        printf("Fail to get disk fress space in %s (Err = %d).\n", szDiskDir, ::GetLastError());
    }else{
        printf("%s has %lld bytes available free disk.\nTotal disk size is %lld bytes.\nTotal free space is %lld bytes.\n", 
                    szDiskDir, n64AvailableFreeDiskBytes, n64TotalDiskBytes, n64TotalFreeDiskBytes);
    }

Dialog隱藏後不在task tray顯示

程式最小化至系統列的測試中發現即使程式最小化之後,還是會存在task tray內,這樣看起來是有點怪。因此需要在最小化時,另外去處理。

所需用到的Api為SetWindowLongPtr(MSDN上說明這樣在32與64平台間比較有相容性)
LONG_PTR SetWindowLongPtr(  
HWND hWnd,
int nIndex,
LONG_PTR dwNewLong
);

以及GetWindowLongPtr
LONG_PTR GetWindowLongPtr(  
HWND hWnd,
int nIndex
);

要不顯示於task tray的時候需用
SetWindowLongPtr(hWnd, GWL_EXSTYLE,GetWindowLong(hWnd, GWL_EXSTYLE) & ~WS_EX_TOOLWINDOW);

如需還原時則可使用
SetWindowLongPtr(hWnd, GWL_EXSTYLE,GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_TOOLWINDOW);

這邊所使用的參數WS_EX_TOOLWINDOW據MSDN的說法,可以讓ALT+TAB的時候也看不到程式,但測試的結果是失敗的,所以看來還需要其他方法才能達成吧。
(結果上面那段寫完之後,竟然成功的在ALT+TAB裡面看不到隱藏的程式,這可真是神奇阿XDD)
參數部分可參考CreateWindowEX裡的dwExStyle。

最後完成的code如下:
LRESULT CMainDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch ( message )
{
case WM_USER: //NOTE:滑鼠移到上面就會送這個進來了
if ( lParam == WM_LBUTTONDBLCLK )
{
SetWindowLongPtr(hWnd, GWL_EXSTYLE,this->ShowWindow(SW_SHOW);
}else if ( lParam == WM_RBUTTONDOWN ) {
::AfxMessageBox("OnMessage_Right button");
}
break;
case WM_SYSCOMMAND: //NOTE:OnCancel會進來到這邊
if ( wParam == SC_MINIMIZE || wParam == SC_CLOSE)
{
if ( wParam == SC_CLOSE )
{
SetWindowLongPtr(hWnd, GWL_EXSTYLE, this->ShowWindow(SW_HIDE);
this->GotoSystemTray();
}
}
break;
}

return CDialog::WindowProc(message, wParam, lParam);
}

Reference:http://delphi.ktop.com.tw/board.php?cid=168&fid=912&tid=74287

程式最小化至系統工具列

所需要用到的API為Shell_NotifyIcon
BOOL Shell_NotifyIcon(
DWORD dwMessage,
PNOTIFYICONDATA lpdata
);

傳入的Message有NIM_ADD、NIM_MODIFY、NIM_DELETE、NIM_SETFOCUS、NIM_SETVERSION
其所需傳入的結構為 NOTIFYICONDATA
typedef struct _NOTIFYICONDATAA{
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessage;
HICON hIcon;
#if (NTDDI_VERSION < NTDDI_WIN2K)
TCHAR szTip[64];
#endif
#if (NTDDI_VERSION >= NTDDI_WIN2K)
TCHAR szTip[128];
DWORD dwState;
DWORD dwStateMask;
TCHAR szInfo[256];
union{
UINT uTimeout;
UINT uVersion; // Used with Shell_NotifyIcon flag NIM_SETVERSION.
} DUMMYUNIONNAME;
TCHAR szInfoTitle[64];
DWORD dwInfoFlags;
#endif
#if (NTDDI_VERSION >= NTDDI_WINXP)
GUID guidItem;
#endif
#if (NTDDI_VERSION >= NTDDI_VISTA)
HICON hBalloonIcon;
#endif
}

如有需要針對滑鼠點擊System tray裡面的Icon反應的話,則需在uCallbackMessage給定回傳之訊息。

之後再於
LRESULT CDialog::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
裡面去接收所需要的資訊並作相對應的動作即可

Sample-縮到System Tray:
bool CMainDlg::GotoSystemTray()
{
NOTIFYICONDATA NotifyIconData;

NotifyIconData.cbSize = sizeof (NOTIFYICONDATA);
NotifyIconData.hWnd = this->m_hWnd;
NotifyIconData.uID = IDD_MAINDLG;
NotifyIconData.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
NotifyIconData.hIcon = hIcon;
strcpy(NotifyIconData.szTip, "This is tip!!");
NotifyIconData.dwState = NIS_SHAREDICON;
strcpy(NotifyIconData.szInfo, "This is Info!!");
strcpy(NotifyIconData.szInfoTitle,"This is Info title!!");
NotifyIconData.dwInfoFlags = NIIF_INFO;
NotifyIconData.uCallbackMessage = WM_USER;


if ( !::Shell_NotifyIcon(NIM_ADD, &NotifyIconData) )
{
CString cstrDis;
cstrDis.Format("Err = %d", ::GetLastError());
::AfxMessageBox(cstrDis);
return false;
}

return true;
}

WinProc code:
LRESULT CMainDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch ( message )
{
case WM_USER: //NOTE:滑鼠移到上面就會送這個進來了
if ( lParam == WM_LBUTTONDBLCLK )
{
::AfxMessageBox("OnMessage_Left button");
}else if ( lParam == WM_RBUTTONDOWN ) {
::AfxMessageBox("OnMessage_Right button");
}
break;
case WM_SYSCOMMAND: //NOTE:OnCancel會進來到這邊
if ( wParam == SC_MINIMIZE || wParam == SC_CLOSE)
{
::AfxMessageBox("OnSystem bar");
}
break;
}

return CDialog::WindowProc(message, wParam, lParam);
}

Reference:http://blog.chinaunix.net/u2/67530/showart_603037.html

Build docker image from multiple build contexts

Build docker image from multiple build contexts ...