VC常用技術新編
以下技術,隻提供函數名和簡單應用事例。詳細內容參看MSDN,事例中的代碼如無特別說明版權歸microsoft所有
1:判斷窗口最小化最大化狀態
最小化到任務欄使用IsIconic()函數判斷
最大化通過IsZoomed()函數判斷
GetWindowPlacement(),調用此函數可以獲取窗口的正常狀態,最大化,最小化。通過WINDOWPLACEMENT結構體進行判斷。
2:一個應用程序隻有一個實例
1:使用FindWindow函數,如果發現指定創建已經存在,就退出
FindWindow第一個參數為查找窗口的類名。
FindWindow第二個參數為查找窗口的標題名(最好選擇這個)
2:使用Mutex互斥體
// Create mutex, because there cannot be 2 instances for same application
HANDLE hMutex = CreateMutex(NULL, FALSE, "ugg");
// Check if mutex is created succesfully
switch(GetLastError())
{
case ERROR_SUCCESS:
// Mutex created successfully. There is no instance running
break;
case ERROR_ALREADY_EXISTS:
// Mutex already exists so there is a running instance of our app.
return FALSE;
default:
// Failed to create mutex by unknown reason
return FALSE;
}
3:改變窗口或者控件大小和移動窗口或者控件位置
1:MoveWindow函數,改變窗口的大小和位置
CRect rc;
GetClientRect(&rc);
m_Button.MoveWindow(0, 0, rc.Width(), rc.Height());
2:SetWindowPos函數,可以應用Z軸方向上,改變重疊控件的顯示次序。
void CWinApp::HideApplication()
{
//m_pMainWnd is the main application window, a member of CWinApp
ASSERT_VALID(m_pMainWnd);
// hide the application's windows before closing all the documents
m_pMainWnd->ShowWindow(SW_HIDE);
m_pMainWnd->ShowOwnedPopups(FALSE);
// put the window at the bottom of z-order, so it isn't activated
m_pMainWnd->SetWindowPos(&CWnd::wndBottom, 0, 0, 0, 0,
SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
}
4:改變窗口的最大化最小化正常狀態
1:SetWindowPlacement函數
設置窗口最大化最小化顯示狀態,通過WINDOWPLACEMENT結構體進行設置
2:POST,Send這三種SC_MAXIMIZE,SC_MINIMIZE,SC_RESTORE命令消息
PostMessage(WM_SYSCOMMAND, SC_MAXIMIZE,0);// 最大化
PostMessage(WM_SYSCOMMAND, SC_ MINIMIZE,0);// 最小化
PostMessage(WM_SYSCOMMAND, SC_RESTORE,0);// 正常
5:更改窗口或者控件的注冊類名
在默認的情況下,MFC的窗口或者控件的注冊類名是MFC自動生成,比如Dialog的注冊類名為”#32770”,CButton的注冊類名為”Button”等,在一些hook應用中,我們可能需要修改這些注冊類名。可以采用如下方法
重載窗口或者控件的PreCreateWindows函數,在函數內做如下修改
BOOL CMyDilaolg::PreCreateWindow(CREATESTRUCT& cs)
{
WNDCLASS wndcls;
ZeroMemory( &wndcls, sizeof(WNDCLASS) );
wndcls.style = CS_DBLCLKS;
wndcls.lpfnWndProc = AfxWndProc;
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.hIcon = AfxGetApp()->LoadIcon( IDI_APPLICATION );
wndcls.hCursor = AfxGetApp()->LoadStandardCursor( IDC_ARROW );
wndcls.hbrBackground = NULL;
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = _T("uggDialog");
AfxRegisterClass( &wndcls );
cs.lpszClass = wndcls.lpszClassName; // 設置控件的注冊類名
return CDialog::PreCreateWindow( cs );
}
最後更新:2017-04-02 00:06:17