#include "pch.h"
#include "framework.h"
#include "Demo.h"
#include "DemoDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CDemoDlg::CDemoDlg(CWnd* pParent )
: CDialogEx(IDD_DEMO_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CDemoDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_LBUTTONDOWN()
ON_WM_KEYDOWN()
ON_WM_KEYUP()
ON_BN_CLICKED(IDC_BUTTON1, &CDemoDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CDemoDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &CDemoDlg::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4, &CDemoDlg::OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON5, &CDemoDlg::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON6, &CDemoDlg::OnBnClickedButton6)
ON_BN_CLICKED(IDC_BUTTON7, &CDemoDlg::OnBnClickedButton7)
ON_BN_CLICKED(IDC_BUTTON8, &CDemoDlg::OnBnClickedButton8)
ON_BN_CLICKED(IDC_BUTTON9, &CDemoDlg::OnBnClickedButton9)
ON_BN_CLICKED(IDC_BUTTON10, &CDemoDlg::OnBnClickedButton10)
ON_BN_CLICKED(IDC_BUTTON11, &CDemoDlg::OnBnClickedButton11)
ON_BN_CLICKED(IDC_BUTTON12, &CDemoDlg::OnBnClickedButton12)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON13, &CDemoDlg::OnBnClickedButton13)
ON_BN_CLICKED(IDC_BUTTON14, &CDemoDlg::OnBnClickedButton14)
ON_BN_CLICKED(IDC_BUTTON15, &CDemoDlg::OnBnClickedButton15)
ON_BN_CLICKED(IDC_BUTTON16, &CDemoDlg::OnBnClickedButton16)
ON_BN_CLICKED(IDC_BUTTON17, &CDemoDlg::OnBnClickedButton17)
ON_BN_CLICKED(IDC_BUTTON18, &CDemoDlg::OnBnClickedButton18)
ON_BN_CLICKED(IDC_BUTTON19, &CDemoDlg::OnBnClickedButton19)
ON_BN_CLICKED(IDC_BUTTON20, &CDemoDlg::OnBnClickedButton20)
ON_BN_CLICKED(IDC_BUTTON21, &CDemoDlg::OnBnClickedButton21)
ON_BN_CLICKED(IDC_BUTTON22, &CDemoDlg::OnBnClickedButton22)
ON_BN_CLICKED(IDC_BUTTON23, &CDemoDlg::OnBnClickedButton23)
ON_BN_CLICKED(IDC_BUTTON24, &CDemoDlg::OnBnClickedButton24)
ON_BN_CLICKED(IDC_BUTTON25, &CDemoDlg::OnBnClickedButton25)
ON_BN_CLICKED(IDC_BUTTON26, &CDemoDlg::OnBnClickedButton26)
ON_BN_CLICKED(IDC_BUTTON27, &CDemoDlg::OnBnClickedButton27)
ON_BN_CLICKED(IDC_BUTTON28, &CDemoDlg::OnBnClickedButton28)
ON_BN_CLICKED(IDC_BUTTON29, &CDemoDlg::OnBnClickedButton29)
ON_BN_CLICKED(IDC_BTN_START, &CDemoDlg::OnBnClickedBtnStart)
ON_BN_CLICKED(IDC_BTN_STOP, &CDemoDlg::OnBnClickedBtnStop)
ON_BN_CLICKED(IDC_BTN_TEST, &CDemoDlg::OnBnClickedBtnTest)
ON_BN_CLICKED(IDC_BUTTON30, &CDemoDlg::OnBnClickedButton30)
ON_BN_CLICKED(IDC_BUTTON31, &CDemoDlg::OnBnClickedButton31)
ON_BN_CLICKED(IDC_BUTTON32, &CDemoDlg::OnBnClickedButton32)
ON_BN_CLICKED(IDC_BUTTON33, &CDemoDlg::OnBnClickedButton33)
ON_BN_CLICKED(IDC_BUTTON34, &CDemoDlg::OnBnClickedButton34)
ON_BN_CLICKED(IDC_BUTTON35, &CDemoDlg::OnBnClickedButton35)
ON_BN_CLICKED(IDC_BUTTON36, &CDemoDlg::OnBnClickedButton36)
ON_BN_CLICKED(IDC_BUTTON37, &CDemoDlg::OnBnClickedButton37)
ON_BN_CLICKED(IDC_BUTTON40, &CDemoDlg::OnBnClickedButton40)
ON_BN_CLICKED(IDC_BUTTON39, &CDemoDlg::OnBnClickedButton39)
ON_BN_CLICKED(IDC_BUTTON41, &CDemoDlg::OnBnClickedButton41)
ON_BN_CLICKED(IDC_BUTTON42, &CDemoDlg::OnBnClickedButton42)
ON_BN_CLICKED(IDC_BUTTON47, &CDemoDlg::OnBnClickedButton47)
ON_BN_CLICKED(IDC_BUTTON48, &CDemoDlg::OnBnClickedButton48)
END_MESSAGE_MAP()
BOOL CDemoDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
HICON hIcon = GetIcon(TRUE);
m_testWnd.MoveWindow(100, 100, 200, 100);
CWnd* pEdit = GetDlgItem(IDC_EDIT1);
pEdit->SetFocus();
return FALSE;
}
void CDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
HCURSOR CDemoDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
int CDemoDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialogEx::OnCreate(lpCreateStruct) == -1)
return -1;
HWND hEdit=::CreateWindow(L"Edit", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_WANTRETURN, 0, 0, 200, 100, m_hWnd, NULL, AfxGetInstanceHandle(), NULL);
m_testWnd.Attach(hEdit);
return 0;
}
void CDemoDlg::OnDestroy()
{
CDialogEx::OnDestroy();
m_testWnd.Detach();
}
void CDemoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
ClientToScreen(&point);
CString str;
str.Format(L"%d,%d", point.x, point.y);
SetWindowText(str);
CDialogEx::OnLButtonDown(nFlags, point);
}
BOOL CDemoDlg::PreTranslateMessage(MSG* pMsg)
{
CString str;
if (pMsg->message == WM_KEYDOWN)
{
switch (pMsg->wParam)
{
case VK_LEFT:
{
str.Format(L"左移");
SetWindowText(str);
break;
}
case VK_ESCAPE:
{
str.Format(L"ESC");
SetWindowText(str);
return TRUE;
}
case VK_RETURN:
{
str.Format(L"回车");
SetWindowText(str);
break;
}
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
void CDemoDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CString str;
switch (nChar)
{
case VK_NUMPAD0:
{
str.Format(L"按了0键盘");
SetWindowText(str);
break;
}
}
CDialogEx::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CDemoDlg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CDialogEx::OnKeyUp(nChar, nRepCnt, nFlags);
}
void CDemoDlg::OnOK()
{
}
void CDemoDlg::OnBnClickedButton1()
{
MessageBox(L"CWnd::MessageBox");
}
void CDemoDlg::OnBnClickedButton2()
{
CString str;
GetWindowText(str);
MessageBox(str);
}
void CDemoDlg::OnBnClickedButton3()
{
SetWindowText(L"设置窗口标题");
}
void CDemoDlg::OnBnClickedButton4()
{
int nLength = GetWindowTextLength();
}
void CDemoDlg::OnBnClickedButton5()
{
ShowWindow(SW_MINIMIZE);
}
void CDemoDlg::OnBnClickedButton6()
{
CWnd* pWnd=GetDlgItem(IDC_BUTTON1);
if (pWnd != NULL)
{
pWnd->SetWindowText(SW_HIDE);
}
}
void CDemoDlg::OnBnClickedButton7()
{
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
pWnd->CloseWindow();
}
void CDemoDlg::OnBnClickedButton8()
{
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
if (pWnd != NULL)
pWnd->DestroyWindow();
}
void CDemoDlg::OnBnClickedButton9()
{
CenterWindow();
}
void CDemoDlg::OnBnClickedButton10()
{
CWnd* pWnd=FindWindow(L"Notepad", L"新建文本文档.txt - 记事本");
if (pWnd == NULL)
{
MessageBox(L"没找到");
}
else
{
pWnd->SendMessage(WM_CLOSE, 0, 0);
}
}
void CDemoDlg::OnBnClickedButton11()
{
CWnd* pWnd = FindWindow(L"Notepad", L"新建文本文档.txt - 记事本");
if (pWnd == NULL)
{
MessageBox(L"没找到");
}
else
{
CWnd* pEditWnd=FindWindowEx(pWnd->m_hWnd,NULL,L"Edit",NULL);
pEditWnd->CloseWindow();
}
}
void CDemoDlg::OnBnClickedButton12()
{
SetTimer(1, 700, NULL);
}
void CDemoDlg::OnTimer(UINT_PTR nIDEvent)
{
FlashWindow(TRUE);
switch (nIDEvent)
{
case 2:
{
CWnd* pWnd = GetDlgItem(IDC_BTN_TEST);
CRect rect;
pWnd->GetWindowRect(&rect);
ScreenToClient(&rect);
int nWidth = rect.Width();
int nHeight = rect.Height();
rect.left += 25;
rect.right += 25;
CRect WndRect;
GetClientRect(&WndRect);
if (rect.left > WndRect.right)
{
rect.left = -nWidth;
rect.right = 0;
}
pWnd->MoveWindow(&rect, TRUE);
break;
}
}
CDialogEx::OnTimer(nIDEvent);
}
void CDemoDlg::OnBnClickedButton13()
{
FlashWindowEx(FLASHW_ALL,5,1000);
}
void CDemoDlg::OnBnClickedButton14()
{
CWnd* pWnd=GetDesktopWindow();
if (pWnd)
{
HWND hWnd = pWnd->m_hWnd;
}
}
void CDemoDlg::OnBnClickedButton15()
{
CString str;
DWORD dwStyle=this->GetStyle();
if (dwStyle &WS_CAPTION)
{
str += L"标题栏风格";
}
SetWindowText(str);
}
void CDemoDlg::OnBnClickedButton16()
{
DWORD dwStyle = GetExStyle();
}
void CDemoDlg::OnBnClickedButton17()
{
DWORD dwStyle = this->GetStyle();
ModifyStyle(0, WS_MAXIMIZEBOX | WS_MINIMIZEBOX);
}
void CDemoDlg::OnBnClickedButton18()
{
}
void CDemoDlg::OnBnClickedButton19()
{
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
if (IsChild(pWnd))
{
MessageBox(L"是");
}
else
{
MessageBox(L"不是");
}
}
void CDemoDlg::OnBnClickedButton20()
{
if (FALSE==IsIconic())
{
ShowWindow(SW_MINIMIZE);
}
else
{
ShowWindow(SW_SHOW);
}
}
void CDemoDlg::OnBnClickedButton21()
{
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
pWnd->EnableWindow(FALSE);
}
void CDemoDlg::OnBnClickedButton22()
{
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
if (pWnd->IsWindowEnabled())
{
pWnd->EnableWindow(FALSE);
}
else
{
pWnd->EnableWindow(TRUE);
}
}
void CDemoDlg::OnBnClickedButton23()
{
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
if (pWnd->IsWindowVisible())
{
pWnd->ShowWindow(SW_HIDE);
}
else
{
pWnd->ShowWindow(SW_SHOW);
}
}
void CDemoDlg::OnBnClickedButton24()
{
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
if (pWnd->IsZoomed())
{
pWnd->CloseWindow();
}
else
{
pWnd->ShowWindow(SW_MAXIMIZE);
}
}
void CDemoDlg::OnBnClickedButton25()
{
CRect rect;
GetWindowRect(&rect);
CString str;
str.Format(L"left:%d, top:%d, right:%d, bottom:%d", rect.left, rect.top, rect.right, rect.bottom);
SetWindowText(str);
}
void CDemoDlg::OnBnClickedButton26()
{
CRect rect;
GetClientRect(&rect);
CString str;
str.Format(L"left:%d, top:%d, right:%d, bottom:%d", rect.left, rect.top, rect.right, rect.bottom);
SetWindowText(str);
}
void CDemoDlg::OnBnClickedButton27()
{
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
CRect rect;
pWnd->GetWindowRect(&rect);
ScreenToClient(&rect);
CString str;
str.Format(L"转换后left:%d, top:%d, right:%d, bottom:%d", rect.left, rect.top, rect.right, rect.bottom);
SetWindowText(str);
}
void CDemoDlg::OnBnClickedButton28()
{
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
CRect rect;
pWnd->GetWindowRect(&rect);
ClientToScreen(&rect);
CString str;
str.Format(L"转换后left:%d, top:%d, right:%d, bottom:%d", rect.left, rect.top, rect.right, rect.bottom);
SetWindowText(str);
}
void CDemoDlg::OnBnClickedButton29()
{
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
pWnd->MoveWindow(0, 0, 70, 20,TRUE);
}
void CDemoDlg::OnBnClickedBtnStart()
{
SetTimer(2, 200, NULL);
}
void CDemoDlg::OnBnClickedBtnStop()
{
KillTimer(2);
int nWidth = GetSystemMetrics(SM_CXSCREEN);
int nHeigth = GetSystemMetrics(SM_CYSCREEN);
}
void CDemoDlg::OnBnClickedBtnTest()
{
}
void CDemoDlg::OnBnClickedButton30()
{
CRect rect;
GetWindowRect(&rect);
int nWidth = rect.Width();
int nHeight = rect.Height();
for (int i = 0; i < 5; i++)
{
rect.OffsetRect(5, 5);
MoveWindow(&rect);
Sleep(50);
rect.OffsetRect(-5, -5);
MoveWindow(&rect);
Sleep(50);
}
}
void CDemoDlg::OnBnClickedButton31()
{
SetWindowPos(&wndTopMost,0,0,500,300, SWP_SHOWWINDOW);
}
void CDemoDlg::OnBnClickedButton32()
{
Sleep(2000);
CWnd* pWnd=GetTopWindow();
CString str;
pWnd->GetWindowText(str);
MessageBox(str);
}
void CDemoDlg::OnBnClickedButton33()
{
CWnd* pWnd = GetFocus();
pWnd->SetWindowText(L"焦点");
}
void CDemoDlg::OnBnClickedButton34()
{
CWnd* pEdit = GetDlgItem(IDC_EDIT1);
pEdit->SetFocus();
}
void CDemoDlg::OnBnClickedButton35()
{
CWnd* pWnd=GetActiveWindow();
CString str;
pWnd->GetWindowText(str);
MessageBox(str);
}
void CDemoDlg::OnBnClickedButton36()
{
CWnd* pWnd=GetForegroundWindow();
CString str;
pWnd->GetWindowText(str);
MessageBox(str);
}
void CDemoDlg::OnBnClickedButton37()
{
GetDlgCtrlID();
}
void CDemoDlg::OnBnClickedButton40()
{
CString str;
GetDlgItemText(IDC_EDIT1,str);
MessageBox(str);
}
void CDemoDlg::OnBnClickedButton39()
{
CString str;
BOOL b;
int n=GetDlgItemInt(IDC_EDIT1,&b,TRUE);
int a = 10;
}
void CDemoDlg::OnBnClickedButton41()
{
SetDlgItemText(IDC_EDIT1, L"123");
}
void CDemoDlg::OnBnClickedButton42()
{
SetDlgItemInt(IDC_EDIT1, 10,TRUE);
}
void CDemoDlg::OnBnClickedButton47()
{
static CWnd testButton;
testButton.Create(L"Button",L"Create创建",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(0,0,120,30),this,1099);
}
void CDemoDlg::OnBnClickedButton48()
{
static CWnd m_wnd;
LPCTSTR lpClassName=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,
LoadCursor(NULL, IDC_ARROW),
CreateSolidBrush(RGB(255, 0, 0)),
LoadIcon(NULL, IDI_ERROR));
m_wnd.CreateEx(WS_EX_APPWINDOW, lpClassName,
L"CreateEx",
WS_BORDER|WS_CAPTION|WS_MINIMIZEBOX|WS_MAXIMIZEBOX,
0,0,600,300,this->m_hWnd,NULL,NULL);
m_wnd.ShowWindow(SW_SHOW);
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c20f5b323840deb2854d9ec0fd98d5c8.png)
获取窗口名以及句柄函数
#include "pch.h"
#include "framework.h"
#include "Spy.h"
#include "SpyDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CSpyDlg::CSpyDlg(CWnd* pParent )
: CDialogEx(IDD_SPY_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSpyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CSpyDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CSpyDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CSpyDlg::OnBnClickedButton2)
END_MESSAGE_MAP()
BOOL CSpyDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
CListCtrl* pList =(CListCtrl*)GetDlgItem(IDC_LIST1);
pList->InsertColumn(0, L"窗口名", LVCFMT_LEFT, 150);
pList->InsertColumn(1, L"句柄", LVCFMT_LEFT, 150);
pList->InsertColumn(2, L"控件ID", LVCFMT_LEFT, 150);
return TRUE;
}
void CSpyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
HCURSOR CSpyDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CSpyDlg::OnBnClickedButton1()
{
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
CWnd* pWnd;
pWnd = this->GetWindow(GW_HWNDFIRST);
while (pWnd)
{
CString str;
pWnd->GetWindowText(str);
pList->InsertItem(0, str);
pWnd->m_hWnd;
str.Format(L"%#x", pWnd->m_hWnd);
pList->SetItemText(0, 1, str);
int nID=pWnd->GetDlgCtrlID();
str.Format(L"%d", nID);
pList->SetItemText(0, 2, str);
pWnd = pWnd->GetWindow(GW_HWNDNEXT);
}
}
void CSpyDlg::OnBnClickedButton2()
{
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
CWnd* pWnd;
pWnd = this->GetWindow(GW_CHILD| GW_HWNDFIRST);
while (pWnd)
{
CString str;
pWnd->GetWindowText(str);
pList->InsertItem(0, str);
str.Format(L"%#x", pWnd->m_hWnd);
pList->SetItemText(0, 1, str);
CWnd* pParentWnd = this->GetParent();
pWnd = pWnd->GetWindow(GW_HWNDNEXT);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/62273f90e26680e85ec1bb2831258159.png)