返回介绍

1 C

2 C++

3 Windows

4 Linux

5 数据库

6 数据结构

7 算法

8 Qt

9 AS400

10 Web

12 C

13 框架

14 协议

15 工具

17 QA

3.1.2 Win32_Code

发布于 2023-10-02 20:38:18 字数 109919 浏览 0 评论 0 收藏 0

// 窗口搭建**

// wincreate.cpp : Defines the entry point for the application.

\#include "stdafx.h"

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​    switch ( nMsg )

​    {

​    case WM_DESTROY:

​        PostQuitMessage( 0 );//能够使getMessage返回0

​        break;

​    }

​    return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​    WNDCLASSEX wce = { 0 };

​    wce.cbSize = sizeof( wce ); //结构体的大小

​    wce.cbClsExtra = 0;//窗口类缓冲区

​    wce.cbWndExtra = 0;//窗口缓冲区

​    wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色

​    wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标

​    wce.hIcon = NULL;//窗口大图标句柄

​    wce.hIconSm = NULL;//窗口小图标句柄

​    wce.hInstance = g_hInstance;//当前模块句柄

​    wce.lpfnWndProc = wndproc;//窗口处理函数

​    wce.lpszClassName = lpClassName;//窗口类名称

​    wce.lpszMenuName = NULL;//菜单名

​    wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​    RegisterClassEx( &wce );//注册,向系统写入

​    

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​    HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​        CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,

​        NULL );

​    return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​    ShowWindow( hWnd,SW_SHOW );

​    UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​    MSG uMsg = { 0 };

​    while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​    {

​        TranslateMessage( &uMsg ); // 翻译,分析

​        DispatchMessage( &uMsg ); // 调度,派发

​    }

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​    g_hInstance = hInstance;

​    Register("Main",WndProc);

​    HWND hWnd = CreateMain( "Main","Windows");

​    Display( hWnd);

​    Message();

​    return 0;

}

**//窗口附加数据、子窗口**

// wincreate.cpp : Defines the entry point for the application.

//

\#include "stdafx.h"

\#include "stdio.h"

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

//窗口处理函数(处理消息)

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       switch ( nMsg )

​       {

​       case WM_SYSCOMMAND:

​              MessageBox( hWnd, "WM_SYSCOMMAND", "Infor",MB_OK );

​              break;

​       case WM_DESTROY:

​              PostQuitMessage( 0 );//能够使getMessage返回0

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );//默认处理函数

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 200;//窗口类缓冲区

​       wce.cbWndExtra = 200;//窗口缓冲区

​       wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色

​       wce.hCursor = NULL;//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = NULL;//菜单名

​       wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​       RegisterClassEx( &wce );// 向系统写入,注册

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​              CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,

​              NULL );

​       return hWnd;

}

//创建子窗口

HWND CreateChild( LPSTR lpClassName,LPSTR lpWndName,HWND hParent )

{

​       HWND hChild = CreateWindowEx( 0,lpClassName, lpWndName,

​              WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW,100,100,200,200,hParent,

​              NULL,g_hInstance,NULL);

​       return hChild;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG uMsg = { 0 };

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

}

//写入附加数据

void SetExtra( HWND hWnd )

{

​       char *pszText = "hello data";

​       SetClassLong( hWnd, 196, (LONG)pszText);

​       SetWindowLong( hWnd, 0, 1000);

}

//读入附件数据

void GetExtra( HWND hWnd )

{

​       LONG nClassExtra = GetClassLong(hWnd, 196);

​       char* pszText = (char*)nClassExtra;

​       LONG nWndExtra = GetWindowLong( hWnd, 0);

​       CHAR seText[256] = { 0 };

​       sprintf( seText, "窗口类:%s,窗口:%d\n",pszText,nWndExtra);

​       MessageBox( NULL, seText,"Infor", MB_OK ); //提示框(显示)

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       g_hInstance = hInstance;

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","WINDOWS");

​       Register( "CHILD",DefWindowProc);

​       HWND hChildl = CreateChild("CHILD", "C1", hWnd );

​       HWND hChild2 = CreateChild("CHILD", "C2", hWnd );

​       //窗口重绘

​       MoveWindow(hChildl, 300,100,200,200,TRUE);

​       MoveWindow(hChild2, 500,100,200,200,TRUE);

​       SetExtra( hChildl );

​       GetExtra( hChild2 );

​       Display( hWnd);     

​       Message();

​       return 0;

}

**//** **窗口消息**

\#include "stdafx.h"

\#include "stdio.h"

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

HANDLE g_hOutput = 0;//接收标准输出句柄

HWND g_hEdit = 0;//用于接收窗口句柄(MoveMessage())

\#define WM_MYMESSAGE WM_USER + 1001   //自定义消息(宏)

//WM_CREATE 消息(创建子窗口)

void OnCreate( HWND hWnd, LPARAM lParam)

{

​       CREATESTRUCT *pcs = (CREATESTRUCT*)lParam;

​       char* pszText = (char*)pcs->lpCreateParams;

​       MessageBox( hWnd,pszText, "Infor",MB_OK );

​       g_hEdit = CreateWindowEx( 0, "EDIT","HELLO",WS_CHILD|WS_VISIBLE|WS_BORDER,

​              0,0,200,200,hWnd,NULL,g_hInstance,NULL );

​       //向自定义消息发送

​       PostMessage( hWnd, WM_MYMESSAGE, 1,2 );

}

//WM_SIZE 消息

void OnSize(HWND hWnd, LPARAM lParam )

{

​       char szText[256] = { 0 };

​       sprintf( szText, "WM_SIZE:宽=%d,高=%d\n",LOWORD(lParam),HIWORD(lParam));

​       WriteConsole( g_hOutput,szText, strlen(szText),NULL,NULL );

​       MoveWindow( g_hEdit,0,0,LOWORD(lParam)-100,HIWORD(lParam)-100, TRUE );//重新绘制子窗口

​       //char *pszText = "WM_SIZE\n";

​       //WriteConsole( g_hOutput,pszText, strlen(pszText),NULL,NULL );

}

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       switch ( nMsg )

​       {

​       case WM_MYMESSAGE: //处理自定义消息

​              {

​                     char szText[256] = { 0 };

​                     sprintf( szText,"wParam=%d,lParam=%d",wParam,lParam );

​                     MessageBox( hWnd, szText, "Infor", MB_OK );

​              }

​       case WM_SIZE:

​              OnSize( hWnd, lParam);

​              break;

​       case WM_CREATE:

​              OnCreate( hWnd, lParam );

​              break;

​       case WM_DESTROY:

//            PostQuitMessage( 0 );//能够使getMessage返回0 //参数随意

//            SendMessage( hWnd, WM_QUIT, 0, 0 );

​              PostMessage( hWnd, WM_QUIT, 0, 0 );

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 0;//窗口类缓冲区

​       wce.cbWndExtra = 0;//窗口缓冲区

​       wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色

​       wce.hCursor = NULL;//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = NULL;//菜单名

​       wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​       RegisterClassEx( &wce );

​       

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       char *pszText = "hello data";

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​              CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,

​              NULL,NULL,g_hInstance,pszText );

​       return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG nMsg = { 0 };

/*

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

*/

​       while(1)

​       {

​              if(PeekMessage(&nMsg,0,NULL,NULL,PM_NOREMOVE))//侦查作用

​              {

​                     //有消息

​                     if(GetMessage( &nMsg,0,NULL,NULL))

​                     {

​                            TranslateMessage( &nMsg );

​                            DispatchMessage( &nMsg );

​                     }

​                     else

​                     {

​                            //WM_QUIT

​                            return;

​                     }

​              }

​              else

​              {

​                     //没消息

​                     //空闲处理

​                     WriteConsole( g_hOutput, "OnIdle",6,NULL,NULL);

​              }

​       }

}

