C语言实现在控制台下创建窗口、设置字体大小、设置定时器的示例代码

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


 如下的代码,增加了一些可以操作按钮切换的内容及按键响应的机制。

适用于工厂端写一些简单的UI操作界面,用Windows的API实现,从本质上了解UI的创建原理。

【运行界面】

C语言实现在控制台下创建窗口、设置字体大小、设置定时器的示例代码_#include

【代码如下】

/**
File Description: this code is only for create window and button and so on for test
Author: Tody Guo
Date: 2020-07-13
*/

#include <windows.h>
#include <stdio.h>

HWND hStatic1 = NULL;
HWND hBtn1 = NULL;
HWND hBtn2 = NULL;

int WindowWidth = 450;
int WindowHight = 350;

const int IDT_TIMER1 = 0;
WCHAR fontname[] = L"Arial";

void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{

WCHAR buf[20];
int Cur = 0x100;

wsprintf(buf, L"%ld", Cur);
SetWindowText(hStatic1, buf);
printf(".");
}

void SetFont(HWND hwnd, int iFont, WCHAR* FontName)//iFont height and Font name
{
HDC hdc;
HFONT hFont;
LOGFONT lf;

lf.lfHeight = iFont;
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = 400;
lf.lfItalic = 0;
lf.lfUnderline = 0;
lf.lfStrikeOut = 0;
lf.lfCharSet = GB2312_CHARSET;
lf.lfOutPrecision = 3;
lf.lfClipPrecision = 2;
lf.lfQuality = 1;
lf.lfPitchAndFamily = 2;
lstrcpyW(lf.lfFaceName, FontName);

hFont = CreateFontIndirect(&lf);
if (hFont == NULL)
MessageBox(hwnd, L"CreateFont failure", L"error", MB_ICONERROR);
hdc = GetDC(hwnd);
SelectObject(hdc, hFont);
GetObject(hFont, sizeof(LOGFONT), &lf);
SendMessage(hwnd, WM_SETFONT, WPARAM(hFont), 0);
ReleaseDC(hwnd, hdc);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch (Message) {
case WM_COMMAND:
switch (LOWORD(wParam))
{
case 1:
MessageBoxW(hWnd, L"Button 1 clicked", L"Tips", MB_OK);
SetFocus(hBtn1); //setting button with foucs for next click
break;

case 2:
MessageBoxW(hWnd, L"Button 2 clicked", L"Tips", MB_OK);
SetFocus(hBtn2);
break;
}
break;

case WM_CREATE:
hStatic1 = CreateWindowExW(0, L"STATIC", L"Loading", WS_CHILD | WS_VISIBLE, 20, 20, 150, 125, hWnd, 0, 0, 0);
SetFont(hStatic1, 80, fontname);

hBtn1 = CreateWindowExW(0, L"BUTTON", L"Button1", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 20, 150, 80, 40, hWnd, (HMENU)1, 0, 0);
SetFocus(hBtn1);

hBtn2 = CreateWindowExW(0, L"BUTTON", L"Button2", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 20, 200, 80, 40, hWnd, (HMENU)2, 0, 0);

/*Create a timer for timer task*/
SetTimer(hWnd, IDT_TIMER1, 1000, TimerProc);
break;
case WM_DESTROY:
printf("WM_DESTROY");
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, Message, wParam, lParam);
}
return 0;
}

int main(int argc, char **argv)
{
WNDCLASS wc; // windows class
HWND hWnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG msg; /* A temporary location for all messages */

HINSTANCE hInstance = GetModuleHandle(NULL);

wc.style = CS_HREDRAW | CS_VREDRAW; //窗口样式
wc.lpfnWndProc = WndProc; //窗口的回调函数
wc.hInstance = hInstance; //窗口实例句柄
wc.cbClsExtra = 0; //窗口结构体扩展:无
wc.cbWndExtra = 0; //窗口实例扩展:无
wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); //窗口背景颜色:白色
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); //窗口图标
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //窗口中的鼠标样式
wc.lpszClassName = L"WindowClass"; //窗口结构体名称
wc.lpszMenuName = NULL; //主菜单名称:无
/* register windows class */
if (!RegisterClass(&wc))
{
MessageBox(NULL, L"Register Window Failed.!", L"Error", MB_ICONSTOP);
}

hWnd = CreateWindowExW(WS_TILED| WS_EX_TOPMOST,
L"WindowClass",
L"Console Window Example",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
GetSystemMetrics(SM_CXSCREEN) / 2 - WindowWidth / 2, /* x */
GetSystemMetrics(SM_CYSCREEN) / 2 - WindowHight / 2, /* y */
WindowWidth, /* width */
WindowHight, /* height */
NULL, NULL, hInstance, NULL);

if (hWnd == NULL) {
MessageBox(NULL, L"Window Creation Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

while (GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
if (!IsDialogMessage(hWnd, &msg)){
TranslateMessage(&msg); /* Translate key codes to chars if present */
DispatchMessage(&msg); /* Send it to WndProc */
}
}
return msg.wParam;
}

 

 

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

“C语言实现在控制台下创建窗口、设置字体大小、设置定时器的示例代码” 的相关文章