WINAPI阶段练习01

作者在 2007-09-08 08:41:00 发布以下内容

#pragma comment(linker,"/subsystem:windows")
#define WINVER 0x0500
#define _WIN32_WINNT 0x0500
#include <windows.h>
#define VK_X 0x58
LPCTSTR lpszAppName = TEXT("Haroldi"),lpszTitle = TEXT("阶段练习");
//==========================================================
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
//==========================================================
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
 MSG msg; HWND hWnd; WNDCLASSEX wcex;

 HANDLE h = CreateMutex(NULL,TRUE,lpszAppName);
 if(!h){MessageBox(NULL,TEXT("不能创建!"),lpszTitle,MB_ICONERROR|MB_SYSTEMMODAL);return FALSE;}
 if(GetLastError() == ERROR_ALREADY_EXISTS)
  {MessageBox(NULL,TEXT("不能重复载入!"),lpszTitle,MB_ICONERROR|MB_SYSTEMMODAL);return FALSE;}

 memset(&wcex,0,sizeof(WNDCLASSEX));
 wcex.cbSize = sizeof(WNDCLASSEX);
 wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 wcex.hCursor = LoadCursor(NULL,IDC_ARROW);
 wcex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
 wcex.hInstance = hInstance;
 wcex.lpfnWndProc = (WNDPROC)WndProc;
 wcex.lpszClassName = lpszAppName;
 wcex.lpszMenuName = lpszAppName;
 wcex.style = CS_VREDRAW | CS_HREDRAW;
 if(!RegisterClassEx(&wcex))
  {MessageBox(NULL,TEXT("不能注册!"),lpszTitle,MB_ICONERROR|MB_SYSTEMMODAL);return FALSE;}
 hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,lpszAppName,lpszTitle,WS_OVERLAPPED,
       CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
 if(!hWnd){MessageBox(NULL,TEXT("不能创建!"),lpszTitle,MB_ICONERROR|MB_SYSTEMMODAL);return FALSE;}
 ShowWindow(hWnd,iCmdShow);
 UpdateWindow(hWnd);

 if(!RegisterHotKey(hWnd,0x0001,MOD_CONTROL | MOD_ALT, VK_X))
  {MessageBox(NULL,TEXT("不能注册热键!"),lpszTitle,MB_OK | MB_SYSTEMMODAL);return FALSE;}
 MessageBox(NULL,TEXT("运行成功!\nCTRL_ALT_X退出"),lpszTitle,MB_OK | MB_SYSTEMMODAL);

 while(GetMessage(&msg,NULL,0,0))
  {TranslateMessage(&msg); DispatchMessage(&msg);}
 return msg.wParam;
}
//==========================================================
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
 HDC hdc; RECT rect; PAINTSTRUCT ps;
 LOGFONT LogFont;  HFONT hFont;

 switch(uMsg)
 {
 case WM_PAINT:
  memset(&LogFont,0,sizeof(LOGFONT));
  lstrcpy(LogFont.lfFaceName,"微软雅黑");
  LogFont.lfHeight = 200;
  LogFont.lfWidth = 30;
  LogFont.lfWeight = 80;
  hFont = CreateFontIndirect(&LogFont);

  hdc = BeginPaint(hWnd,&ps);
  GetClientRect(hWnd,&rect);
  SelectObject(hdc,hFont);
  SetTextColor(hdc,RGB(255,0,0));
  DrawText(hdc,TEXT("禁止重复运行,Ctrl_Alt_X退出 。"),-1,&rect,DT_SINGLELINE | DT_VCENTER | DT_CENTER);
  EndPaint(hWnd,&ps);
  break;
 case WM_HOTKEY:
  if(LOWORD(wParam) == 0x0001)
   if(IDOK == MessageBox(NULL,TEXT("确定退出吗?"),lpszTitle,MB_OKCANCEL | MB_SYSTEMMODAL))
    DestroyWindow(hWnd);
  break;
 case WM_KEYDOWN:
  switch(LOWORD(wParam))
  {
  case VK_ESCAPE:  DestroyWindow(hWnd);  break;
  case VK_F1:  MessageBox(NULL,TEXT("这里是帮助文件..."),lpszTitle,MB_OK); break;
  default:   break;
  }
  break;
 case WM_DESTROY: PostQuitMessage(0); break;
 default: return DefWindowProc(hWnd,uMsg,wParam,lParam);
 }
 return 0L;
}
//==================================

WINAPI | 阅读 2426 次
文章评论,共0条
游客请输入验证码
浏览16310次
最新评论