//主函数

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       AllocConsole();//dos窗口

​       g_hOutput = GetStdHandle( STD_OUTPUT_HANDLE );//获得dos窗口的句柄

​       g_hInstance = hInstance;

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","WINDOWS");

​       Display( hWnd);

​       Message();

​       return 0;

}

**//****键盘消息**

\#include "stdafx.h"

\#include "stdio.h"

HANDLE g_hOutput = 0;//用于(dos)标准输出句柄

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

int g_xPos = 100; //记录坐标

int g_yPos = 100;

//     VM_PAINT 绘制消息

void OnPaint( HWND hWnd)

{

​       char *pszText = "WM_PAINT\n";

​       //WriteConsole( g_hOutput, pszText, strlen(pszText),NULL,NULL );

​       //绘图

​       PAINTSTRUCT ps = { 0 };

​       HDC hdc = BeginPaint( hWnd, &ps);

​       TextOut( hdc, g_xPos, g_yPos,"hello",strlen("hello"));//画字符串

​       EndPaint( hWnd, &ps );

}

//键盘消息(按下/松开)

void OnKeyDown( HWND hWnd, WPARAM wParam )

{

​       char szText[256]={0};

​       sprintf( szText, "WM_KEYDOWN:%08X\n",wParam);

​       WriteConsole( g_hOutput,szText,strlen(szText),NULL,NULL );

​       switch( wParam )

​       {

​       case VK_UP://虚拟键码

​              g_yPos--;

​              break;

​       case VK_DOWN://虚拟键码

​              g_yPos++;

​              break;

​       case VK_LEFT://虚拟键码

​              g_xPos--;

​              break;

​       case VK_RIGHT://虚拟键码

​              g_xPos++;

​              break;

​       }

​       InvalidateRect( hWnd,NULL, TRUE );//重画

}

void OnKeyUp( HWND hWnd, WPARAM wParam )

{

​       char szText[256]={0};

​       sprintf( szText, "WM_KEYUP:%08X\n",wParam);

​       WriteConsole( g_hOutput,szText,strlen(szText),NULL,NULL );

}

// WM_CHAR消息

void OnChar( HWND hWnd, WPARAM wParam )

{

​       char szText[256]={ 0 };

​       sprintf( szText, "WM_CHAR:%08X\n",wParam);

​       WriteConsole( g_hOutput,szText,strlen(szText),NULL,NULL );

}

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       switch ( nMsg )

​       {

​       case WM_CHAR:

​              OnChar( hWnd,wParam );

​              break;

​       case WM_KEYDOWN:

​              OnKeyDown( hWnd, wParam );

​              break;

​       case WM_KEYUP:

​              OnKeyUp( hWnd, wParam );

​              break;

​       case WM_LBUTTONDOWN://申明重绘消息

​              InvalidateRect( hWnd,NULL,TRUE );

​       case WM_PAINT: //重绘消息

​              OnPaint( hWnd );

​              break;

​       case WM_DESTROY:

​              PostQuitMessage( 0 );//能够使getMessage返回0从而可以退出

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 0;//窗口类缓冲区

​       wce.cbWndExtra = 0;//窗口缓冲区

​       wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色

​       wce.hCursor = NULL;//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = NULL;//菜单名

//     wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​       wce.style = 0;//0非强制重绘

​       RegisterClassEx( &wce );//注册,向系统写入

​       

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​              CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,

​              NULL );

​       return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG uMsg = { 0 };

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       AllocConsole();

​       g_hOutput = GetStdHandle( STD_OUTPUT_HANDLE );//调用dos窗口

​       g_hInstance = hInstance;//接收窗口句柄

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","WINDOWS");

​       Display( hWnd);

​       Message();

​       return 0;

}

**//****鼠标消息**

\#include "stdafx.h"

\#include "stdio.h"

HANDLE g_hOutput = 0;

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

int g_yPos = 100;

int g_xPos = 100;

//鼠标按键事件

void OnLButtonDown( HWND hWnd, WPARAM wParam,LPARAM lParam)

{

​       char szText[256] = { 0 };

​       sprintf( szText,"WM_LBUTTOONDOWN:按键状态=%08X, x=%d,y=%d\n",

​              wParam, LOWORD(lParam), HIWORD(lParam) );

​       WriteConsole( g_hOutput, szText, strlen(szText),NULL,NULL);

}

void OnLButtonUp( HWND hWnd, WPARAM wParam,LPARAM lParam)

{

​       char szText[256] = { 0 };

​       sprintf( szText,"WM_LBUTTOONUP:按键状态=%08X, x=%d,y=%d\n",

​              wParam, LOWORD(lParam), HIWORD(lParam) );

​       WriteConsole( g_hOutput, szText, strlen(szText),NULL,NULL);

}

//鼠标移动事件

void OnMouseMove( HWND hWnd, LPARAM lParam)

{

​       char szText[256] = { 0 };

​       sprintf( szText,"WM_MOUSEMoVE:x=%d,y=%d\n",LOWORD(lParam), HIWORD(lParam) );

//     WriteConsole( g_hOutput, szText, strlen(szText),NULL,NULL);

​       g_xPos = LOWORD(lParam);

​       g_yPos = HIWORD(lParam);

​       InvalidateRect( hWnd, NULL, TRUE );

}

//绘制函数

void OnPaint( HWND hWnd)

{

​       PAINTSTRUCT ps = { 0 };

​       HDC hdc = BeginPaint( hWnd, &ps);

​       TextOut( hdc,g_xPos,g_yPos,"hello",strlen("hello"));//画字符串

​       EndPaint( hWnd, &ps );

}

//鼠标双击事件

void OnLButtonDblClk( HWND hWnd )

{

​       char *pszText = "WM_LBUTTONDBLCLK\n";

​       WriteConsole( g_hOutput,pszText,strlen(pszText),NULL,NULL );

}

void OnMouseWheel( HWND hWnd, WPARAM wParam )

{

​       short nDelta = HIWORD( wParam );

​       char szText[256] = { 0 };

​       sprintf( szText, "偏移量:%d\n",nDelta );

​       WriteConsole( g_hOutput, szText,strlen(szText),NULL,NULL );

}

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       switch ( nMsg )

​       {

​       case WM_MOUSEWHEEL:

​              OnMouseWheel( hWnd, wParam );

​              break;

​       case WM_LBUTTONDBLCLK:

​              OnLButtonDblClk( hWnd );

​              break;

​       case WM_PAINT:

​              OnPaint( hWnd );

​              break;

​       case WM_MOUSEMOVE:

​              OnMouseMove( hWnd,lParam);

​              break;

​       case WM_LBUTTONDOWN:

​              OnLButtonDown( hWnd, wParam,lParam );

​              break;

​       case WM_LBUTTONUP:

​              OnLButtonUp( hWnd, wParam, lParam );

​              break;

​       case WM_DESTROY:

​              PostQuitMessage( 0 );//能够使getMessage返回0

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 0;//窗口类缓冲区

​       wce.cbWndExtra = 0;//窗口缓冲区

​       wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色

​       wce.hCursor = NULL;//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = NULL;//菜单名

​       wce.style = CS_VREDRAW | CS_VREDRAW | CS_DBLCLKS;//窗口风格

​       //wce.style = 0;

​       RegisterClassEx( &wce );//注册,向系统写入

​       

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​              CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,

​              NULL );

​       return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG uMsg = { 0 };

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       AllocConsole();

​       g_hOutput = GetStdHandle( STD_OUTPUT_HANDLE );

​       g_hInstance = hInstance;

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","WINDOWS");

