c++界面开发入门

先随便新建一个项目,比如这个控制台应用:

 项目右键添加资源:

选dialog,如下图:

 

然后双击资源文件,如下图:

 右键查看属性:

记住这个id,后面会用到,另这个id可以手动修改。

CPP文件写如下代码:

 

文字版:

#include <iostream>
#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    return 0;
}
int main()
{
    std::cout << "Hello World!\n";

    DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)WndProc);
}

代码里的IDD_DIALOG1,就是上上图里的那个ID。

 然后运行一下,可以出现结果:

目前按键还没作用,现在增加按键响应。

右键这个按键,然后选属性,可以看到这个按钮的ID,如下图:

 这个ID也可以手动修改。

然后修改代码,如下:

#include <iostream>
#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    if (msg == WM_COMMAND) {

		auto id = LOWORD(wParam);
		if (id == IDOK)
		{
			MessageBox(hWnd, TEXT("Button pushed"), TEXT("OK button"), MB_OK);
		}
		else if (id == IDCANCEL)
		{
			EndDialog(hWnd, id);
		}
    }
    
    return 0;
}
int main()
{
    std::cout << "Hello World!\n";

    DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)WndProc);
}

 运行后如下:

按确定键弹出MessageBox;按取消键关闭整个对话框。

至此,一个界面开发入门算是介绍完了。不过代码都是抄的stack大佬的,我自己凭本事是入不了门的😂。C++入门太难了,网上干货少,api难找,找到了也不知道怎么用,vs2019又对新手不友好,总之太难了🤣。

比如这个对话框,如果只看官方文档的话,api解释如下:

void DialogBoxA(
	[in, optional]  hInstance,
	[in]            lpTemplate,
	[in, optional]  hWndParent,
	[in, optional]  lpDialogFunc
);

四个参数解释如下:

Parameters

[in, optional] hInstance

Type: HINSTANCE

A handle to the module which contains the dialog box template. If this parameter is NULL, then the current executable is used.

[in] lpTemplate

Type: LPCTSTR

The dialog box template. This parameter is either the pointer to a null-terminated character string that specifies the name of the dialog box template or an integer value that specifies the resource identifier of the dialog box template. If the parameter specifies a resource identifier, its high-order word must be zero and its low-order word must contain the identifier. You can use the MAKEINTRESOURCE macro to create this value.

[in, optional] hWndParent

Type: HWND

A handle to the window that owns the dialog box.

[in, optional] lpDialogFunc

Type: DLGPROC

A pointer to the dialog box procedure. For more information about the dialog box procedure, see DialogProc.

原文链接:

DialogBoxA macro (winuser.h) - Win32 apps | Microsoft Learn

文档里连个示例都没有,一个新手,光看这些,如何知道要怎么调用呢?更别提按钮点击事件了。到最后还是得去百度谷歌搜,搜到了合适的教程可以很快上手,但搜了半天都找不到合适的,那岂不是要难受死了?白白浪费了大把的时间。这也是本文的意义所在。

