WPARAM,LPARAM

一只正在啃的蜗牛 2018-10-15 09:57:10
LONG yourWndProc(HWND hWnd,UINT uMessageType,WPARAM wP,LPARAM)
{
switch(uMessageType)
{//使用SWITCH语句将各种消息分开
case(WM_PAINT):
doYourWindow(...);//在窗口需要重新绘制时进行输出
break;
case(WM_LBUTTONDOWN):
doYourWork(...);//在鼠标左键被按下时进行处理
break;
default:
callDefaultWndProc(...);//对于其它情况就让系统自己处理
break;
}
}
怎么定义,
完整的流程是怎么样的
...全文
209 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
绿色盒子 2018-10-17
  • 打赏
  • 举报
回复
创建一个Win32应用程序的步骤:
① 编写WinMain函数,可以在MSDN上查找并复制;
② 设计窗口类;
③ 注册窗口类;
④ 创建窗口;
⑤ 显示并更新窗口;
⑥ 编写消息循环;
⑦ 编写窗口过程函数。窗口过程函数的语法,可通过MSDN查看WNDCLASS的lpfnWndProc成员变量,在这个成员的解释中可以查到。

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


LRESULT CALLBACK WinSunProc(
HWND hwnd, //窗口句柄
UINT uMsg, //消息代码,由TranslateMessage翻译得到
WPARAM wParam, //消息附加参数
LPARAM lParam //消息附加参数
);

int WINAPI WinMain(
HINSTANCE hInstance, //表示该程序当前运行的实例的句柄,这好似一个数值
HINSTANCE hPrevInstance, //表示当前实例的前一个实例的句柄
LPSTR lpCmdLine, //一个以空终止的字符串
int nCmdShow //指定程序的窗口应该如何显示
)
{
//设计一个窗口类
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_INFORMATION);
wndcls.hInstance=hInstance;//应用程序实例句柄由WinMain函数传进来
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="Window";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;

//注册窗口
RegisterClass(&wndcls);

//创建一个窗口,定义一个变量用来保存成功创建窗口后返回的句柄
HWND hwnd;
hwnd=CreateWindow("Window","窗口测试",WS_OVERLAPPEDWINDOW,
0,0,600,400,NULL,NULL,hInstance,NULL);

//显示及刷新窗口
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

//定义消息结构体,开始消息循环
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