​       Display( hWnd);

​       Message();

​       return 0;

}

**//****定时器消息**

\#include "stdafx.h"

\#include "stdio.h"

int g_xPos = 0;

int g_yPos = 0;

BOOL g_Right = TRUE;  //g_left = FALSE;

BOOL g_Bottom = TRUE;//g_Top = FALSE;

//int nRightRect = 110;

//int nBottomRect = 110;

//nRightRect = nLeftRect+60;

//nBottomRect = nTopRect+60;

HANDLE g_hOutput = 0;

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

//定时器处理函数

void OnTimer( HWND hWnd, WPARAM wParam )

{

//     char szText[256]={0};

//     sprintf( szText, "定时器:%d\n",wParam );

//     WriteConsole( g_hOutput, szText, strlen(szText),NULL,NULL );

​       //获得边框大小

​       RECT rc ={0};

​       GetClientRect( hWnd, &rc);

​       if(g_Right)

​       {

​              if( g_xPos+50 >= rc.right )

​              {

​                     g_Right = FALSE;

​              }

​              else

​              {

​                     g_xPos++;

​              }

​       }

​       else

​       {

​              if ( g_xPos <= rc.left )

​              {

​                            g_Right = TRUE;

​                     }

​              else

​              {

​                     g_xPos--;

​              }

​       }

​       if ( g_Bottom)

​       {

​              if ( g_yPos+50 <= rc.bottom)

​              {

​                     g_yPos++;

​              }

​              else

​              {

​                     g_Bottom = FALSE;

​              }

​       }

​       else

​       {

​              if ( g_yPos >= rc.top)

​              {

​                     g_yPos--;       

​              }

​              else

​              {

​                     g_Bottom = TRUE;

​              }

​       }

//     InvalidateRect( hWnd, NULL,TRUE);

​       InvalidateRect( hWnd, NULL,FALSE);

}

//自定义定时器处理函数

void CALLBACK TimerProc ( HWND hWnd, UINT nMsg,UINT idTimer,DWORD dwCur )

{

​       char szText[256] = { 0 };

​       sprintf( szText, "定时器处理函数处理:%d\n",idTimer);

​       WriteConsole( g_hOutput, szText,strlen(szText),NULL,NULL );

}

//绘图函数

void OnPaint( HWND hWnd)

{

​       PAINTSTRUCT ps = { 0 };

​       HDC hdc = BeginPaint( hWnd,&ps);

​       Ellipse ( hdc, g_xPos,g_yPos,g_xPos+50,g_yPos+50 );

​       EndPaint ( hWnd, &ps);

}

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       switch ( nMsg )

​       {

​       case WM_LBUTTONDOWN:

​              KillTimer( hWnd ,1);//左键停止

​              break;

​       case WM_PAINT:

​              OnPaint( hWnd );

​              break;

​       case WM_TIMER://触发定时器

​              OnTimer( hWnd, wParam );

​              break;

​       case WM_CREATE:

​              {

​                     //放定时器

​                     SetTimer( hWnd, 1, 10, NULL );

​       //            SetTimer( hWnd, 2, 2000, TimerProc );

​                     break;

​              }

​       case WM_DESTROY:

​              PostQuitMessage( 0 );//能够使getMessage返回0

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 0;//窗口类缓冲区

​       wce.cbWndExtra = 0;//窗口缓冲区

​       wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色

​       wce.hCursor = NULL;//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = NULL;//菜单名

​       wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​       RegisterClassEx( &wce );//注册,向系统写入

​       

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​              CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,

​              NULL );

​       return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG uMsg = { 0 };

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       AllocConsole();

​       g_hOutput = GetStdHandle( STD_OUTPUT_HANDLE );

​       g_hInstance = hInstance;

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","WINDOWS");

​       Display( hWnd);

​       Message();

​       return 0;

}

**//****创建菜单项**

\#include "stdafx.h"

\#include "stdio.h"

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

HMENU hFile = 0;

HANDLE g_hOutput = 0;

//构建菜单

void OnCreate ( HWND hWnd )

{

​       HMENU hMain = CreateMenu();

​       hFile = CreatePopupMenu();

​       AppendMenu ( hFile, MF_STRING|MF_CHECKED, 1003, "新建");

​       AppendMenu ( hFile, MF_SEPARATOR, 0, "");// 分割线

​       AppendMenu ( hFile, MF_STRING|MF_MENUBARBREAK, 1004, "退出");//并排

​       HMENU hHelp = CreatePopupMenu();

​       AppendMenu( hHelp,MF_STRING|MF_GRAYED,1005, "关于");

​       AppendMenu ( hMain, MF_POPUP, (UINT)hFile, "文件");

​       AppendMenu ( hMain, MF_POPUP, (UINT)hHelp, "帮助");

​       SetMenu( hWnd, hMain ); //设置

}

//功能实现

int g_state = 0;//全局变量(标志量)

void OnCommand( HWND hWnd,WPARAM wParam )

{

​       switch( LOWORD(wParam) )

​       {

​       case 1003:

​              if( g_state == 0 )

​              {

​                     CheckMenuItem( hFile,0,MF_BYPOSITION|MF_UNCHECKED );

​              }

​              else

​              {

​                     CheckMenuItem( hFile, 0,MF_BYPOSITION|MF_CHECKED );//菜单项位置

​              }

​              g_state = !g_state;//反向

​              break;

​       case 1004:

​              MessageBox( hWnd, "退出被点击", "Infor",MB_OK );

​              break;

​       case 1005:

​              MessageBox( hWnd, "关于被点击", "Infor",MB_OK );

​              break;

​       }

}

//附带信息

void OnInitMenuPopup( HWND hWnd, WPARAM wParam, LPARAM lParam )

{

​       char szText[256] = { 0 };

​       sprintf( szText,"即将显示菜单:%d,被点击的顶层菜单项索引:%d,\

​              即将显示的菜单是否为窗口菜单:%d\n",wParam,LOWORD(lParam),HIWORD(lParam) );

​              WriteConsole( g_hOutput,szText, strlen(szText),NULL,NULL );

}

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       switch ( nMsg )

​       {

​       case WM_INITMENUPOPUP:

​              OnInitMenuPopup( hWnd, wParam, lParam );

​              break;

​       case WM_COMMAND:

​              OnCommand( hWnd, wParam );

​              break;

​       case WM_CREATE:

​              OnCreate( hWnd );

​              break;

​       case WM_DESTROY:

​              PostQuitMessage( 0 );//能够使getMessage返回0

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 0;//窗口类缓冲区

​       wce.cbWndExtra = 0;//窗口缓冲区

​       wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色

​       wce.hCursor = NULL;//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = NULL;//菜单名

​       wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​       RegisterClassEx( &wce );//注册,向系统写入

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       AllocConsole();

​       g_hOutput = GetStdHandle ( STD_OUTPUT_HANDLE );

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​              CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,

​              NULL );

​       return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG uMsg = { 0 };

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       g_hInstance = hInstance;

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","WINDOWS");

​       Display( hWnd);

​       Message();

​       return 0;

}

**//****绘图**

\#include "stdafx.h"

\#include "resource.h"

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

int g_kind = 0; // 标志量

void OnCommand(HWND hWnd,WPARAM wParam)

{

​       g_kind = LOWORD(wParam);

​       switch(LOWORD(wParam))

​       {

​       case ID_PIT:

​       case ID_LINE:

​       case ID_REC:

​       case ID_ELL:

​       case ID_ARC:

​              //MessageBox(hWnd,"CTRL+Y被点击","Infor",MB_OK);

​              InvalidateRect( hWnd,NULL,TRUE );//重绘

​              break;     

​       }

}

