13windows_static静态标签
#include <windows.h>
#include <iostream>
#include "resource.h"
/**
*dat : 2015-05-22 10:59:53
*file : F:\long\win32 study\Win32_Project\13Windows_static\13windows_static.cpp
*file base: 13windows_static
*say: 本函数说明了静态框如果添加文字及图片,如何使用STN_CLICK消息
*/
CHAR szText[256] = { 0 };
#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
HINSTANCE g_hInst = NULL; //窗口句柄
HANDLE g_hStdout = NULL; //控制台句柄
//OnCommand
void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
int nCmdID = HIWORD(wParam);//父窗口的ID
int nCtrlID = LOWORD(wParam);//控件ID
switch (nCtrlID)
{
//变换图标
case ID_CHANGICON:
{
HICON hIcon = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_ICON1));
HWND hStatic = GetDlgItem(hWnd, 1002); //根据子控件ID获得ID所对应的句柄
SendMessage(hStatic, STM_SETICON, (WPARAM)hIcon, 0);
}
break;
//静态框截获点击消息
case 1001:
switch (nCmdID)
{
case STN_CLICKED:
MessageBox(NULL, "nCmdID", "static", MB_OK);
break;
}
break;
case 1002:
switch (nCmdID)
{
case STN_CLICKED:
MessageBox(NULL, "nCmdID_pic", "static_pic", MB_OK);
break;
}
}
}
//OnCreate静态框
void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
//文字风格
CreateWindow("STATIC", "哈喽!静态框",
WS_CHILD/*子窗口*/ | WS_VISIBLE | SS_SUNKEN | SS_SIMPLE/*独有的风格*/|SS_NOTIFY/*让窗口能点击风格*/,
50, 50, 100, 100, hWnd, (HMENU)1001, g_hInst, NULL);
//图片风格
CreateWindow("STATIC", "#101"/*把对应的ID资源号弄进来*/,
WS_CHILD | WS_VISIBLE | SS_ICON | SS_SUNKEN|SS_NOTIFY/*让窗口能点击风格*/,
200, 50, 100, 100, hWnd, (HMENU)1002, g_hInst, NULL);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_COMMAND:
OnCommand(hWnd, nMsg, wParam, lParam);
break;
case WM_CREATE://静态框要在WM_CREATE消息中创建
OnCreate(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 = NULL;
wce.hIcon = NULL;
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)
{
HMENU hMen = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU1));
HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, hMen, g_hInst, 0);
return hWnd;
}
void ShowWnd(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
void Msg()
{
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
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();
return 0;
}
时间: 2024-10-12 09:50:11