//编写窗口过程函数
LRESULT CALLBACK WinSunProc(
HWND hwnd, //窗口句柄
UINT uMsg, //消息代码,由TranslateMessage翻译得到
WPARAM wParam, //消息附加参数
LPARAM lParam //消息附加参数
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char code is %d",wParam);
MessageBox(hwnd,szChar,"char",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"鼠标左击了","提示",0);//弹出消息窗口告诉用户点击了鼠标
HDC hdc;//创建一个DC对象
hdc=GetDC(hwnd);//用hdc保存GetDC函数返回的与特定窗口相关联的DC的句柄
TextOut(hdc,0,20,"Hello World!",strlen("Hello World!"));//在窗口(0,20)的位置处输出一行文字
ReleaseDC(hwnd,hdc);//释放DC所占用的资源,否则会引起内存泄漏
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"linsn",strlen("linsn"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if (IDYES==MessageBox(hwnd,"是否真的结束?","退出提示",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_RBUTTONDOWN:
HDC hdc1;
hdc1=GetDC(hwnd);
MoveToEx(hdc1,0,50,NULL);
LineTo(hdc1,400,50);
ReleaseDC(hwnd,hdc1);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
Eleven 2018-10-17
  • 打赏
  • 举报
回复
VS创建一个Win32 Application看一下先~
schlafenhamster 2018-10-16
  • 打赏
  • 举报
回复
WM_PAINT
An application sends the WM_PAINT message when the system or another application makes a request to paint a portion of an application's window. The message is sent when the UpdateWindow or RedrawWindow function is called, or by theDispatchMessage function when the application obtains a WM_PAINT message by using theGetMessage orPeekMessage function.

WM_PAINT
hdc = (HDC) wParam; // the device context to draw in

Parameters
hdc
Handle to the device context to draw in. If this parameter is NULL, use the default device context. This parameter is used by some common controls to enable drawing in a device context other than the default device context. Other windows can safely ignore this parameter.
Return Values
An application returns zero if it processes this message.

Remarks
TheDefWindowProc function validates the update region. The function may also send the WM_NCPAINT message to the window procedure if the window frame must be painted and send theWM_ERASEBKGND message if the window background must be erased.

The system sends this message when there are no other messages in the application's message queue. DispatchMessage determines where to send the message; GetMessage determines which message to dispatch. GetMessage returns the WM_PAINT message when there are no other messages in the application's message queue, and DispatchMessage sends the message to the appropriate window procedure.

A window may receive internal paint messages as a result of calling RedrawWindow with the RDW_INTERNALPAINT flag set. In this case, the window may not have an update region. An application should call the GetUpdateRect function to determine whether the window has an update region. If GetUpdateRect returns zero, the application should not call the BeginPaint and EndPaint functions.

An application must check for any necessary internal painting by looking at its internal data structures for each WM_PAINT message, because a WM_PAINT message may have been caused by both a non-NULL update region and a call to RedrawWindow with the RDW_INTERNALPAINT flag set.

The system sends an internal WM_PAINT message only once. After an internal WM_PAINT message is returned from GetMessage or PeekMessage or is sent to a window by UpdateWindow, the system does not post or send further WM_PAINT messages until the window is invalidated or until RedrawWindow is called again with the RDW_INTERNALPAINT flag set.

For some common controls, the default WM_PAINT message processing checks the wParam parameter. If wParam is non-NULL, the control assumes that the value is an HDC and paints using that device context.


WM_LBUTTONDOWN
The WM_LBUTTONDOWN message is posted when the user presses the left mouse button while the cursor is in the client area of a window. If the mouse is not captured, the message is posted to the window beneath the cursor. Otherwise, the message is posted to the window that has captured the mouse.

WM_LBUTTONDOWN
fwKeys = wParam; // key flags
xPos = LOWORD(lParam); // horizontal position of cursor
yPos = HIWORD(lParam); // vertical position of cursor

Parameters
fwKeys
Value of wParam. Indicates whether various virtual keys are down. This parameter can be any combination of the following values: Value Description
MK_CONTROL Set if the ctrl key is down.
MK_LBUTTON Set if the left mouse button is down.
MK_MBUTTON Set if the middle mouse button is down.
MK_RBUTTON Set if the right mouse button is down.
MK_SHIFT Set if the shift key is down.


xPos
Value of the low-order word of lParam. Specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.
yPos
Value of the high-order word of lParam. Specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.
Return Values
If an application processes this message, it should return zero.

Remarks
An application can use the MAKEPOINTS macro to convert the lParam parameter to a POINTS structure.

在帮助中 都可以找到的

みしつかん 2018-10-16
  • 打赏
  • 举报
回复
HWND hWnd 窗口句柄
UINT uMessageType 消息的类型
WPARAM wP 和LPARAM 是储存消息信息的2个参数
这框架都有了,自己定义下消息类型和消息的处理函数就好了啊
叶恭介叶恭介 2018-10-15
  • 打赏
  • 举报
回复
// Ex12_01.cpp Native windows program to display text in a window
#include <windows.h>

LRESULT WINAPI WindowProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);

// Insert code for WinMain() here (Listing OFWIN_1)
// Listing OFWIN_1
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WindowClass; // Structure to hold our window's attributes

static LPCTSTR szAppName = L"OFWin"; // Define window class name
HWND hWnd; // Window handle
MSG msg; // Windows message structure

WindowClass.cbSize = sizeof(WNDCLASSEX); // Set structure size

// Redraw the window if the size changes
WindowClass.style = CS_HREDRAW | CS_VREDRAW;

// Define the message handling function
WindowClass.lpfnWndProc = WindowProc;

WindowClass.cbClsExtra = 0; // No extra bytes after the window class
WindowClass.cbWndExtra = 0; // structure or the window instance

WindowClass.hInstance = hInstance; // Application instance handle

// Set default application icon
WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION);

// Set window cursor to be the standard arrow
WindowClass.hCursor = LoadCursor(0, IDC_ARROW);

// Set gray brush for background color
WindowClass.hbrBackground =
static_cast<HBRUSH>(GetStockObject(GRAY_BRUSH));

WindowClass.lpszMenuName = 0; // No menu
WindowClass.lpszClassName = szAppName; // Set class name
WindowClass.hIconSm = 0; // Default small icon

// Now register our window class
RegisterClassEx(&WindowClass);

// Now we can create the window
hWnd = CreateWindow(
szAppName, // the window class name
L"A Basic Window the Hard Way", // The window title
WS_OVERLAPPEDWINDOW, // Window style as overlapped
CW_USEDEFAULT, // Default screen position of upper left
CW_USEDEFAULT, // corner of our window as x,y...
CW_USEDEFAULT, // Default window size
CW_USEDEFAULT, // ....
0, // No parent window
0, // No menu
hInstance, // Program Instance handle
0 // No window creation data
);

ShowWindow(hWnd, nCmdShow); // Display the window
UpdateWindow(hWnd); // Cause window client area to be drawn

// The message loop
while(GetMessage(&msg, 0, 0, 0) == TRUE) // Get any messages
{
TranslateMessage(&msg); // Translate the message
DispatchMessage(&msg); // Dispatch the message
}

return static_cast<int>(msg.wParam); // End, so return to Windows
}

// Insert code for WindowProc() here (Listing OFWIN_2)
// Listing OFWIN_2
LRESULT WINAPI WindowProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hDC; // Display context handle
PAINTSTRUCT PaintSt; // Structure defining area to be drawn
RECT aRect; // A working rectangle

switch(message) // Process selected messages
{
case WM_PAINT: // Message is to redraw the window
hDC = BeginPaint(hWnd, &PaintSt);// Prepare to draw the window

// Get upper left and lower right of client area
GetClientRect(hWnd, &aRect);

SetBkMode(hDC, TRANSPARENT); // Set text background mode

// Now draw the text in the window client area
DrawText(
hDC, // Device context handle
L"But, soft! What light through yonder window breaks?",
-1, // Indicate null terminated string
&aRect, // Rectangle in which text is to be drawn
DT_SINGLELINE| // Text format - single line
DT_CENTER| // - centered in the line
DT_VCENTER); // - line centered in aRect

EndPaint(hWnd, &PaintSt); // Terminate window redraw operation
return 0;

case WM_DESTROY: // Window is being destroyed
PostQuitMessage(0);
return 0;

default: // Any other message - we don't
// want to know, so call
// default message processing
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
oyljerry 2018-10-15
  • 打赏
  • 举报
回复
主要就是各种窗口消息,直接MSDN文档看看
  • 打赏
  • 举报
回复
还有其他形式?
class CNewsWindow { public: CNewsWindow(void); ~CNewsWindow(void); public: HWND m_hWnd; public: // 设置皮肤(位图资源名称,标题栏文本颜色) BOOL SetSkin(LPCTSTR lpSkinBitmapName,COLORREF CaptionColor=0x000000); // 创建窗口 BOOL Create(LPCTSTR lpWindowName,int nWidth=250,int nHeight=180); BOOL Create(LPCTSTR lpClassName,LPCTSTR lpWindowName,int nWidth,int nHeight); // 设置新闻标题、内容、链接 BOOL SetNews(LPCTSTR lpNewsTitle,LPCTSTR lpNewsContent,LPCTSTR lpNewsURL); // 显示窗口 void Show(); // 设置为主窗口,主窗口销毁后会退出消息循环 void SetMainWindow(BOOL bMainWindow=TRUE); // 设置自动关闭,如果为真,点击链接后窗口自动关闭 void SetAutoClose(BOOL bAutoClose=TRUE); public: BOOL IsWindow(); BOOL DestroyWindow(); private: #define NCT_CLOSE 0 #define NCT_VIEW 1 #define NCT_TITLE 2 #define NCT_CONTENT 3 typedef struct tagNEWSCONTROL { CString strText; int nType; int x; int y; int nWidth; int nHeight; RECT Rect; }NEWSCONTROL, *LPNEWSCONTROL; private: LPNEWSCONTROL m_pControls; int m_nControlCount; private: HCURSOR m_hArrowCursor; HCURSOR m_hHandCursor; HCURSOR m_hCurCursor; HICON m_hAppSmallIcon; HDC m_hSkinDC; HDC m_hCacheDC; HBITMAP m_hSkinBitmap; HBITMAP m_hSkinOldBitmap; HBITMAP m_hCacheBitmap; HBITMAP m_hCacheOldBitmap; HFONT m_hFont; HFONT m_hBoldFont; COLORREF m_CaptionColor; int m_nHoverIndex; int m_nDownIndex; BOOL m_bMainWindow; BOOL m_bAutoClose; BOOL m_bTracking; CString m_strURL; private: BOOL DrawWindow(); BOOL DrawWindowEx(); void DrawButton(HDC hDC,LPNEWSCONTROL pControl); void DrawStatic(HDC hDC,LPNEWSCONTROL pControl); void DrawNineRect(HDC hdcDest,RECT DestRect,RECT SrcRect,RECT NineRect,UINT crTransparent=0xFF00FF); RECT CreateRect(LONG left,LONG top,LONG right,LONG bottom); void CreateControl(LPNEWSCONTROL pControl,int nType,int x,int y,int nWidth,int nHeight,CString strText=_T("")); int ControlFromPoint(POINT pt); int ControlFromPoint(LPARAM lParam); void SetCursor(HCURSOR hCursor); public: LRESULT OnMessage(UINT message, WPARAM wParam, LPARAM lParam); LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam); LRESULT OnEraseBkgnd(UINT message, WPARAM wParam, LPARAM lParam); LRESULT OnPaint(UINT message, WPARAM wParam, LPARAM lParam); LRESULT OnMouseMove(UINT message, WPARAM wParam, LPARAM lParam); LRESULT OnMouseHover(UINT message, WPARAM wParam, LPARAM lParam); LRESULT OnMouseLeave(UINT message, WPARAM wParam, LPARAM lParam); LRESULT OnLButtonDown(UINT message, WPARAM wParam, LPARAM lParam); LRESULT OnLButtonUp(UINT message, WPARAM wParam, LPARAM lParam); LRESULT OnControlClick(UINT message, WPARAM wParam, LPARAM lParam); LRESULT OnDestroy(UINT message, WPARAM wParam, LPARAM lParam); LRESULT OnSetCursor(UINT message, WPARAM wParam, LPARAM lParam); };

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