//绘制点

void DrawPit(HDC hdc)

{

​       //SetPixel(hdc,100,100,RGB(255,0,0));//点

​       for(int i=0; i<256; i++ )//面

​       {

​              for(int j=0; j<256;j++ )

​              {

​                     SetPixel(hdc,i,j,RGB(256,0,0));

​              }

​       }

}

//绘制线

void DrawLine(HDC hdc)

{

​       MoveToEx(hdc,100,100,NULL);//设置当前点

​       LineTo(hdc,300,300);//绘线

​       LineTo(hdc,200,500);//接着画

}

//绘制矩形

void DrawRec( HDC hdc)

{

​       Rectangle(hdc,100,100,300,300);//矩形

​       //RoundRect(hdc,300,100,500,300,50,50);//圆角矩形

​       RoundRect(hdc,300,100,500,300,200,200);//变成圆了

}

//绘制圆形

void DrawEll(HDC hdc)

{

​       Ellipse(hdc,100,100,300,300);//圆

​       Ellipse(hdc,300,100,600,300);//椭圆

}

//绘制弧线

void DrawArc(HDC hdc)

{

​       SetArcDirection(hdc,AD_CLOCKWISE);//顺时针//设置取弧规则

​       Arc(hdc,100,100,300,300,100,100,300,300);//画圆(默认逆时针取弧)

}

//绘图

void OnPaint(HWND hWnd)

{

​       PAINTSTRUCT ps = { 0 };

​       HDC hdc = BeginPaint(hWnd,&ps);

​       //画笔

​       //HPEN hPen = CreatePen(PS_SOLID,100,RGB(255,0,0));//做笔//实线

​       HPEN hPen = CreatePen(PS_DASH,1,RGB(255,0,0));//做笔//虚线(必须一个像素宽)

​       HGDIOBJ nOldPen = SelectObject(hdc,hPen);//送笔

​       //画刷

​       //HBRUSH hBrush = CreateSolidBrush(RGB(0,255,0));//实心画刷(填充单一色)

​       //HBRUSH hBrush = CreateHatchBrush(HS_CROSS,RGB(0,255,0));//填充阴影线

​       //HGDIOBJ hBrush = GetStockObject(NULL_BRUSH);//透明画刷(从系统获取)

​       //位图

​       HBITMAP hBmp = LoadBitmap( g_hInstance,MAKEINTRESOURCE(IDB_BITMAP1));

​       HBRUSH hBrush = CreatePatternBrush( hBmp );

​       HGDIOBJ nOldBrush = SelectObject(hdc,hBrush);//送刷

​       switch(g_kind)

​       {

​       case ID_ARC:

​              DrawArc(hdc);//绘制弧线

​              break;

​       case ID_ELL:

​              DrawEll(hdc);//绘制圆形

​              break;

​       case ID_REC:

​              DrawRec(hdc);//绘制矩形

​              break;

​       case ID_PIT:

​              DrawPit(hdc);//绘制点

​              break;

​       case ID_LINE:

​              DrawLine(hdc);//绘制线

​              break;

​       }

​       SelectObject(hdc,nOldPen);//换回画笔

​       DeleteObject( hPen);//释放画笔

​       SelectObject(hdc,nOldBrush);//换回画刷

​       DeleteObject( hBrush );//释放画刷

​       EndPaint(hWnd,&ps);

}

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       switch ( nMsg )

​       {

​       case WM_COMMAND:

​              OnCommand(hWnd,wParam);

​              break;

​       case WM_DESTROY:

​              PostQuitMessage( 0 );//能够使getMessage返回0

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 0;//窗口类缓冲区

​       wce.cbWndExtra = 0;//窗口缓冲区

​       wce.hbrBackground =

​              CreateSolidBrush(RGB(0,0,255));//(HBRUSH)(COLOR_WINDOW+1);//背景色

​       wce.hCursor = NULL;//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);//菜单名

​       wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​       RegisterClassEx( &wce );//注册,向系统写入

​       

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​              CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,

​              NULL );

​       return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG uMsg = { 0 };

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       g_hInstance = hInstance;

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","WINDOWS");

​       Display( hWnd);

​       Message();

​       return 0;

}

**//****对话框**

\#include "stdafx.h"

\#include "resource.h"

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

//模式对话框过程函数

BOOL CALLBACK ModalDlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)

{

​       switch (uMsg)

​       {

​       case WM_INITDIALOG:

​              SetWindowText(hDlg,"模式对话框");

​              return TRUE;

​       case WM_COMMAND:

​              switch(LOWORD(wParam))

​              {

​              case IDOK:

​                     EndDialog(hDlg,1);

​                     return TRUE;

​              case IDCANCEL:

​                     EndDialog(hDlg,2);

​                     //DestroyWindow(hDlg);

​                     return TRUE;

​              }

​              break;

​       case WM_CLOSE:

​              EndDialog(hDlg,3);

​              return TRUE;

​       case WM_DESTROY:

​              MessageBox(hDlg,"WM_DESTORY来了!","对话框",MB_OK);

​              return TRUE;

​       }

​       return FALSE;

}

//无模式对话框过程函数

BOOL CALLBACK ModelessProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)

{

​       switch (uMsg)

​       {

​       case WM_INITDIALOG:

​              SetWindowText(hDlg,"无模式对话框");

​              return TRUE;

​       case WM_COMMAND:

​              switch(LOWORD(wParam))

​              {

​              case IDOK:

​                     DestroyWindow(hDlg);

​                     return TRUE;

​              case IDCANCEL:

​                     DestroyWindow(hDlg);

​                     //DestroyWindow(hDlg);

​                     return TRUE;

​              }

​              break;

​       case WM_CLOSE:

​              return TRUE;

​       case WM_DESTROY:

​              SetActiveWindow(GetParent(hDlg));//激活父窗口

​              return TRUE;

​       }

​       return FALSE;

}

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       switch ( nMsg )

​       {

​       case WM_COMMAND:

​              switch(LOWORD(wParam))

​              {

​              case IDM_DLG_MODAL://模式

​                     {

​                            int nResult = DialogBox(g_hInstance,MAKEINTRESOURCE(IDD_DIALOG),

​                                   hWnd,ModalDlgProc);

​                            switch (nResult)

​                            {

​                            case 1:

​                                   MessageBox(hWnd,"用户按了确定按钮!","对话框",MB_OK);

​                                   break;

​                            case 2:

​                                   MessageBox(hWnd,"用户按了取消按钮!","对话框",MB_OK);

​                                   break;

​                            case 3:

​                                   MessageBox(hWnd,"用户按了关闭按钮!","对话框",MB_OK);

​                                   break;

​                            }

​                            return 0;

​                     }

​              case IDM_DLG_MODELESS://无模式

​                     HWND hDlg = CreateDialog(g_hInstance,MAKEINTRESOURCE(IDD_DIALOG),

​                            hWnd,ModelessProc);

​                     ShowWindow(hDlg,SW_NORMAL);

​                     return 0;

​              }

​              break;

​       case WM_DESTROY:

​              PostQuitMessage( 0 );//能够使getMessage返回0

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 0;//窗口类缓冲区

​       wce.cbWndExtra = 0;//窗口缓冲区

​       wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色

​       wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = MAKEINTRESOURCE(IDR_MENU_MAIN);//菜单名

​       wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​       RegisterClassEx( &wce );//注册,向系统写入

​       

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​              CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,

​              NULL );

​       return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG uMsg = { 0 };

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       g_hInstance = hInstance;

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","WINDOWS");

​       Display( hWnd);

​       Message();

​       return 0;

}

**//****按钮**

\#include "stdafx.h"

\#define   IDC_BTN_PUSH       4001

