消息对话框 有模式 和 无模式的 静态框 点击事件等14.4.22

#include "stdafx.h"    
#include "resource.h"
HINSTANCE g_hInst = NULL;    
    
void onPaint(HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam){    
    /*PAINTSTRUCT ps={0};    
    HDC hDC=BeginPaint(hWnd,&ps); 

    EndPaint(hWnd,&ps);    
    */
} 
    
void onCreate(HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam){    
	//
	CreateWindow("STATIC","Hello Static",
		WS_CHILD|WS_VISIBLE|SS_SUNKEN|SS_NOTIFY,50,50,100,100,hWnd,(HMENU)10001,g_hInst,NULL);
    //加载图标到静态框  #103是资源图标的ID号 并且要加上SS_ICON  加上SS_NOTIFY表示有点击事件
	CreateWindow("STATIC","#103",
		WS_CHILD|WS_VISIBLE|SS_ICON|SS_SUNKEN|SS_NOTIFY,
		200,50,100,100,hWnd,(HMENU)10002,g_hInst,NULL);
}
//获取用户操作事件  相当于WndProc
BOOL CALLBACK SettingDlgProc(HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
{
	//wParam  是指当前接受输入焦点子控件窗口句柄
	//lParam  创建时附带的参数
	switch (nMsg){
	case WM_SYSCOMMAND:
		{
			switch(wParam){
			case SC_CLOSE:
				EndDialog(hWnd,1);
				break;
			}
		break;
	//在对话框里面没有CREATE事件 取代它的是INITDIALOG
	case WM_CREATE:
	  MessageBox(NULL,"WM_CREATE","WM_CREATE",MB_OK);
		break;
	//在显示对话框之前执行该操作 更有模式和无模式没关系
	case WM_INITDIALOG:
		MessageBox(NULL,"INITDIALOG","INITDIALOG",MB_OK);
		break;
		}
	}
	return 0;
	//也可以直接返回0 ,无模式下我们更喜欢用上面的那种返回
	//return DefDlgProc(hWnd,nMsg,wParam,lParam);
}
//
void Setting( HWND hWnd )
{	
	//加载对话框资源
	int nRet = DialogBox( g_hInst, 
		MAKEINTRESOURCE(IDD_SETING), hWnd, 
		SettingDlgProc );
	if(1==nRet){
		MessageBox(NULL,"取消","WinDlg",MB_OK);
	}else{
		MessageBox(NULL,"设置配置窗口","WinDlg",MB_OK);
	}
}
//无模式窗口  鼠标可以点击下层窗口焦点
//有模式 和无模式的区别 一个可以阻塞 禁止了其他窗口的输入  一个不阻塞
void noMode(HWND hWnd){
	HWND dHWND=CreateDialog(g_hInst,MAKEINTRESOURCE(IDD_NOMODE),hWnd,SettingDlgProc);
	//创建好了 必须调用显示 有模式的可以不用
	ShowWindow(dHWND,SW_SHOW);
}
void onCommand(HWND hWnd,    
                           UINT nMsg,    
                           WPARAM wParam,    
                           LPARAM lParam ){
	int nCmd=LOWORD(wParam);
	int nCmdL=HIWORD(wParam);
	switch(nCmd){
	case 1001:
		PostQuitMessage(0);
		break;
	//无
	case 1003:
		noMode(hWnd);
		break;
	case 1004:
                //加载一个图标
                //HICON hIcon=LoadIcon(g_hInst,MAKEINTRESOURCE(IDI_STATI2),STM_SETICON)
                //吧图标加载到静态框里面 对应的是他的ID号GetDlgite是获取指定ID的窗口句柄
                //HWND hStatic=GetDlgite(hWnd,1002);
                //给静态框发送一个消息
                //SendMessage(hStatic,STM_SETICON,(WPARAM)hIcon,0);
		Setting(hWnd);
		break;
	//获取到静态框的ID
	case 10001:
	case 10002:
		switch(nCmdL){
		//在静态框上的点击事件
		case STN_CLICKED:
			MessageBox(NULL,"STATIC 静态框","static",MB_OK);
			break;
		}
		break;
	}
	
}
    
//主窗口的窗口处理函数    
LRESULT CALLBACK WndProc( HWND hWnd,    
                           UINT nMsg,    
                           WPARAM wParam,    
                           LPARAM lParam )    
{    
    switch( nMsg )    
    { 
	case WM_CREATE:
		onCreate(hWnd, nMsg, wParam, lParam);
		break;
    case WM_PAINT:    
        //onPaint(hWnd, nMsg, wParam, lParam);    
        break;    
    case WM_DESTROY:    
        PostQuitMessage( 0 );    
        return 0; 
	case WM_COMMAND:
		onCommand(hWnd, nMsg, wParam, lParam);
		break;

    }    
    return DefWindowProc( hWnd, nMsg, wParam, lParam );    
}    
//窗口注册函数    
BOOL RegisterWnd( LPSTR   pszClassName,int    nBrush )    
{    
    WNDCLASSEX wce = { 0 };    
    wce.cbSize     = sizeof( wce );    
    wce.style      = CS_HREDRAW|CS_VREDRAW;    
    wce.cbClsExtra = 0;    
    wce.cbWndExtra = 0;    
    wce.lpfnWndProc= WndProc;    
    wce.hInstance  = g_hInst;    
    wce.hCursor    = NULL;    
    wce.hIcon      = NULL;    
    wce.hbrBackground = HBRUSH(nBrush);    
    wce.lpszClassName = pszClassName;    
    wce.lpszMenuName  = NULL;    
    wce.hIconSm       = NULL;    
    
    ATOM nAtom = RegisterClassEx( &wce );    
    if( nAtom == 0 )    
    {    
        return FALSE;    
    }    
    return TRUE;    
}    
//创建主窗口    
HWND CreateWnd( LPSTR pszClassName )    
{    
	//创建菜单
    HMENU hMenu=CreateMenu();
	HMENU hPopMenu=CreatePopupMenu();
	HMENU hPopMenuBox=CreatePopupMenu();
	AppendMenu(hPopMenu,MF_STRING,1001,"退出");
	AppendMenu(hMenu,MF_STRING|MF_POPUP,(UINT)hPopMenu,"文件");

	AppendMenu(hPopMenuBox,MF_STRING,10003,"(无)弹出窗口");
	AppendMenu(hPopMenuBox,MF_STRING,10004,"(有)弹出窗口");
	AppendMenu(hMenu,MF_STRING|MF_POPUP,(UINT)hPopMenuBox,"消息");

    HWND hWnd = CreateWindowEx( 0,    
        pszClassName, "MainWnd",     
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,    
        CW_USEDEFAULT, CW_USEDEFAULT,    
        CW_USEDEFAULT, NULL, hMenu, g_hInst,    
        NULL );    
    return hWnd;    
}    
//消息循环    
void Message( )    
{    
    MSG msg = { 0 };    
    while( GetMessage( &msg, NULL, 0, 0 ) )    
    {    
        TranslateMessage( &msg );    
        DispatchMessage( &msg );    
    }    
}    
//显示窗口    
void DisplayWnd( HWND hWnd )    
{    
    ShowWindow( hWnd, SW_SHOW );    
    UpdateWindow( hWnd );    
}    
int APIENTRY WinMain(HINSTANCE hInstance,    
                     HINSTANCE hPrevInstance,    
                     LPSTR     lpCmdLine,    
                     int       nCmdShow)    
{    
    // TODO: Place code here.    
    g_hInst = hInstance;    
    RegisterWnd("luoqiang",COLOR_BACKGROUND);    
    DisplayWnd(CreateWnd("luoqiang"));    
    Message();    
    return 0;    
}    

你可能感兴趣的:(C++)