今天開始學習VC++,照着書上寫了幾個Win32的程式。
1,#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam,LPARAM lParam)
{
HDC hdc;//裝置環境句柄
PAINTSTRUCT ps;
RECT rect;
POINT point;
switch(iMsg)
{
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
DrawText(hdc,"Hello,VC6.0!",-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;
}
case WM_LBUTTONDOWN:
hdc = GetDC(hwnd);
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
Ellipse(hdc,point.x-50,point.y-50,point.x+50,point.y+50);
ReleaseDC(hwnd,hdc);
case WM_DESTROY:
PostQuitMessage(0);
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
static char szAppName[] = "HelloWin32";//應用程式名
HWND hwnd;//視窗句柄
MSG msg;//消息
WNDCLASSEX wndclass;//視窗類
wndclass.cbSize = sizeof(wndclass);//視窗類結構的大小
wndclass.style = CS_HREDRAW|CS_VREDRAW;//類風格:水準和垂直方向重畫
wndclass.lpfnWndProc = WndProc;//視窗過程
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;
wndclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
RegisterClassEx(&wndclass);
hwnd=CreateWindow(szAppName,
"The Hello App",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
TranslateMessage(&msg);
DispatchMessage(&msg);
return msg.wParam;
2,//t1.h
class CMyApp:public CWinApp
public:
virtual BOOL InitInstance();
};
class CMainWindow:public CFrameWnd
CMainWindow();
protected:
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags,CPoint point);
DECLARE_MESSAGE_MAP()
//t1.cpp
#include <afxwin.h>
#include "t1.h"
CMyApp myApp;
BOOL CMyApp::InitInstance()
this->m_pMainWnd = new CMainWindow;
this->m_pMainWnd->ShowWindow(this->m_nCmdShow);
this->m_pMainWnd->UpdateWindow();
return TRUE;
BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
Create(NULL,_T("The MFC Application"));
void CMainWindow::OnPaint()
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.DrawText("Weclome to VC!!",-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
void CMainWindow::OnLButtonDown(UINT nFlags,CPoint point)
CClientDC dc(this);
CRect rect(point.x-50,point.y-50,point.x+50,point.y+50);
dc.Ellipse(rect);
本文轉自Phinecos(洞庭散人)部落格園部落格,原文連結:http://www.cnblogs.com/phinecos/archive/2007/03/30/694562.html,如需轉載請自行聯系原作者