\#define   IDC_BTN_DEFPUSH    4002

\#define   IDC_BTN_GROUP      4003

\#define   IDC_BTN_CHECK      4004

\#define   IDC_BTN_AUTOCHECK  4005

\#define   IDC_BTN_3STATE     4006

\#define   IDC_BTN_AUTO3STATE 4007

\#define   IDC_BTN_RADIO1     4008

\#define   IDC_BTN_RADIO2     4009

\#define   IDC_BTN_AUTORADIO1 4010

\#define   IDC_BTN_AUTORADIO2 4011

\#define   IDC_BTN_AUTORADIO3 4012

\#define   IDC_BTN_AUTORADIO4 4013

\#define   IDC_BTN_PUSHLIKE   4014

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       switch ( nMsg )

​       {

​       case WM_CREATE:

​       //创建按钮

​              {

​              //下压式按钮

​              int x = 4, y = 4;

​              CreateWindowEx(

​                     0,

​                     "BUTTON",

​                     "Push Button",

​                     WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,

​                     x,

​                     y,

​                     150,

​                     32,

​                     hWnd,

​                     (HMENU)IDC_BTN_PUSH,

​                     g_hInstance,

​                     NULL);

​              //缺省下压按钮

​              CreateWindowEx(

​                     0,

​                     "BUTTON",

​                     "Default Push Button",

​                     WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,

​                     x,

​                     y += 36,

​                     150,

​                     32,

​                     hWnd,

​                     (HMENU)IDC_BTN_PUSH,

​                     g_hInstance,

​                     NULL);

​              //分组框

​              CreateWindowEx(

​                     0,

​                     "BUTTON",

​                     "Group Box",

​                     WS_CHILD | WS_VISIBLE | BS_GROUPBOX,

​                     x,

​                     y += 36,

​                     150,

​                     96,

​                     hWnd,

​                     (HMENU)IDC_BTN_GROUP,

​                     g_hInstance,

​                     NULL);

​              //复选框(手动)

​              CreateWindowEx(

​                     0,

​                     "BUTTON",

​                     "Check Box",

​                     WS_CHILD | WS_VISIBLE | BS_CHECKBOX,

​                     x,

​                     y += 100,

​                     150,

​                     24,

​                     hWnd,

​                     (HMENU)IDC_BTN_CHECK,

​                     g_hInstance,

​                     NULL);

​              //复选框(自动)

​              CreateWindowEx(

​                     0,

​                     "BUTTON",

​                     "Auto Check Box",

​                     WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,

​                     x,

​                     y += 28,

​                     150,

​                     24,

​                     hWnd,

​                     (HMENU)IDC_BTN_AUTOCHECK,

​                     g_hInstance,

​                     NULL);

​              //三态复选框(手动)

​              CreateWindowEx(

​                     0,

​                     "BUTTON",

​                     "3 State",

​                     WS_CHILD | WS_VISIBLE | BS_3STATE,

​                     x,

​                     y += 28,

​                     150,

​                     24,

​                     hWnd,

​                     (HMENU)IDC_BTN_3STATE,

​                     g_hInstance,

​                     NULL);

​              //三态复选框(自动)

​              CreateWindowEx(

​                     0,

​                     "BUTTON",

​                     "Auto 3 State",

​                     WS_CHILD | WS_VISIBLE | BS_AUTO3STATE,

​                     x,

​                     y += 28,

​                     150,

​                     24,

​                     hWnd,

​                     (HMENU)IDC_BTN_AUTO3STATE,

​                     g_hInstance,

​                     NULL);

​              //复选框1

​              SendMessage( CreateWindowEx(/* 创建并初始化*/

​                     0,

​                     "BUTTON",

​                     "Radio Button 1",

​                     WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON | WS_GROUP,/* 设为组长*/

​                     x,

​                     y += 28,

​                     150,

​                     24,

​                     hWnd,

​                     (HMENU)IDC_BTN_RADIO1,

​                     g_hInstance,

​                     NULL),BM_SETCHECK,BST_CHECKED,0);

​              //复选框2

​              CreateWindowEx(

​                     0,

​                     "BUTTON",

​                     "Radio Button 2",

​                     WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,

​                     x,

​                     y += 28,

​                     150,

​                     24,

​                     hWnd,

​                     (HMENU)IDC_BTN_RADIO2,

​                     g_hInstance,

​                     NULL);

​              //复选框1(自动)

​              SendMessage( CreateWindowEx(/* 创建并初始化*/

​                     0,

​                     "BUTTON",

​                     "Auto Radio Button 1",

​                     WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON| WS_GROUP,/* 设为组长*/

​                     x,

​                     y += 28,

​                     150,

​                     24,

​                     hWnd,

​                     (HMENU)IDC_BTN_AUTORADIO1,

​                     g_hInstance,

​                     NULL),BM_SETCHECK,BST_CHECKED,0);

​              //复选框2(自动)

​              CreateWindowEx(

​                     0,

​                     "BUTTON",

​                     "Auto Radio Button 2",

​                     WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,

​                     x,

​                     y += 28,

​                     150,

​                     24,

​                     hWnd,

​                     (HMENU)IDC_BTN_AUTORADIO2,

​                     g_hInstance,

​                     NULL);

​              //复选框3(自动)

​              SendMessage( CreateWindowEx(/* 创建并初始化*/

​                     0,

​                     "BUTTON",

​                     "Auto Radio Button 3",

​                     WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON| WS_GROUP,/* 设为组长*/

​                     x,

​                     y += 28,

​                     150,

​                     24,

​                     hWnd,

​                     (HMENU)IDC_BTN_AUTORADIO3,

​                     g_hInstance,

​                     NULL),BM_SETCHECK,BST_CHECKED,0);

​              //复选框4(自动)

​              CreateWindowEx(

​                     0,

​                     "BUTTON",

​                     "Auto Radio Button 4",

​                     WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,

​                     x,

​                     y += 28,

​                     150,

​                     24,

​                     hWnd,

​                     (HMENU)IDC_BTN_AUTORADIO4,

​                     g_hInstance,

​                     NULL);

​              //复选框(自动)(类似于下压按钮)

​              CreateWindowEx(

​                     0,

​                     "BUTTON",

​                     "Push Like",

​                     WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | BS_PUSHLIKE,

​                     x,

​                     y += 28,

​                     150,

​                     32,

​                     hWnd,

​                     (HMENU)IDC_BTN_PUSHLIKE,

​                     g_hInstance,

​                     NULL);

​              return 0;

​              }

​       //消息处理

​       case WM_COMMAND:

​              //按钮消息

​              switch (LOWORD(wParam))

​              {

​              case IDC_BTN_PUSH:

​              case IDC_BTN_DEFPUSH:

​                     switch (HIWORD(wParam))

​                     {

​                     case BN_CLICKED:

​                            {

​                                   CHAR szText[256];

​                                   GetWindowText((HWND)lParam,szText,

​                                          sizeof(szText)/sizeof(szText[0])); //取按钮文字

​                                   MessageBox(hWnd,szText,"按钮",MB_OK);

​                                   return 0;

​                            }

​                     }

​                     break;

​              case IDC_BTN_CHECK:

​                     switch(HIWORD(wParam))

​                     {

​                     //复选框(勾)

​                     case BN_CLICKED:

​                            switch(SendMessage((HWND)lParam,BM_GETCHECK,0,0))

​                            {//打勾勾

​                            case BST_CHECKED:

​                                   SendMessage((HWND)lParam,BM_SETCHECK,BST_UNCHECKED,0);

​                                   break;

​                            case BST_UNCHECKED:

​                                   SendMessage((HWND)lParam,BM_SETCHECK,BST_CHECKED,0);

​                                   break;

​                            }

​                            return 0;

​                     }

​                     break;

​                     //3态复选框(勾)

​                     case IDC_BTN_3STATE:

​                            switch(HIWORD(wParam))

​                            {

​                            case BN_CLICKED:

​                                   switch(SendMessage((HWND)lParam,BM_GETCHECK,0,0))

​                                   {//打勾勾

​                                   case BST_CHECKED:

​                                          SendMessage((HWND)lParam,

​                                                 BM_SETCHECK,BST_INDETERMINATE,0);//灰态

​                                          break;

​                                   case BST_INDETERMINATE:

​                                          SendMessage((HWND)lParam,

​                                                 BM_SETCHECK,BST_UNCHECKED,0);//无勾

​                                          break;

​                                   case BST_UNCHECKED:

​                                          SendMessage((HWND)lParam,

​                                                 BM_SETCHECK,BST_CHECKED,0);//有勾

​                                          break;

​                                   }

​                                   return 0;

​                            }

​                            break;

​                     case IDC_BTN_RADIO1://单选框1

​                            switch (HIWORD(wParam))

​                            {

​                            case BN_CLICKED:

​                                   if(SendMessage((HWND)lParam,BM_GETCHECK,0,

​                                          0) == BST_UNCHECKED)//当前没选

​                                   {

​                                          SendMessage((HWND)lParam,BM_SETCHECK,BST_CHECKED,0);

​                                          SendMessage(GetDlgItem(hWnd,IDC_BTN_RADIO2),

​                                                 BM_SETCHECK,BST_UNCHECKED,0);

​                                   }

​                                   return 0;

​                            }

​                            break;

​                     case IDC_BTN_RADIO2://单选框2

​                            switch (HIWORD(wParam))

​                            {

​                            case BN_CLICKED:

​                                   if(SendMessage((HWND)lParam,BM_GETCHECK,0,

​                                          0) == BST_UNCHECKED)//当前没选

​                                   {

​                                          SendMessage((HWND)lParam,BM_SETCHECK,BST_CHECKED,0);

​                                          SendMessage(GetDlgItem(hWnd,IDC_BTN_RADIO1),

​                                                 BM_SETCHECK,BST_UNCHECKED,0);

​                                   }

​                                   return 0;

​                            }

​                            break;

​              }

​              break;

​       case WM_DESTROY:

​              PostQuitMessage( 0 );//能够使getMessage返回0

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 0;//窗口类缓冲区

​       wce.cbWndExtra = 0;//窗口缓冲区

​       wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色

​       wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = NULL;//菜单名

​       wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​       RegisterClassEx( &wce );//注册,向系统写入

​       

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​              CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,

​              NULL );

