|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 qq1242009750 于 2018-9-10 20:18 编辑
本贴来实现以下功能:
1.由于我们用非模式对话框来模拟模式对话框,所以我们在创建主窗口的时候不要让主窗口显示出来,所以要把ShowWindow函数和WS_VISIBLE风格去掉
2.在主窗口创建消息里(WM_CREATE)创建一个非模式对话框,此对话框要用CreateDialog函数来创建,并用一个全局变量来接受它的返回值
3.既然用到了非模式对话框,那么就要在消息循环里抓取它的消息,因为消息循环里有主窗口的消息也有非模式对话框的消息,所以我们要用IsDialogMessage函数来判断消息的来源,消息循环里我们只派发和翻译主窗口的消息
首先 我们来创建好大致的框架:#include <tchar.h>
#include <windows.h>
#define CLASSNAME TEXT("Name")
void CfiWndClass(HINSTANCE hInstance);
LRESULT _stdcall WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void CfiWndClass(HINSTANCE hInstance)
{
WNDCLASSEX wcx;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcx.hCursor = LoadCursor(hInstance, IDC_ARROW);
wcx.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcx.hIconSm = NULL;
wcx.hInstance = hInstance;
wcx.lpfnWndProc = WndProc;
wcx.lpszClassName = CLASSNAME;
wcx.lpszMenuName = NULL;
wcx.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&wcx);
}
int _stdcall _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
CfiWndClass(hInstance);
HWND hWnd = CreateWindow(CLASSNAME, TEXT("GsssD"), WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, hInstance, 0);
//因为我们用的是非模式对话框,但是又要达到模式对话框的效果所以我们把能显示主窗口的函数去掉
//ShowWindow(hWnd, nShowCmd);
//UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT _stdcall WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
第二步,在资源代码中创建一个对话框:
.
在这里给大家一个小Tips 滚动条可以在工具箱里拖取出来:
]
我们需要三个滚动条,六个静态文本,一个列表框,一个自绘制的按钮
完成后,是这个样子滴:
]
因为滚动条下面的静态文本的内容时候改变的,所以要给他们不同的ID
好了,那么接下来我们来创建非模式对话框吧#include <tchar.h>
#include <windows.h>
#include "resource.h"
#define CLASSNAME TEXT("Name")
void CfiWndClass(HINSTANCE hInstance);
LRESULT _stdcall WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
INT_PTR _stdcall DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
HWND DlghWnd;
void CfiWndClass(HINSTANCE hInstance)
{
WNDCLASSEX wcx;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcx.hCursor = LoadCursor(hInstance, IDC_ARROW);
wcx.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcx.hIconSm = NULL;
wcx.hInstance = hInstance;
wcx.lpfnWndProc = WndProc;
wcx.lpszClassName = CLASSNAME;
wcx.lpszMenuName = NULL;
wcx.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&wcx);
}
int _stdcall _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
CfiWndClass(hInstance);
HWND hWnd = CreateWindow(CLASSNAME, TEXT("GsssD"), WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, hInstance, 0);
//ShowWindow(hWnd, nShowCmd);
//UpdateWindow(hWnd);
MSG msg;
//先获得消息,然后在判断是哪种消息
while (GetMessage(&msg, nullptr, 0, 0))
{
if (DlghWnd == 0 || !IsDialogMessage(DlghWnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
LRESULT _stdcall WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
DlghWnd = CreateDialog((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), MAKEINTRESOURCE(IDD_DIALOG1), hWnd, DlgProc);
ShowWindow(DlghWnd, SW_SHOW);
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
INT_PTR _stdcall DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
BOOL Ret = FALSE;
switch (message)
{
case WM_CLOSE:
DestroyWindow(hDlg);
Ret = TRUE;
break;
case WM_DESTROY:
SendMessage(GetParent(hDlg), WM_DESTROY, 0, 0);
Ret = TRUE;
break;
}
return Ret;
}
好,我们来看看效果:
哈哈,是不是很爽呢? 有人会问了按钮怎么不见了?这个是正常现象,因为按钮要我们自己绘画。
好了,这次的功能就做到这里吧!
传送门:对话框(非模式)调色板①
传送门:对话框(非模式)调色板③
|
|