现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

Windows SDK编程示例

2012-08-01 04:59 工业·编程 ⁄ 共 1740字 ⁄ 字号 暂无评论

// SDK.c -- Windows SDK编程示例
// 编译:cl SDK.c user32.lib

#include <windows.h>

#define IDC_BUTTON1 100

HINSTANCE hInst;

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
TCHAR szWindowClass[]=TEXT("My Windows");
TCHAR szTitle[]=TEXT("Windows SDK Program");
HWND hWnd;
MSG msg;
WNDCLASS wndclass;

hInst=hInstance;

//窗口类定义
wndclass.style   = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC)WndProc;
wndclass.cbClsExtra  = 0;
wndclass.cbWndExtra  = 0;
wndclass.hInstance  = hInstance;
wndclass.hIcon   = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor  = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szWindowClass;

//注册窗口类
if (!RegisterClass(&wndclass))
  return FALSE;

//创建窗口
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
   CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
   NULL, NULL, hInstance, NULL);
if (!hWnd)
    return FALSE;

ShowWindow(hWnd, nCmdShow);    //显示窗口
UpdateWindow(hWnd);      //绘制客户区

while (GetMessage(&msg, NULL, 0, 0)) //消息循环
{
  TranslateMessage(&msg);
  DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
  switch (LOWORD(wParam))
  {
   case IDC_BUTTON1:    //点击按钮
   MessageBox(hwnd, "Hello World!", "SDK", MB_OK|MB_ICONEXCLAMATION);
   break;
  }
  return 0;

case WM_CREATE:
  //创建子窗口控件
  CreateWindow("STATIC", "Hello Windows 98!", WS_CHILD|WS_VISIBLE|SS_CENTER,
     180,100,250,20, hwnd, NULL, hInst, NULL);
  CreateWindow("BUTTON", "Push me", WS_CHILD|WS_VISIBLE,
     250,150,100,30, hwnd, (HMENU)IDC_BUTTON1, hInst, NULL);
  return 0;

case WM_DESTROY:
  PostQuitMessage(0);
  return 0;

default:
  return DefWindowProc(hwnd, message, wParam, lParam);
}
}

给我留言

留言无头像?