​       return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG uMsg = { 0 };

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       g_hInstance = hInstance;

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","Windows");

​       Display( hWnd);

​       Message();

​       return 0;

}

**//****编辑框**

\#include "stdafx.h"

\#include <stdlib.h>

\#define  IDC_EDIT_SINGLEINE  4001

\#define  IDC_EDIT_PASSWORD   4002

\#define  IDC_EDIT_NUMBER     4003

\#define  IDC_EDIT_UPPERCASE  4004

\#define  IDC_EDIT_MULTILINE  4005

\#define  IDC_EDIT_READONLY   4006

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       switch ( nMsg )

​       {

​       case WM_CREATE:

​              {

​                     int x = 4, y = 4;

​                     //单行编辑框

​                     CreateWindowEx(

​                            WS_EX_CLIENTEDGE,

​                            "EDIT",

​                            "Hello, world !",

​                            WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,

​                            x,

​                            y,

​                            238,

​                            24,

​                            hWnd,

​                            (HMENU)IDC_EDIT_SINGLEINE,

​                            g_hInstance,

​                            NULL);

​                     //单行编辑框(口令式(密码))

​                     CreateWindowEx(

​                            WS_EX_CLIENTEDGE,

​                            "EDIT",

​                            NULL,

​                            WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_PASSWORD | ES_CENTER,

​                            x,

​                            y += 28,

​                            238,

​                            24,

​                            hWnd,

​                            (HMENU)IDC_EDIT_PASSWORD,

​                            g_hInstance,

​                            NULL);

​                     //单行编辑框(只能输入数字)

​                     CreateWindowEx(

​                            WS_EX_CLIENTEDGE,

​                            "EDIT",

​                            NULL,

​                            WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER | ES_RIGHT,

​                            x,

​                            y += 28,

​                            238,

​                            24,

​                            hWnd,

​                            (HMENU)IDC_EDIT_NUMBER,

​                            g_hInstance,

​                            NULL);

​                     //单行编辑框(只能输入大写)

​                     CreateWindowEx(

​                            WS_EX_CLIENTEDGE,

​                            "EDIT",

​                            NULL,

​                            WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_UPPERCASE,

​                            x,

​                            y += 28,

​                            238,

​                            24,

​                            hWnd,

​                            (HMENU)IDC_EDIT_UPPERCASE,

​                            g_hInstance,

​                            NULL);

​                     //多行编辑框

​                     CreateWindowEx(

​                            WS_EX_CLIENTEDGE,

​                            "EDIT",

​                            NULL,

​                            WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_HSCROLL | WS_VSCROLL,

​                            x,

​                            y += 28,

​                            238,

​                            183,

​                            hWnd,

​                            (HMENU)IDC_EDIT_MULTILINE,

​                            g_hInstance,

​                            NULL);

​                     //多行编辑框(只读)

​                     CreateWindowEx(

​                            WS_EX_CLIENTEDGE,

​                            "EDIT",

​                            "你好啊\r\n我爱学习",

​                            WS_CHILD | WS_VISIBLE | ES_MULTILINE |

​                                   WS_HSCROLL | WS_VSCROLL | ES_READONLY,

​                            x,

​                            y += 187,

​                            238,

​                            183,

​                            hWnd,

​                            (HMENU)IDC_EDIT_READONLY,

​                            g_hInstance,

​                            NULL);

​                     return 0;

​              }

​              //鼠标左键消息

​       case WM_LBUTTONDOWN:

​              {

​                     int nLen = GetWindowTextLength(GetDlgItem (hWnd,IDC_EDIT_MULTILINE));

​                     if(nLen)

​                     {

​                            ++nLen;

​                            LPSTR pszText = (LPSTR)malloc(nLen * sizeof(pszText[0]));

​                            GetWindowText(GetDlgItem(hWnd,IDC_EDIT_MULTILINE),pszText,nLen);

​                            MessageBox(hWnd,pszText,"密码",MB_OK);

​                            free(pszText);

​                     }

​                     return 0;

​              }

​       case WM_DESTROY:

​              PostQuitMessage( 0 );//能够使getMessage返回0

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 0;//窗口类缓冲区

​       wce.cbWndExtra = 0;//窗口缓冲区

​       wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色

​       wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = NULL;//菜单名

​       wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​       RegisterClassEx( &wce );//注册,向系统写入

​       

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​              CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,

​              NULL );

​       return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG uMsg = { 0 };

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       g_hInstance = hInstance;

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","Windows");

​       Display( hWnd);

​       Message();

​       return 0;

}

**//****滚动条**

\#include "stdafx.h"

\#define   IDC_SB_R  4001

\#define   IDC_SB_G  4002

\#define   IDC_SB_B  4003

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       switch ( nMsg )

