Chinaunix首页 | 论坛 | 博客
  • 博客访问: 263404
  • 博文数量: 28
  • 博客积分: 688
  • 博客等级: 上士
  • 技术积分: 365
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-16 10:25
文章分类

全部博文(28)

文章存档

2012年(28)

我的朋友

分类: WINDOWS

2012-05-23 10:30:11

CreateWindowEx创建窗口程序 #include
#include

LRESULT CALLBACK WinDouProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);


class CWnd
{
public:
CWnd()
{
m_hWnd = NULL;
}
BOOL CreateEx(
DWORD dwExStyle, // extended window style
LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // handle to menu or child-window identifier
HANDLE hInstance, // handle to application instance
LPVOID lpParam // pointer to window-creation data
);
BOOL ShowWindow( int nCmdShow );
BOOL UpdateWindow();
public:
HWND m_hWnd;
};


BOOL CWnd::CreateEx(
DWORD dwExStyle, // extended window style
LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // handle to menu or child-window identifier
HANDLE hInstance, // handle to application instance
LPVOID lpParam // pointer to window-creation data
)
{
m_hWnd = ::CreateWindowEx (dwExStyle,lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,(HINSTANCE)hInstance,lpParam);

if(m_hWnd != NULL)
return TRUE;
else
return FALSE;
}

BOOL CWnd::ShowWindow(int nCmdShow)
{
return ::ShowWindow(m_hWnd,nCmdShow);
}

BOOL CWnd::UpdateWindow()
{
return ::UpdateWindow(m_hWnd);
}

int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
)
{
WNDCLASS wndclass; //先设计窗口类
wndclass.cbCl***tra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
wndclass.hCursor = LoadCursor(NULL,IDC_HELP);
wndclass.hIcon = LoadIcon(NULL,IDI_WARNING);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WinDouProc;
wndclass.lpszClassName = "Magic_Maggie";
wndclass.lpszMenuName = 0;
wndclass.style = CS_VREDRAW | CS_HREDRAW;
//某一个变量原油几个变量去掉一个特征,可以用取反(~)后再进行与(&)
//例如:style上去掉CS_NOCLOSE,可以style&~CS_NOCLOSE;
RegisterClass(&wndclass); ///注意先建立再注册昂
CWnd wnd;
wnd.CreateEx(NULL,"Magic_Maggie","DouDou",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
wnd.ShowWindow(SW_SHOWNORMAL);
wnd.UpdateWindow();


MSG msg; //消息循环
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg); //触发WinDouProc
}
return 0;
}


LRESULT CALLBACK WinDouProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_LBUTTONDOWN:
MessageBox(hwnd,"您按下了鼠标左键昂","豆豆的程序",MB_OK);
HDC hdc;
hdc = GetDC(hwnd);
//The GetDC function retrieves a handle to a display device context for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the device context.
TextOut(hdc,0,0,"感谢您对豆豆程序的支持昂",strlen("感谢您对豆豆程序的支持昂"));
ReleaseDC(hwnd,hdc);
break;

case WM_CHAR:
char szChar[20];
sprintf(szChar,"Char is %d",wParam);
MessageBox(hwnd,szChar,"豆豆的程序",MB_OK);
break;

case WM_PAINT:
PAINTSTRUCT ps;
HDC hDc;
hDc = BeginPaint(hwnd,&ps);
TextOut(hDc,0,0,"这个是重绘滴哦",strlen("这个是重绘滴哦"));
EndPaint(hwnd,&ps);
break;

case WM_CLOSE: //这个case与下边的destroy这个case不要弄错了,否则窗口不出现,但任务管理器中运行
if(IDYES == MessageBox(hwnd,"您真的要退出么?","豆豆的程序",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;

case WM_DESTROY:
PostQuitMessage(0);
//////////////////////////////////////////?????????????????????
break;

default:
return DefWindowProc(hwnd,uMsg,wParam,lParam); // 别忘记了return

}
return 0;
}

每天都要点滴提高 之 总结篇

  • 应该建立Win32 Application程序,而不是Win32 Console Application程序。
  • m_hWnd = ::CreateWindowEx(dwExStyle,lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,(HINSTANCE)hInstance,lpParam);

    加一个强制类型转换就不会报错误说:error C2664: 'CreateWindowExA' : cannot convert parameter 11 from 'void *' to 'struct HINSTANCE__ *'

  • 带Afx的是应用程序框架函数,相当于全局函数

  • 如果类中要调用SDK函数,并且这个SDK函数与类名同,编译会出现错误。要加::

  • CWnd wnd;其中wnd不是窗口,关闭窗口时,对象并没有销毁,窗口的生命周期与对象的不同。然而对象如果销毁了,窗口(资源)也就没有了。

阅读(1801) | 评论(0) | 转发(0) |
0

上一篇:MFC好书推荐

下一篇:内联函数的使用

给主人留下些什么吧!~~