这是一款真正意义上适合软件界面开发C++界面库。采用XML管理GDI资源(如:图片、字体、颜色等),最大程度将界面与逻辑分开,让程序员有更多的时间去进行软件内部的逻辑处理。SKINSE扩展了非常丰富的API接口,兼容其他界面控件,使界面开发更加灵活、高效。SkinSE只用到了windows几个底层的核心库,没有用到(MFC/ATL等第三方库),采用纯API编写,采用C语言导出方式,增强可移植性,可以用于多种计算机语言。 1.SKINSE界面库内部没有完全采用HOOK拦截窗口的机制,主要采用子类化控件,修改窗口过程函数的方式进行界面美化。但是内置HOOK技术,可以动态设置HOOK,保证了SKINSE在整理构架上的灵活性。 2.SKINSE界面库内置DirectUI绘制思想,将界面绘制元素抽象成图片、文本、矩形区域、线条、动画,并且内置EventItem、DrawItem绘制机制。可以将这些绘制元素以及绘制机制组合开发,实现按钮、单选框、复选框、分组框、Tab控件、动画控件等界面控件。 3.SKINSE界面库提供了非常丰富的API接口,最大限度的让SKINSE界面库更加透明化、个性化,让SKINSE界面库的使用范围得到最大延伸。 4.SKINSE界面库在子类化进行控件美化的时候,尽量不修改控件的默认属性,最大程度地兼容window标准控件的默认属性。比如,目前市场上很多界面库在绘制窗口标题栏的时候,去掉了窗口的WS_CAPTION属性, 导致GetClientRect、GetWindowRect方法失效,从而加大了界面开发和设计的难度,当然也导致不能完美支持SDI/MDI等界面框架。 5.SKINSE界面库提供了界面控件动态布局的特性。省去了程序开发中最为繁琐的窗口控件位置的调整。只需要调整一下XML配置属性,就可以进行完美布局。 6.SKINSE界面库支持BMP、ICO、PNG、JPG等图片格式,支持皮肤色调变换,提供SKINSE内部的图像引擎接口。 7.SKINSE界面库支持SDI、MDI等界面框架,支持20余种界面控件,完整兼容window 2000、window2003、window XP、vista、window7当前的主流操作系统。并且支持window平台的VC、.NET、VB6、Delphi、C++ Builder、PowerBuilder、易语言等多种计算机语言开发
C++写的一个简单的界面演示系统 void CMiniDrawDoc::AddFigure (CFigure *PFigure) { m_FigArray.Add (PFigure); SetModifiedFlag (); } CFigure *CMiniDrawDoc::GetFigure (int Index) { if (Index m_FigArray.GetUpperBound ()) return 0; return (CFigure *)m_FigArray.GetAt (Index); } int CMiniDrawDoc::GetNumFigs () { return m_FigArray.GetSize (); } void CMiniDrawDoc::DeleteContents() { // TODO: Add your specialized code here and/or call the base class int Index = m_FigArray.GetSize (); while (Index--) delete m_FigArray.GetAt (Index); m_FigArray.RemoveAll (); CDocument::DeleteContents(); } void CMiniDrawDoc::OnEditClearAll() { // TODO: Add your command handler code here DeleteContents (); UpdateAllViews (0); SetModifiedFlag (); } void CMiniDrawDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->Enable (m_FigArray.GetSize ()); } void CMiniDrawDoc::OnEditUndo() { // TODO: Add your command handler code here int Index = m_FigArray.GetUpperBound (); if (Index > -1) { delete m_FigArray.GetAt (Index); m_FigArray.RemoveAt (Index); } UpdateAllViews (0); SetModifiedFlag (); } void CMiniDrawDoc::OnUpdateEditUndo(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->Enable (m_FigArray.GetSize ()); } // implementation of figure classes: IMPLEMENT_SERIAL (CFigure, CObject, 3) CRect CFigure::GetDimRect () { return CRect (min (m_X1, m_X2), min (m_Y1, m_Y2), max (m_X1, m_X2) + 1, max (m_Y1, m_Y2) + 1); } void CFigure::Serialize (CArchive& ar) { if (ar.IsStoring ()) ar << m_X1 << m_Y1 << m_X2 << m_Y2 <> m_X1 >> m_Y1 >> m_X2 >> m_Y2 >> m_Color; } IMPLEMENT_SERIAL (CLine, CFigure, 3) CLine::CLine (int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness) { m_X1 = X1; m_Y1 = Y1; m_X2 = X2; m_Y2 = Y2; m_Color = Color; m_Thickness = Thickness; } void CLine::Serialize (CArchive& ar) { CFigure::Serialize (ar); if (ar.IsStoring ()) ar <> m_Thickness; } void CLine::Draw (CDC *PDC) { CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_SOLID, m_Thickness, m_Color); POldPen = PDC->SelectObject (&Pen); // draw figure: PDC->MoveTo (m_X1, m_Y1); PDC->LineTo (m_X2, m_Y2); // remove pen/brush: PDC->SelectObject (POldPen); } IMPLEMENT_SERIAL (CRectangle, CFigure, 3) CRectangle::CRectangle (int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness) { m_X1 = X1; m_Y1 = Y1; m_X2 = X2; m_Y2 = Y2; m_Color = Color; m_Thickness = Thickness; } void CRectangle::Serialize (CArchive& ar) { CFigure::Serialize (ar); if (ar.IsStoring ()) ar <> m_Thickness; } void CRectangle::Draw (CDC *PDC) { CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, m_Thickness, m_Color); POldPen = PDC->SelectObject (&Pen); PDC->SelectStockObject (NULL_BRUSH); // draw figure: PDC->Rectangle (m_X1, m_Y1, m_X2, m_Y2); // remove pen/brush: PDC->SelectObject (POldPen); } IMPLEMENT_SERIAL (CRectFill, CFigure, 3) CRectFill::CRectFill (int X1, int Y1, int X2, int Y2, COLORREF Color) { m_X1 = min (X1, X2); m_Y1 = min (Y1, Y2); m_X2 = max (X1, X2); m_Y2 = max (Y1, Y2); m_Color = Color; } void CRectFill::Draw (CDC *PDC) { CBrush Brush, *POldBrush; CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, 1, m_Color); POldPen = PDC->SelectObject (&Pen); Brush.CreateSolidBrush (m_Color); POldBrush = PDC->SelectObject (&Brush); // draw figure: PDC->Rectangle (m_X1, m_Y1, m_X2, m_Y2); // remove pen/brush: PDC->SelectObject (POldPen); PDC->SelectObject (POldBrush); } IMPLEMENT_SERIAL (CRectRound, CFigure, 3) CRectRound::CRectRound (int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness) { m_X1 = min (X1, X2); m_Y1 = min (Y1, Y2); m_X2 = max (X1, X2); m_Y2 = max (Y1, Y2); m_Color = Color; m_Thickness = Thickness; } void CRectRound::Serialize (CArchive& ar) { CFigure::Serialize (ar); if (ar.IsStoring ()) ar <> m_Thickness; } void CRectRound::Draw (CDC *PDC) { CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, m_Thickness, m_Color); POldPen = PDC->SelectObject (&Pen); PDC->SelectStockObject (NULL_BRUSH); // draw figure: int SizeRound = (m_X2 - m_X1 + m_Y2 - m_Y1) / 6; PDC->RoundRect (m_X1, m_Y1, m_X2, m_Y2, SizeRound, SizeRound); // remove pen/brush: PDC->SelectObject (POldPen); } IMPLEMENT_SERIAL (CRectRoundFill, CFigure, 3) CRectRoundFill::CRectRoundFill (int X1, int Y1, int X2, int Y2, COLORREF Color) { m_X1 = min (X1, X2); m_Y1 = min (Y1, Y2); m_X2 = max (X1, X2); m_Y2 = max (Y1, Y2); m_Color = Color; } void CRectRoundFill::Draw (CDC *PDC) { CBrush Brush, *POldBrush; CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, 1, m_Color); POldPen = PDC->SelectObject (&Pen); Brush.CreateSolidBrush (m_Color); POldBrush = PDC->SelectObject (&Brush); // draw figure: int SizeRound = (m_X2 - m_X1 + m_Y2 - m_Y1) / 6; PDC->RoundRect (m_X1, m_Y1, m_X2, m_Y2, SizeRound, SizeRound); // remove pen/brush: PDC->SelectObject (POldPen); PDC->SelectObject (POldBrush); } IMPLEMENT_SERIAL (CCircle, CFigure, 3) CCircle::CCircle (int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness) { m_X1 = min (X1, X2); m_Y1 = min (Y1, Y2); m_X2 = max (X1, X2); m_Y2 = max (Y1, Y2); m_Color = Color; m_Thickness = Thickness; } void CCircle::Serialize (CArchive& ar) { CFigure::Serialize (ar); if (ar.IsStoring ()) ar <> m_Thickness; } void CCircle::Draw (CDC *PDC) { CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, m_Thickness, m_Color); POldPen = PDC->SelectObject (&Pen); PDC->SelectStockObject (NULL_BRUSH); // draw figure: PDC->Ellipse (m_X1, m_Y1, m_X2, m_Y2); // remove pen/brush: PDC->SelectObject (POldPen); } IMPLEMENT_SERIAL (CCircleFill, CFigure, 3) CCircleFill::CCircleFill (int X1, int Y1, int X2, int Y2, COLORREF Color) { m_X1 = min (X1, X2); m_Y1 = min (Y1, Y2); m_X2 = max (X1, X2); m_Y2 = max (Y1, Y2); m_Color = Color; } void CCircleFill::Draw (CDC *PDC) { CBrush Brush, *POldBrush; CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, 1, m_Color); POldPen = PDC->SelectObject (&Pen); Brush.CreateSolidBrush (m_Color); POldBrush = PDC->SelectObject (&Brush); // draw figure: PDC->Ellipse (m_X1, m_Y1, m_X2, m_Y2); // remove pen/brush: PDC->SelectObject (POldPen); PDC->SelectObject (POldBrush); }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值