​       {

​       case WM_CREATE:

​              {

​                     int x = 4, y = 4;

​                     //滚动条(垂直)

​                     HWND hwndSB = CreateWindowEx (

​                            0,

​                            "SCROLLBAR",

​                            NULL,

​                            WS_CHILD | WS_VISIBLE | SBS_VERT,

​                            x,

​                            y,

​                            50,

​                            300,

​                            hWnd,

​                            (HMENU)IDC_SB_R,

​                            g_hInstance,

​                            NULL);

​                     SendMessage(hwndSB,SBM_SETRANGE,0,255);//设置范围

​                     SendMessage(hwndSB,SBM_SETPOS,0,FALSE);//设置位置

​                     //滚动条(水平)

​                     hwndSB = CreateWindowEx (

​                            0,

​                            "SCROLLBAR",

​                            NULL,

​                            WS_CHILD | WS_VISIBLE | SBS_HORZ,

​                            x += 54,

​                            y += 125,

​                            150,

​                            50,

​                            hWnd,

​                            (HMENU)IDC_SB_G,

​                            g_hInstance,

​                            NULL);

​                     SendMessage(hwndSB,SBM_SETRANGE,0,255);//设置范围

​                     SendMessage(hwndSB,SBM_SETPOS,0,FALSE);//设置位置

​                     //滚动条(垂直)

​                     hwndSB = CreateWindowEx (

​                            0,

​                            "SCROLLBAR",

​                            NULL,

​                            WS_CHILD | WS_VISIBLE | SBS_VERT,

​                            x += 154,

​                            y -= 125,

​                            50,

​                            300,

​                            hWnd,

​                            (HMENU)IDC_SB_B,

​                            g_hInstance,

​                            NULL);

​                     SendMessage(hwndSB,SBM_SETRANGE,0,255);//设置范围

​                     SendMessage(hwndSB,SBM_SETPOS,0,FALSE);//设置位置

​                     return 0;

​              }

​       //消息处理

​              //处理水平滚动条

​       case WM_HSCROLL:

​              {

​                     int nPos = SendMessage((HWND)lParam,SBM_GETPOS,0,0);

​                     switch(LOWORD(wParam))

​                     {

​                     case SB_LINELEFT:

​                            --nPos;

​                            break;

​                     case SB_LINERIGHT:

​                            ++nPos;

​                            break;

​                     case SB_PAGELEFT:

​                            nPos -= 32;

​                            break;

​                     case SB_PAGERIGHT:

​                            nPos += 32;

​                            break;

//                   case SB_THUMBPOSITION: //松手时拿到消息

​                     case SB_THUMBTRACK: // 滚动过程中拿到消息

​                            nPos = HIWORD(wParam);

​                            break;

​                     }

​                     SendMessage((HWND)lParam,SBM_SETPOS,nPos,TRUE);

​                     InvalidateRect(hWnd,NULL,TRUE);//重绘

​                     return 0;

​              }

​              //处理垂直滚动条

​       case WM_VSCROLL:

​              {

​                     int nPos = SendMessage((HWND)lParam,SBM_GETPOS,0,0);

​                     switch(LOWORD(wParam))

​                     {

​                     case SB_LINEUP:

​                            --nPos;

​                            break;

​                     case SB_LINEDOWN:

​                            ++nPos;

​                            break;

​                     case SB_PAGEUP:

​                            nPos -= 32;

​                            break;

​                     case SB_PAGEDOWN:

​                            nPos += 32;

​                            break;

//                   case SB_THUMBPOSITION: //松手时拿到消息

​                     case SB_THUMBTRACK: // 滚动过程中拿到消息

​                            nPos = HIWORD(wParam);

​                            break;

​                     }

​                     SendMessage((HWND)lParam,SBM_SETPOS,nPos,TRUE);

​                     InvalidateRect(hWnd,NULL,TRUE);//重绘

​                     return 0;

​              }

​       //刷背景消息

​       case WM_ERASEBKGND:

​              {

​                     HBRUSH hbru = CreateSolidBrush(RGB(

​                            SendMessage(GetDlgItem(hWnd,IDC_SB_R),SBM_GETPOS,0,0),

​                            SendMessage(GetDlgItem(hWnd,IDC_SB_G),SBM_GETPOS,0,0),

​                            SendMessage(GetDlgItem(hWnd,IDC_SB_B),SBM_GETPOS,0,0) ));

​                     RECT rcClient;

​                     GetClientRect(hWnd,&rcClient);

​                     FillRect((HDC)wParam,&rcClient,hbru);

​                     DeleteObject(hbru);

​                     return 0;

​              }

​       case WM_DESTROY:

​              PostQuitMessage( 0 );//能够使getMessage返回0

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 0;//窗口类缓冲区

​       wce.cbWndExtra = 0;//窗口缓冲区

​       wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色

​       wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = NULL;//菜单名

​       wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​       RegisterClassEx( &wce );//注册,向系统写入

​       

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW |

​              WS_CLIPCHILDREN |WS_HSCROLL | WS_VSCROLL,CW_USEDEFAULT,

​              CW_USEDEFAULT,CW_USEDEFAULT,

​              CW_USEDEFAULT,NULL,NULL,g_hInstance,

​              NULL );

​       return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG uMsg = { 0 };

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       g_hInstance = hInstance;

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","Windows");

​       Display( hWnd);

​       Message();

​       return 0;

}

**//****组合框**

\#include "stdafx.h"

\#include "resource.h"

\#include <stdio.h>

\#include <stdlib.h>

\#define IDC_CMB_SIM     4001

\#define IDC_CMB_DD      4002

\#define IDC_CMB_DDL     4003

HINSTANCE g_hInstance = 0; //接收当前程序实例句柄

//窗口处理函数

LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)

