vc++開發簡單的半透明窗口程序示例

環境:xp  vc6.0


1: 打開vc,依次點擊 文件 -> 新建 -> 工程 -> MFC AppWizard {exe} ,填寫工程名test  -> 確定 -> 基本對話框 -> 完成

2: 打開 Test2Dlg.cpp 找到 OnInitDialog()函數,註釋掉函數中的內容,

   添加以下代碼:

        //加入WS_EX_LAYERED擴展屬性
	SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
	GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
	HINSTANCE hInst = LoadLibrary("User32.DLL");
	if(hInst)
	{
		typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
		MYFUNC fun = NULL;
		//取得SetLayeredWindowAttributes函數指針
		fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
		if(fun){
			fun(this->GetSafeHwnd(),0,128,2);//實現半透明效果
		}
		FreeLibrary(hInst);
	}
最後函數OnInitDialog()的代碼段爲

BOOL CTestDlg::OnInitDialog()
{
	
	//加入WS_EX_LAYERED擴展屬性
	SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
	GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
	HINSTANCE hInst = LoadLibrary("User32.DLL");
	if(hInst)
	{
		typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
		MYFUNC fun = NULL;
		//取得SetLayeredWindowAttributes函數指針
		fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
		if(fun){
			fun(this->GetSafeHwnd(),0,128,2);//實現半透明效果
		}
		FreeLibrary(hInst);
	}



	/*
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	*/
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

3:編譯運行,結果截圖如下



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章