// 6WinRes.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "6WinRes.h"
#include "Resource.h"
#include <iostream>
#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
HINSTANCE g_hInst = NULL; //窗口句柄
HANDLE g_hStdout = NULL; //控制台句柄
CHAR szText[256] = { 0 };
BOOL OnSetCursor(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
//如果鼠标是在系统标题栏上,就返回FALSE
int nHitTest = LOWORD(wParam);
if (HTCLIENT != nHitTest)
{
//不在客户区,返回False
//让DefWindowProc处理
return FALSE;
}
//获得窗口的客户区
RECT rcCilent = { 0 };
GetClientRect(hWnd, &rcCilent);
//获得当前光标的位置
POINT ptPos = { 0 };
GetCursorPos(&ptPos); //获得光标坐标
ScreenToClient(hWnd, &ptPos); //获得客户区坐标
//根据位置设置光标
HCURSOR hCursor = NULL;
if (ptPos.x < rcCilent.left / 2)
{
if (ptPos.y < rcCilent.bottom / 2)
{
hCursor = LoadCursor(NULL, IDC_SIZEALL);
}
else
{
hCursor = LoadCursor(NULL, IDC_CROSS);
}
}
else
{
if (ptPos.y < rcCilent.bottom / 2)
{
hCursor = LoadCursor(NULL, IDC_WAIT);
}
else
{
hCursor = LoadCursor(NULL, IDC_UPARROW);
}
}
//设置光标
SetCursor(hCursor);
return TRUE;
}
void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
int nCmdID = LOWORD(wParam);
switch (nCmdID)
{
case ID_EXIT:
PrintLog("ID_EXIT 触发\n");
break;
case ID_OPEN:
PrintLog("ID_OPEN 触发\n");
break;
case ID_HELP:
PrintLog("ID_HELP 触发\n");
break;
case ID_ABOUT:
PrintLog("ID_ABOUT 触发\n");
break;
case ID_REGISTER:
PrintLog("ID_REGISTER 触发\n");
break;
}
//语法习惯
//unsigned int nCmdID;
//DWORD dwCmdID;
//WORD wCmdId;
//可以直接用 nCmdID 定义,代表数字
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_SETCURSOR:
if (TRUE == OnSetCursor(hWnd, nMsg, wParam, lParam))
{
return 0;
}
//OnSetCursor(hWnd, nMsg, wParam, lParam); //VS2013直接用这句也可解决
break;
case WM_COMMAND:
OnCommand(hWnd, nMsg, wParam, lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
BOOL RegisterWnd(LPSTR pszClassName)
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof(wce);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
wce.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_CURSOR1));
wce.hIcon = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_ICON3));
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW | CS_VREDRAW;
ATOM atom = RegisterClassEx(&wce);
if (atom == NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
HWND CreateWnd(LPSTR pszClassName)
{
//加载字符串资源
CHAR szTextCreate[256] = { 0 };
LoadString(g_hInst, IDS_MAIN, szTextCreate, 256);
//加载菜单
HMENU hMainMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU1));
//加载完之后要放在窗口上,放到CreateWindow函数里的倒数第三个就成
HWND hWnd = CreateWindowEx(0, pszClassName, szTextCreate, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, hMainMenu, g_hInst, 0);
return hWnd;
}
void ShowWnd(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
void Msg(HWND hWnd)
{
//加载加速键,按X键触发退出消息
HACCEL hAccel = LoadAccelerators(g_hInst, MAKEINTRESOURCE(IDR_ACCELERATOR1));
//消息循环
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{ //增加加速键的消息处理
if (!TranslateAccelerator(hWnd, hAccel, &msg)) //加速键,如果不返回零,则以前的消息 就不执行了
{ //正常消息处理
//字符串消息处理
TranslateMessage(&msg);
//消息派发
DispatchMessage(&msg);
}
}
}
void ConsoleWnd()
{
AllocConsole();
g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR szText[] = "Debug start:\n";
WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
g_hInst = hInstance;
ConsoleWnd();
RegisterWnd("oooo");
HWND hWnd = CreateWnd("oooo");
ShowWnd(hWnd);
Msg(hWnd); //加速键处理
return 0;
}
时间: 2024-10-23 19:02:27