创建打开对话框,读取文件路径

CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,L"All Files(*.*)|*.*|",AfxGetMainWnd());
CString strPath;
if(dlg.DoModal()==IDOK)
{
strPath=dlg.GetPathName();
MessageBox(strPath);
}

 

窗口抖动

void CHelloDlg::OnButton1()
{
CRect rect;
this->GetWindowRect(&rect);
int off=10;
for (int i=1;i<=4;i++)
{
rect.OffsetRect(off,FALSE);
this->MoveWindow(&rect,TRUE);
if (off==10)
{
off=-10;
}
else
{
off=10;
}
::Sleep(50);
}
}

 

窗口淡入淡出

#include <winuser.h>

为了在程序中使用该函数,就得对其头文件进行一些小小的修改,可以在工程中的StdAfx.h文件靠前的位置加上如下定义,不添加如下定义AW_BLEND提示没定义:

#undef WINVER
#define WINVER 0x500

在OnInitDialog()中添加

CenterWindow();         //创建窗口,使窗口居中
DWORD dwStyle = AW_BLEND;
HINSTANCE hinst=LoadLibrary("user32.dll");    //载入动态库
typedef BOOL(WINAPI MYFUNC(HWND,DWORD,DWORD));  //定义函数类型
MYFUNC* AnimateWindow; //定义函数指针
AnimateWindow=(MYFUNC*)::GetProcAddress(hinst,"AnimateWindow"); //获收函数地址
AnimateWindow(this->m_hWnd,500,dwStyle);  //设置动画窗体
FreeLibrary(hinst);

 

窗体绘制背景

在OnPaint()中添加如下:

CPaintDC dc(this);  //窗体DC
CBitmap m_bitmap; //位图变量
m_bitmap.LoadBitmap(IDB_LOGO); //载入位图资源
CDC memdc;//临时DC
memdc.CreateCompatibleDC(&memdc); //创建临时DC
memdc.SelectObject(&m_bitmap);//选中位图
int width,height; //定义位置宽度和高度
BITMAP bmp; 
m_bitmap.GetBitmap(&bmp); //获取位置信息
width=bmp.bmWidth; //位图宽度
height=bmp.bmHeight; //位图高度
CRect rect;
this->GetClientRect(&rect); //获取窗体客户区大小
dc.StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&memdc,0,0,width,height,SRCCOPY); //将位图绘制在位图上面作为背景

 

圆角窗体

在OnInitDialog()中添加

CRect rect;
this->GetWindowRect(&rect);
HRGN rgn=CreateRoundRectRgn(0,0,rect.Width(),rect.Height(),30,30);
SetWindowRgn(rgn,FALSE);

 

绘制窗体背景渐变色

在OnPaint()中添加

CPaintDC dc(this);
CRect rect;
CBrush brush;
this->GetClientRect(&rect);

int x,y;
for (int m=255;m>0;m--)
{
x=rect.Width()*m/255;
y=rect.Height()*m/255;
brush.DeleteObject();
brush.CreateSolidBrush(RGB(255,m,0));
dc.FillRect(CRect(0,0,x,y),&brush);
}

 

动态改变标题图标

HICON hIcon;
hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);
SetIcon(hIcon,TRUE); //设置大图标
SetIcon(hIcon,FALSE); //设置小图标

 

窗口最大化、最小化

PostMessage(WM_SYSCOMMAND,SC_MAXIMIZE);

PostMessage(WM_SYSCOMMAND,SC_MINIMIZE);