{

​       static HWND hwndSim = NULL,hwndDd = NULL,hwndDdl = NULL;

​       switch ( nMsg )

​       {

​       case WM_CREATE:

​              {

​                     int x = 4,y = 4;

​                     //组合框(简单的)

​                     hwndSim = CreateWindowEx(

​                            0,

​                            "COMBOBOX",

​                            NULL,

​                            WS_CHILD | WS_VISIBLE | WS_VSCROLL |

​                                   CBS_AUTOHSCROLL | CBS_SIMPLE,

​                            x,

​                            y,

​                            200,

​                            250,

​                            hWnd,

​                            (HMENU)IDC_CMB_SIM,

​                            g_hInstance,

​                            NULL);

​                     //组合框(下拉式)

​                     hwndDd = CreateWindowEx(

​                            0,

​                            "COMBOBOX",

​                            NULL,

​                            WS_CHILD | WS_VISIBLE | WS_VSCROLL |

​                            CBS_AUTOHSCROLL | CBS_DROPDOWN,

​                            x += 204,

​                            y,

​                            200,

​                            250,

​                            hWnd,

​                            (HMENU)IDC_CMB_DD,

​                            g_hInstance,

​                            NULL);

​                     //组合框(下拉列表式)

​                     hwndDdl = CreateWindowEx(

​                            0,

​                            "COMBOBOX",

​                            NULL,

​                            WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,

​                            x += 204,

​                            y,

​                            200,

​                            250,

​                            hWnd,

​                            (HMENU)IDC_CMB_DDL,

​                            g_hInstance,

​                            NULL);

​                     break;

​              }

​       //组合框消息

​       case WM_COMMAND:

​              switch (LOWORD(wParam))

​              {

​              //添加选项

​              case IDM_CMB_ADD:

​                     {

​                            for (int i = 0; i< 100; ++i)

​                            {

​                                   CHAR szItem[256];

​                                   sprintf(szItem,"Item %d",i);

​                                   SendMessage(hwndSim,CB_ADDSTRING,0,(LPARAM)szItem);

​                                   SendMessage(hwndSim,CB_SETITEMDATA,i,i+1000);//附加数据

​                                   SendMessage(hwndDd,CB_ADDSTRING,0,(LPARAM)szItem);

​                                   SendMessage(hwndDdl,CB_ADDSTRING,0,(LPARAM)szItem);

​                            }

​                            SendMessage(hwndSim,CB_INSERTSTRING,10,(LPARAM)"Item 9.5");

​                            SendMessage(hwndDd,CB_INSERTSTRING,10,(LPARAM)"Item 9.5");

​                            SendMessage(hwndDdl,CB_INSERTSTRING,10,(LPARAM)"Item 9.5");

​                            return 0;

​                     }

​              //删除选项

​              case IDM_CMB_DEL:

​                     SendMessage(hwndSim,CB_DELETESTRING,10,0);

​                     SendMessage(hwndDd,CB_DELETESTRING,10,0);

​                     SendMessage(hwndDdl,CB_DELETESTRING,10,0);

​                     return 0;

​              //全删除

​              case IDM_CMB_RESET:

​                     SendMessage(hwndSim,CB_RESETCONTENT,0,0);

​                     SendMessage(hwndDd,CB_RESETCONTENT,0,0);

​                     SendMessage(hwndDdl,CB_RESETCONTENT,0,0);

​                     return 0;

​              //获取选项

​              case IDM_CMB_GET:

​                     {

​                            int nSel = SendMessage(hwndSim,CB_GETCURSEL,0,0);

​                            CHAR szMsg[256];

​                            if(nSel == CB_ERR)

​                                   sprintf(szMsg,"没有选项!");

​                            else

​                                   sprintf(szMsg,"当前选项:%d",nSel);

​                            MessageBox(hWnd,szMsg,"WinCombo",MB_OK);

​                            return 0;

​                     }

​              //设置当前被选项

​              case IDM_CMB_SET:

​                     {

​                            SendMessage(hwndDd,CB_SETCURSEL,5,0);

​                            return 0;

​                     }

​              //模糊查找

​              case IDM_CMB_FIND:

​                     {

​                            int nFind = SendMessage(hwndDdl,CB_FINDSTRING,-1,(LPARAM)"Item 55");

​                            CHAR szMsg[256];

​                            if(nFind == CB_ERR)

​                                   sprintf(szMsg,"没找到!");

​                            else

​                                   sprintf(szMsg,"找到了:%d",nFind);

​                            MessageBox(hWnd,szMsg,"WinCombo",MB_OK);

​                            return 0;

​                     }

​              //精确查找

​              case IDM_CMB_EXACT:

​                     {

​                            int nFind = SendMessage(hwndDdl,CB_FINDSTRINGEXACT,-1,(LPARAM)"Item");

​                            CHAR szMsg[256];

​                            if(nFind == CB_ERR)

​                                   sprintf(szMsg,"没找到!");

​                            else

​                                   sprintf(szMsg,"找到了:%d",nFind);

​                            MessageBox(hWnd,szMsg,"WinCombo",MB_OK);

​                            return 0;

​                     }

​              //选择设置

​              case IDM_CMB_SELSTR:

​                     {

​                            SendMessage(hwndDdl,CB_SELECTSTRING,-1,(LPARAM)"itEm 55");

​                            return 0;

​                     }

​              //获取内容

​              case IDM_CMB_TEXT:

​                     {

​                            int nSel = SendMessage(hwndSim,CB_GETCURSEL,0,0);

​                            if(nSel != CB_ERR)

​                            {

​                                   int nLen = SendMessage(hwndSim,CB_GETLBTEXTLEN,

​                                          nSel,0);

​                                   ++nLen;

​                                   LPSTR pszText = (LPSTR)malloc(nLen * sizeof(pszText[0]));

​                                   SendMessage(hwndSim,CB_GETLBTEXT,nSel,

​                                          (LPARAM)pszText);

​                                   MessageBox(hWnd,pszText,"WinCombo",MB_OK);

​                                   free(pszText);

​                            }

​                            return 0;

​                     }

​              //显示文件/目录

​              case IDM_CMB_DIR:

​                     {

​                     SendMessage(hwndSim,CB_DIR,DDL_ARCHIVE,(LPARAM)"C:/Windows/*.*");//文件

​                     SendMessage(hwndDd,CB_DIR,DDL_DIRECTORY | DDL_EXCLUSIVE/*排除它*/,

​                            (LPARAM)"C:/Windows/*.*");//目录

​                     SendMessage(hwndDdl,CB_DIR,DDL_HIDDEN | DDL_ARCHIVE | DDL_SYSTEM |

​                            DDL_EXCLUSIVE/*排除它*/,(LPARAM)"C:/*.*");//隐藏文件

​                     return 0;

​                     }

​              //附加数据

​              case IDM_CMB_DATA:

​                     {

​                            int nSel = SendMessage(hwndSim,CB_GETCURSEL,0,0);//获得所选项

​                            if(nSel != CB_ERR)

​                            {

​                                   int data = SendMessage(hwndSim,CB_GETITEMDATA,nSel,0);//拿选项

​                                   char szMsg[256];

​                                   sprintf(szMsg,"附加数据:%d",data);

​                                   MessageBox(hWnd,szMsg,"WinCombo",MB_OK);

​                            }

​                            return 0;

​                     }

​              //通知消息

​              case IDC_CMB_SIM:

​                     switch (HIWORD(wParam))

​                     {

​                     case CBN_SELCHANGE:

​                            int nSel = SendMessage(hwndSim,CB_GETCURSEL,0,0);

​                            if(nSel != CB_ERR)

​                            {

​                                   SendMessage(hwndDd,CB_SETCURSEL,nSel,0);

​                                   SendMessage(hwndDdl,CB_SETCURSEL,nSel,0);

​                            }

​                            return 0;

​                     }

​              }

​              break;

​       case WM_DESTROY:

​              PostQuitMessage( 0 );//能够使getMessage返回0

​              break;

​       }

​       return DefWindowProc( hWnd, nMsg, wParam,lParam );

}

//注册窗口类

void Register(LPSTR lpClassName,WNDPROC wndproc )

{

​       WNDCLASSEX wce = { 0 };

​       wce.cbSize = sizeof( wce ); //结构体的大小

​       wce.cbClsExtra = 0;//窗口类缓冲区

​       wce.cbWndExtra = 0;//窗口缓冲区

​       wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色

​       wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标

​       wce.hIcon = NULL;//窗口大图标句柄

​       wce.hIconSm = NULL;//窗口小图标句柄

​       wce.hInstance = g_hInstance;//当前模块句柄

​       wce.lpfnWndProc = wndproc;//窗口处理函数

​       wce.lpszClassName = lpClassName;//窗口类名称

​       wce.lpszMenuName = MAKEINTRESOURCE(IDR_MENU_MAIN);//菜单名

​       wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格

​       RegisterClassEx( &wce );//注册,向系统写入

​       

}

//创建主窗口

HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )

{

​       HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,

​              CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,

​              NULL );

​       return hWnd;

}

//显示(绘制)窗口

void Display(HWND hWnd )

{

​       ShowWindow( hWnd,SW_SHOW );

​       UpdateWindow( hWnd);

}

//消息循环

void Message()

{

​       MSG uMsg = { 0 };

​       while( GetMessage(&uMsg,NULL,0,0)) //获取消息

​       {

​              TranslateMessage( &uMsg ); // 翻译,分析

​              DispatchMessage( &uMsg ); // 调度,派发

​       }

}

int APIENTRY WinMain(HINSTANCE hInstance,

​                     HINSTANCE hPrevInstance,

​                     LPSTR     lpCmdLine,

​                     int       nCmdShow)

{

​       g_hInstance = hInstance;

​       Register("Main",WndProc);

​       HWND hWnd = CreateMain( "Main","Windows");

​       Display( hWnd);

​       Message();

​       return 0;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文